Go Down

Topic: [Resolved] How to have an Arduino wait until it receives data over serial? (Read 3196 times) previous topic - next topic

Sonicdh

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?

Quote

#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

Code: [Select]

while(Serial.available() <= 0){}

cmiyc

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.

Change this:
Code: [Select]
Serial.println("3. SD Directory Listing");

To this:
Code: [Select]
Serial.println(F("3. SD Directory Listing"));
Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

Sonicdh

#3
Jun 06, 2012, 05:59 am Last Edit: Jun 06, 2012, 06:36 am by Sonicdh Reason: 1

Use Serial.available() for the tester of the while loop
Code: [Select]

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.
Quote

#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!");
}



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.
I did that and it didn't do anything after the menu was printed. I changed it back.

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.

Sonicdh


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

Go Up