Indexing to control if statements

Hello all, I'm learning programming for the first time for my senior design project and I could use a hand.

I have a loop full of if statements and I need them to trigger in consecutive order, so I have assigned a variable 'stage' that increases by one within each if statement, then I make the next if statement require my 'stage' variable to be a certain number before it will run that command. In this example I'm just trying to swap between stages 0 and 1 consecutively.

//define constants
const int dirPin = 2;  // Direction
const int stepPin = 3; // Step
const int dirPin_2 = 4;
const int stepPin_2 = 5;
const int solenoidPin = 7; //solenoid
const int Detector = 8; //laser receiver

const int STEPS_PER_REV = 200;

int time;
int dir;
int steps;
int speed;
int stage = 0;


void setup() {
  
    // Setup the pins
  pinMode(stepPin,OUTPUT); 
  pinMode(stepPin_2,OUTPUT);
  pinMode(dirPin,OUTPUT);
  pinMode(dirPin_2,OUTPUT);
  pinMode(solenoidPin, OUTPUT);
  pinMode(Detector, INPUT);

}

  //solenoid function
int solenoidControl(int time) {
  digitalWrite(solenoidPin, HIGH); 
    delay(time);                          
    digitalWrite(solenoidPin, LOW);       
    delay(10);
}

  //stepper function
int motorControl(int dir, int steps, int speed) {

    //direction
  digitalWrite(dirPin,dir);

for(int x = 0; x < steps; x++) { //"steps" controls number of rotations
    digitalWrite(stepPin,HIGH);
    delayMicroseconds(speed); //controls speed
    digitalWrite(stepPin,LOW); 
    delayMicroseconds(speed);
  }
}

void loop() {

if (stage = 0) {
  
  if (digitalRead(Detector) == LOW) {
int time = 1000;
solenoidControl(time);

delay(10);

int dir = LOW;
int steps = STEPS_PER_REV*2;
int speed = 2000;
motorControl(dir, steps, speed);

int stage = 1;

delay(10);
    }
}

if (stage = 1) {

  if (digitalRead(Detector) == LOW) {
  int dir = HIGH;
  int steps = STEPS_PER_REV;
  int speed = 1000;
  motorControl(dir, steps, speed);

  int stage = 0;
  
  delay (10);
}
}
}

When I go to run this code, the motor just sits there and spins, the solenoid doesn't even move.

Once I get this basic indexing to work I will apply what I learn to my full code which will have 60 stages or so.

Any ideas as to why this is happening?

Thanks in advance :slight_smile:

if (stage = 1)

Single equal sign (=) means assignment, double equal sign (==) is an equality test.

There might be other problems as well.
Please use the auto-format function of the IDE when you post your code.

Pieter

PieterP:
Single equal sign (=) means assignment, double equal sign (==) is an equality test.

Thanks! That got stage 0 working properly, but it isn't swapping between the two stages.

Don't make us guess which changes you made :wink:

You are shadowing the "stage" variable. You have one global "stage" variable that is used in the conditions of the if statements, and two other local "stage" variables inside of the bodies of the if statements.
This might be a good moment to learn about scope as well.

Replace the "int stage = x" inside of the if statements with "stage = x".

PieterP:
You are shadowing the "stage" variable. You have one global "stage" variable that is used in the conditions of the if statements, and two other local "stage" variables inside of the bodies of the if statements.
This might be a good moment to learn about scope as well.

Replace the "int stage = x" inside of the if statements with "stage = x".

That did the trick, thanks!

You don't actually need the series of if statements because once you set stage to 1 in the stage equals 0 section the test for stage equals 1 will always be true so it is redundant and you might just as well execute the code unconditionally