Code skipping?

Good eveing,

I am quite new to the world of C with only very little/basic experience but have been playing with PHP/mySQL and HTML for a while so have some comprehension of loops...but:

I have used some of the provided code to try and make a stepper motor (which works by the way) start once a button has been pressed.

The analog pin A0 does pick up the voltage as intended, but the code seems to just start the stepper motor BEFORE the switch is pressed...

I am using a voltage splitter to give the 3 buttons on one pin but they give the following values: 5v, 3.2v and 2.4v. They are consistent when the device is going and I get regular voltage output from them.

My code is not the best formatted (being quite new to the whole thing) but I am assuming I have screwed up a loop or done something daft...

/*  */
 
#include <Stepper.h>

const int stepsPerRevolution = 2500;  // change this to fit the number of steps per revolution
                                     // for your motor

// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, 10,12,9,11);            

int stepCount = 0;         // number of steps the motor has taken

void setup() {
  // initialize the serial port:
  Serial.begin(9600);
  
  pinMode(A0, INPUT); // READS voltage from 3 buttons.
  pinMode(A5, INPUT);
  
  digitalWrite(12, LOW); // These are the pins to ULN2003A to control stepper motor //
  digitalWrite(11, LOW);// These are the pins to ULN2003A to control stepper motor //
  digitalWrite(10, LOW);// These are the pins to ULN2003A to control stepper motor //
  digitalWrite(9, LOW);// These are the pins to ULN2003A to control stepper motor //
  
delay(1000);// Included a delay to let the system "settle" before it takes any input from user. //
}





void loop() {
  float voltage;
  int sensorValue;
  
  sensorValue = analogRead(A0);
  voltage = (sensorValue * (5.0/1023)); //Outputs the A0 analog voltage reading as a int of 0-5.
  delay(50);
  
Serial.println(sensorValue); //Prints the value for my debugging - has been showing as 0-0.1V
 
  if (voltage>4.5){ //Setting If loop to check for the third (5V button) being presses...but motor usually starts before this!
  
    int sensorValue = analogRead(A0);
    float voltage = (sensorValue * (5.0/1023));
    delay(50);
  }
 
    
 
  
 

while(stepCount<2500){ // This should have started once the If statement has been satisfied
 
  myStepper.step(1); //Move the stepper on one cycle
  stepCount++; // Adds 1 to int stepCount
  
   int sensorValue = analogRead(A0); //This is for my debugging, which shows the three buttons outputting the correct values as I had intended during the motor turning...
   float voltage = (sensorValue * (5.0/1023));
   Serial.print("Step taken and voltage on pinA0 is ");
   Serial.print(voltage);
   Serial.print(" and stepCount is ");
   Serial.print(stepCount);
   Serial.println("");
   
   delay(10);
}
}

And here is the output:

0
Step taken and voltage on pinA0 is 0.00 and stepCount is 1
Step taken and voltage on pinA0 is 0.00 and stepCount is 2
Step taken and voltage on pinA0 is 0.00 and stepCount is 3
Step taken and voltage on pinA0 is 0.00 and stepCount is 4
Step taken and voltage on pinA0 is 0.00 and stepCount is 5
Step taken and voltage on pinA0 is 0.00 and stepCount is 6
Step taken and voltage on pinA0 is 0.00 and stepCount is 7
Step taken and voltage on pinA0 is 0.00 and stepCount is 8
Step taken and voltage on pinA0 is 0.00 and stepCount is 9

GOES ON A LITTLE AND I THEN PUSH BUTTON 3

Step taken and voltage on pinA0 is 5.00 and stepCount is 117
Step taken and voltage on pinA0 is 5.00 and stepCount is 118
Step taken and voltage on pinA0 is 5.00 and stepCount is 119
Step taken and voltage on pinA0 is 5.00 and stepCount is 120
Step taken and voltage on pinA0 is 5.00 and stepCount is 121
Step taken and voltage on pinA0 is 5.00 and stepCount is 122
Step taken and voltage on pinA0 is 5.00 and stepCount is 123
Step taken and voltage on pinA0 is 5.00 and stepCount is 124
Step taken and voltage on pinA0 is 5.00 and stepCount is 125
Step taken and voltage on pinA0 is 5.00 and stepCount is 126

Thank you for any help... I am probably doing something stupid...

The IF statement doesnt loop, the condition is checked only once, then the program moves on.

What you have in mind is this:

while(!(voltage>4.5)) {            //as long as voltage isn't bigger than 4.5, do these:
//If voltage on A0 was not more than 4.5V, then keep checking till it is...
    int sensorValue = analogRead(A0);
    float voltage = (sensorValue * (5.0/1023));
    delay(50);
 
   Serial.println(sensorValue); //For my debugging
}

Thanks!

Everyday is a learning day :(.

It now seems to be stuck in the while loop even though the voltage is going over 4.5...

while (!(voltage>4.5)){ 
  
    int sensorValue = analogRead(A0);
    float voltage = (sensorValue * (5.0/1023));
    Serial.println("In while loop and voltage is ");    Serial.print(voltage);
    delay(50);
  }

Output while pressing button:

5.00In while loop and voltage is 
4.99In while loop and voltage is 
5.00In while loop and voltage is 
5.00In while loop and voltage is 
4.99In while loop and voltage is 
5.00In while loop and voltage is 
5.00In while loop and voltage is 
5.00In while loop and voltage is 
5.00In while loop and voltage is 
5.00In while loop and voltage is 
5.00In while loop and voltage is 
5.00In while loop and voltage is 
5.00In while loop and voltage is 
5.00In while loop and voltage is 
5.00In while loop and voltage is 
5.00In while loop and voltage is 
5.00In while loop and voltage is 
5.00In while loop and voltage is 
5.00In while loop and voltage is 
5.00In while loop and voltage is

Try changing this:

int sensorValue = analogRead(A0);
float voltage = (sensorValue * (5.0/1023));

with this:

sensorValue = analogRead(A0);
voltage = (sensorValue * (5.0/1023));

Ah great yeah.
I see I was just re-defining the int and float each time so the values were trapping the loop!