IF loop statement not looping as intended

Hi all,

I'm working on a project for my final year and created a device that help to accelerate a certain process in sheetmetal bending on marking out lines that guides user when bending sheetmetal. So far the device comprise an Arduino nano with a stepper motor with a gear system as the stepper motor can move the steps accurately.

The problem I've been running into is about the if statement loop. Doing a test on the device, it only loops once and carried on to loop over from the start instead. The codes involving the if loop issue are the if statement in the void loop() (line 56 to 62) and String reset() variable (line 122 to 129).

Edit 1: For context, what I'm trying to achieve is for the if statement to keeping looping until the String text "Y" is entered correctly but instead have looped once and carried on to restart void loop() which is intended after the String text "Y" is entered correctly.

I hope you could point out the cause of the issue will be great. Thanks

#include <Stepper.h>//declare this so circuit board can move motor

//Motor settings
int stepsPerRevolution=2038; // amounts steps get one revolution
int Speed=8;// Speed of motor
int dt=1000;//delay time in ms

//Minimum Bend Radius
String MRad_s1= "What is your mininimum bend radius?";//string msg of asking for bend radius
float MRad_fl;//declare number for bend radius in decimal points
String MRad_s2= "Minimum bend radius: ";//string msg of displaying bend radius (past tense) read

//K-Value
String Kval_s1="What is your K-Value?";//string msg of asking for K-Value
float Kval_fl;//declare number for K-Value in decimal points
String Kval_s2="K-Value: ";//string msg of displaying K-Value (past tense) read

//Thickness
String Thicc_s1="What is the thickness of the sheetmetal?";//string msg of asking for thickness of metal
float Thicc_fl;//declare number for thickness of metal in decimal points
String Thicc_s2="Thickness: ";//string msg of displaying thickness of metal  (past tense) read
String in="in";//display 

//Setback
String SB_s="Setback distance: ";// string msg of showing calculated setback distance
float SB_fl;//declare nummber for setback in decimal points

//Motor movement
String Motor_s1="Number of steps: ";


//Home/reset
String Home_s1="Reset back?";
String Home_s2;

//Stepper motor setup
Stepper myStepper(stepsPerRevolution, 8,10,9,11);// declare circuit board what pins the motor is connected


void setup() 
{
  // put your setup code here, to run once:
Serial.begin(9600);// basic startup for display
myStepper.setSpeed(Speed);//setup speed of motor
}

void loop()
{ 

bend_info();//ask bend radius info
K_info();//ask k value info
thic_info();// ask thickness info
setback_info();//calculated and display setback distance
float HomeSteps=motor_move();//move motor and get number of steps for motor to home back
String r1=reset();//ask whether reset/home back the motor
if (r1=="Y")//reset/home the motor
{
     myStepper.step(-HomeSteps);}//reset/home back by using negative value of steps
else if(r1!="Y")//loop reset() variable
{ 
  reset();//loop reset()variable
}
//void loop will loop to start (bend_info())
}

//Bend radius info
void bend_info(){
    Serial.println(MRad_s1); //output message asking for minimum bend radius
    while(Serial.available()==0){}//wait for user input
    MRad_fl=Serial.parseFloat();//declare parseFloat for board to read and save user's input on serial monitor 
    Serial.print(MRad_s2);//output string bend radius (past tense) read
    Serial.print(MRad_fl,3);//output float bend radius (past tense) read togther with string text code above
    Serial.println(in);//output inches unit beside above output code
    delay(dt);//delay
}


//K-Value info
void K_info(){
    Serial.println(Kval_s1); //output message asking for K-Value
    while(Serial.available()==0){}//wait for user input
    Kval_fl=Serial.parseFloat();//declare parseFloat for board to read and save user's input on serial monitor 
    Serial.print(MRad_s2);//output string bend radius (past tense) read
    Serial.print(Kval_s2);//output string K-Value (past tense) read
    Serial.println(Kval_fl,4);//output float k-Value (past tense) read togther with string text code above
    delay(dt);//delay
}

//Thickness info
void thic_info(){
    Serial.println(Thicc_s1); //output message asking for Thickness
    while(Serial.available()==0){}//wait for user input
    Thicc_fl=Serial.parseFloat();//declare parseFloat for board to read and save user's input on serial monitor 
    Serial.print(MRad_s2);//output string bend radius (past tense) read
    Serial.print(Thicc_s2);//output string Thickness (past tense) read
    Serial.print(Thicc_fl,3);//output float Thickness (past tense) read togther with string text code above
    Serial.println(in);//output inches unit beside above output code
    delay(dt);//delay
}


//Setback info
void setback_info(){
    SB_fl= Kval_fl*(MRad_fl+Thicc_fl);//calculation formula for setback
    Serial.print(SB_s); //output message saying Setback distance
    Serial.print(SB_fl,4);//output calculated Setback value
    Serial.println(in);//output inches unit beside above output code
    delay(dt);//delay
}


//Motor movement
float motor_move(){
    float MotorSteps=SB_fl*(2038/2.11); //convert Setback distance to number of steps
    Serial.print(Motor_s1); //output string stating about number of steps
    Serial.println(MotorSteps);//output number calculated steps for motor to move
    myStepper.step(MotorSteps);//moves the motor to number of steps
    return MotorSteps;//send number of steps back to loop
}

//Reset info/home back the motor
    String reset(){
    Serial.println(Home_s1);//output string asking whether to reset
    while(Serial.available()==0){}//wait for user input
    String Home_s2=Serial.readString();//read string from serial monitor input
    Serial.println(Home_s2);//output string user inputted
    return Home_s2;//send string text back to loop

}

Try turning off line ending in the monitor.

image

Hello ashowerhandle
Try these mods:

if (r1=='Y')//reset/home the motor
{
     myStepper.step(-HomeSteps);}//reset/home back by using negative value of steps
else if(r1!='Y')//loop reset() variable
{ 

Have a nice day and enjoy programming in C++ and learning.
Дайте миру шанс!

I haven't had a chance to have a proper look to see what you might be getting at, but maybe you have a misconception about how if() works? It's not a loop....

1 Like

This has already been done from the start.

yea i still quite new to the programming code, havent really get a full grasp of understanding how it works tbh

Have changed but it didn't work in looping the if statement properly

As @octopirate mentioned in post4, you may be expecting the else if statement to keep looping. But that is not what happens.
The else if evaluates once only; if the condition is true (r1!="Y"), it will run the reset() function once and then move on:

Perhaps what you want is:

while (r1 != "y") {
reset();
}

Note this will be a blocking process, and the program will stay stuck here until the while loop ends.

Thx for the suggestion and feedback on my misconception @cncnotes . So far I've tried using the while loop statement and as you warned, is stuck within the while loop. For now, I've isolated the resetting code into another sketch and test it from there. I tried using a break statement for the while loop to break the loop but is still looping.

Could you help point out whats wrong with the code below?

//Home/reset
String Home_s1="Reset back?";
String Home_s2;
void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);// basic startup for display
}

void loop() 
{
  // put your main code here, to run repeatedly:
String r1=reset();//ask whether reset/home back the motor

  while(r1 !="Y")//loop reset() variable
  { 
  reset();//loop reset()variable
    if (r1 =="Y")//reset/home the motor
    {
    Serial.println("Reseted");
     break;
    }
  }
}

//Reset info/home back the motor
    String reset(){
    Serial.println(Home_s1);//output string asking whether to reset
    while(Serial.available()==0){}//wait for user input
    String Home_s2=Serial.readString();//read string from serial monitor input
    Serial.println(Home_s2);//output string user inputted
    return Home_s2;//send string text back to loop
}

Well for a start @ashowerhandle it calls the reset() function which isn't in the code shown in #10. But let's assume that it's the one from the opening post and is actually there and correctly asks the user if they want to reset and returns the answer.

Let's assume that the user answers Z to this line:

String r1=reset();//ask whether reset/home back the motor

So then that gets you into the while() because Z != Y.

But then this line:

reset();//loop reset()variable

... doesn't use the value returned from the new ask, and just chucks it away. So that this line:

if (r1 =="Y")//reset/home the motor

... can never be true since you got there by dint of the input not being Y and you haven't changed it.

Did you mean:

reset();//loop reset()variable
// to actually be:
r1=reset();//loop reset()variable
// ?????

I think what I just said is right; it's a bit convoluted to unravel and I haven't fully woken up yet. E&OE.

With a couple of print statements you can figure out what is going on...

void loop()
{
  // put your main code here, to run repeatedly:
  Serial.println("Top of loop");
  String r1 = reset(); //ask whether reset/home back the motor

  while (r1 != "Y") //loop reset() variable
  {
    Serial.println("Top of while");
    reset();//loop reset()variable
//    r1 = reset();  <<< missing assignment in line above
    if (r1 == "Y") //reset/home the motor
    {
      Serial.println("Reseted");
      break;
    }
  }
}

And when you do that you can see that @octopirate is correct.. you are not assigning the result of reset() to r1... and therefore never get out of the while loop.

Serial.print is your friend... use it.

I've not been following this thread closely, so maybe there's a good reason, but I'm wondering why that stuff in #10 is in a while() in the first place? As you now know @ashowerhandle if() doesn't loop so maybe that's why it's in a while() but never forget that loop() is doing the looping. That means that an if() can get asked a zillion times a second, once each time thru loop(), almost as if the if() itself was loopy.

Hell yeah- after all, it's pretty much the only debug tool we have!

@octopirate @cncnotes Yeap the code is working as intended. Thank you guys so much for the help and advice :grin:

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.