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
}
