How to STOP an Arduino sketch by typing '0' into serial monitor

How do I STOP an Arduino sketch by typing '0' into serial monitor? This below code doesn't work; how do I fix it?

int incomingByte = 0; // for incoming serial data

void setup() {
}

void loop() { // send data only when you receive data:
if (Serial.available() >= 0) { // read the incoming byte:
incomingByte = Serial.read();

// say what you got:
Serial.print("I received: ");
Serial.println(incomingByte);

if (incomingByte==0) {
while(1) { }
}

}

}

Thanks! It works now.

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.

Well, of course, you haven't stopped anything, you're just running around in circles.
They're easy to break out of.

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?

The best one is the one that works, whatever "works" means to you.
However, you can't put enough milliseconds in a long to last a year.

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.

The switch-case doesn't quite work. Here's the code:

void loop() {

if (Serial.available() > 0) {
incomingByte = Serial.read(); // read the incoming byte:
switch (incomingByte) {
case '1':
while (1) {
myStepper.step(55.5);
myStepper.step(-55.5); }
//break;
case '0':
while(1) { };
//break;
default: break;
}
}
}

Why doesn't it work? I type in '1', the motor moves back+forth. When I type in '0', no response at all. WHY?

Because the code is stuck in the while(1){} loop with nothing to make it exit.

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.

PeterH:

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);
}
}
}
if ( MotorRun = 1 ) {

No.

@op: Please learn how to use code tags.

http://arduino.cc/forum/index.php/topic,97455.0.html

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.

Never mind. Fixed a stupid error. Your code works perfectly now.

This is not an exact answer to the original question but if you just want to exit the loop by hitting any key, this will work:

while (1) { //continuous.

if (Serial.available() > 0) {
Serial.println("Exiting...");
break;//exit while loop if any key is hit.
}

//your code here

} //end of while