So I've wired up a little robot with a sound shield and some sensors. I'm trying to write a sketch that will let check the sensors.
What I'd like for it to do is print out a little menu over serial, wait until the user sends a selection, jump to the function that matches their selection, then (once the function is done) jump back and print the menu again. What it actually does is just loop the menu again and again, completely bypassing the while loop (I've tested this by putting a Serial.print in the loop to see if it gets in or not).
Here's what I've written, but I'm not a that good of a coder, so it doesn't work. Where am I going wrong?
#include <Servo.h>
Servo steering;
Servo throttle;
int pos = 0;
int val = 0;
void setup(){ Serial.begin(9600);
throttle.write(90);
steering.write(90);
pinMode(A0, INPUT);
pinMode(7, INPUT);
char ch = 0;
}
void loop(){ Serial.println("Menu"); Serial.println("--------------------"); Serial.println("1. Motion Readout"); Serial.println("2. Distance Readout"); Serial.println("3. SD Directory Listing"); Serial.println("4. Sound Test"); Serial.println("5. Car Test"); Serial.println("--------------------"); Serial.println("Type the number and press enter");
while(char ch = 0){
ch = Serial.read();
}
char ch;
switch(ch)
{
case '1':
motion();
}
ch = 0;
}
//menu over, lets get to work.
void motion(){ Serial.println("Haha, it works!");
}
I'm pretty sure a While loop is the right thing to do, but I'm probably implementing it wrong. Can anyone shed some light on this?
WizenedEE:
Use Serial.available() for the tester of the while loop
while(Serial.available() <= 0){}
Okay, I did that. I also made the variable "ch" a global variable so I would have to keep re-defining it. The loop works now, but it doesn't go back into the while loop once it gets out. It just keeps looping void loop. I tried adding a Serial.flush(), but it doesn't seem to have any effect.
#include <Servo.h>
Servo steering;
Servo throttle;
int pos = 0;
int val = 0;
char ch;
void setup(){ Serial.begin(9600);
throttle.write(90);
steering.write(90);
pinMode(A0, INPUT);
pinMode(7, INPUT);
}
void loop(){ Serial.println("Menu"); Serial.println("--------------------"); Serial.println("1. Motion Readout"); Serial.println("2. Distance Readout"); Serial.println("3. SD Directory Listing"); Serial.println("4. Sound Test"); Serial.println("5. Car Test"); Serial.println("--------------------"); Serial.println("Type the number and press enter");
while(Serial.available() <= 0){
ch = Serial.read();
} Serial.println(ch);
switch(ch)
{
case '1':
motion();
}
ch = 0; Serial.flush();
}
//menu over, lets get to work.
void motion(){ Serial.println("Haha, it works!");
}
[quote author=James C4S link=topic=108803.msg817129#msg817129 date=1338953654]
Do yourself a favor and change all of your serial prints to use PROGMEM. Otherwise you are going to be debugging out of RAM issues in the near future.[/quote] I did that and it didn't do anything after the menu was printed. I changed it back.
The while loop should have nothing in the block. That way, you are telling it to sit still until you get something in the buffer, you then read it, parse it and loop.
Also, remove the Serial.flush(), it doesn't do what you think it does.
Arrch:
The while loop should have nothing in the block. That way, you are telling it to sit still until you get something in the buffer, you then read it, parse it and loop.
Also, remove the Serial.flush(), it doesn't do what you think it does.
Haha! Perfect! Now to fill in all the functions! Thanks! XD