Need to break the Stepper motor operation within "for loop". But the code that I have written breaks the operation after the completion of the loop, it does not break the operation between the loop. Please chek the code and tell me any possible way to stop the loop in between..
Code:
#include <Stepper.h>
int in1Pin = 8;
int in2Pin = 9;
int in3Pin = 10;
int in4Pin = 12;
bool entry = false;
int j;
Stepper motor(200, in1Pin, in2Pin, in3Pin, in4Pin);
void setup() {
pinMode(in1Pin, OUTPUT);
pinMode(in2Pin, OUTPUT);
pinMode(in3Pin, OUTPUT);
pinMode(in4Pin, OUTPUT);
while (!Serial);
Serial.begin(9600);
motor.setSpeed(300);
Serial.println("Type in your selection");
entry = false;
}
void loop() {
if (Serial.available() > 0){
switch(Serial.read()){
case 'a':
entry = true;
break;
case 'b':
entry = false;
break;
default:break;
}
}
if(entry == true){
for(j = -20; j <= 20; j++){
motor.step(j/0.176);
}
}
else if(entry == false){
digitalWrite(in1Pin,LOW);
digitalWrite(in2Pin,LOW);
digitalWrite(in3Pin,LOW);
digitalWrite(in4Pin,LOW);
}
}
From Comments: when i send 'a' through serial monitor, the stepper starts rotating, when i send 'b' through serial it should break stepper motor rotation, but its breaking only after the completion of loop (not within the loop)