However, my next problem is this-- Once I stop the program by typing '0' into serial monitor, how do I re-start the whole program? It's a whole lot trickier.
Clown_of_God:
However, my next problem is this-- Once I stop the program by typing '0' into serial monitor, how do I re-start the whole program? It's a whole lot trickier.
No, it's not trickier
bwt use the # key to get code- formatting.
You cannot stop the loop, but you can bypass code
in pseudocode
void loop(){
//first read incomming byte
//then
if(inkomming=='o'){
//execute some sode
}
else{
//bypass execution or do something else
}
}//end loop
Or read your references and use while or other logical structure
Thanks! Now I have another issue:
I can stop the program by making the code do while(1) { }. The other option is that instead of the while loop, I can use the delay function: "delay(1year)" in order to stop the code. Which is the better option?
Clown_of_God:
Thanks! Now I have another issue:
I can stop the program by making the code do while(1) { }. The other option is that instead of the while loop, I can use the delay function: "delay(1year)" in order to stop the code. Which is the better option?
are you a troll?
A delay will prevent you from doing anything while it last.
Take a look at switch case. That's the one you want
Clown_of_God:
Thanks! Now I have another issue:
I can stop the program by making the code do while(1) { }. The other option is that instead of the while loop, I can use the delay function: "delay(1year)" in order to stop the code. Which is the better option?
What behaviour do you actually want? You've talked about 'resuming' the sketch based on another input, and if that's what you want then this has a big impact on how you should implement the 'stop' command. For example, one possible implementation would be to do into a loop waiting for the 'resume' character to arrive on the serial port.
Basically, I want to be able to implement a "button-like on-off switch" in my sketch, where 1 will always turn on the motor and 0 will always turn off the motor. I can keep pressing 1 to turn it on, 0 to turn it off, 1 to turn it on, over and over and over.
Clown_of_God:
Basically, I want to be able to implement a "button-like on-off switch" in my sketch, where 1 will always turn on the motor and 0 will always turn off the motor. I can keep pressing 1 to turn it on, 0 to turn it off, 1 to turn it on, over and over and over.
In that case you aren't trying to stop the sketch at all - you're just turning the motor on and off when commanded. That's trivially easy to do and doesn't involve making your sketch stop or start.
Wait for a command.
If the command is 'on' switch the motor on.
If the command is 'off' switch the motor off.
Clown_of_God:
Basically, I want to be able to implement a "button-like on-off switch" in my sketch, where 1 will always turn on the motor and 0 will always turn off the motor. I can keep pressing 1 to turn it on, 0 to turn it off, 1 to turn it on, over and over and over.
In that case you aren't trying to stop the sketch at all - you're just turning the motor on and off when commanded. That's trivially easy to do and doesn't involve making your sketch stop or start.
Wait for a command.
If the command is 'on' switch the motor on.
If the command is 'off' switch the motor off.
Try something more like this:
char MotorRun = 0;
void loop() {
if (Serial.available() > 0) {
incomingByte = Serial.read(); // read the incoming byte:
switch (incomingByte) {
case '1':
MotorRun = 1;
case '0':
MotorRun = 0;
default: break;
}
if ( MotorRun = 1 ) {
myStepper.step(55.5);
myStepper.step(-55.5);
}
}
}
Typically, you have a "break" at the end of each case statement. And you also dont have any statement for when the case is '0'. Just tested this code above and it doesn't work. When I type in '1' or '0', no response at all from motor.