Tell me more ×
Electrical Engineering Stack Exchange is a question and answer site for electronics and electrical engineering professionals, students, and enthusiasts. It's 100% free, no registration required.

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

share|improve this question
1  
First, out here in the real world "shield" and "sketch" are normal words that don't make sense as you used them. No, the arduino marketing folks don't get to redefine standard english words and have the rest of us just go along. Second, this is a software issue. Surely there is a blocking call available in the libraries to get the next byte from the UART. – Olin Lathrop Jun 6 '12 at 11:27
3  
@OlinLathrop, Lets tag it arduino and accept this is the language they use inside their community, feel free to share that it is not common language anywhere else but it is okay if they choose to use that term. – Kortuk Jun 6 '12 at 13:27
@Kortuk: No, I don't go along with that. I realize those are the terms used in the arduino community, but they have specific and different meanings everywhere else. This is a general electronics forum, not a arduino forum. If arduino people come here, they have to be prepared to talk general electronics correctly. I also deeply resent how the arduino people have tried to hijack common english words for their private use. Every effort should be made not to tolerate this in the larger world. At the very least, we can edjucate people that come here using the wrong terms. – Olin Lathrop Jun 6 '12 at 14:33
4  
@OlinLathrop, I tried to say this before but I may have failed to communicate. Explaining the further definitions of this word is very useful, especially as the user branch out. The tags are a way to almost create coexisting subsites, if you are talking in questions that have the arduino tag you can expect shield to be used in this way, attempting to force the community to use a different word everywhere will make the questions significantly less useful to the arduino community on the whole. Educate but try to accept that a different term will be used in their questions. – Kortuk Jun 6 '12 at 14:44
2  
@OlinLathrop How fortunate that the rest of the world takes care never to repurpose or overload terms. Like current, or code, or wire, or power. – Nick Johnson Jun 10 '12 at 9:34
show 1 more comment

migrated from programmers.stackexchange.com Jun 6 '12 at 11:19

2 Answers

Here are some suggestions:

  • move declaration of ch above the while loop
  • you used = instead of ==, so while( ch = 0) will loop forever, because ch = 0 will always evaluate to true. It should be while( ch == 0)
  • refactor your menu display into a DisplayMenu() function, just because it's cleaner that way, then call DisplayMenu() from loop().
share|improve this answer
Also don't change the value of character you read. Do something like "while(repeat)" and change the value of repeat to false when you want to stop it. Don't forget to initialize "repeat=true" – Jamiro14 Jun 6 '12 at 13:45
I don't agree with this, since it is perfectly valid and standard practice to evaluate an input character in a while loop. He merely had an equality syntax error and used = instead of ==. But inside of a loop, I would agree with what you said about modifying the value of the input character, since that obviously defeats the whole point of capturing the keyboard input. – Dave Jun 6 '12 at 14:16
I'm not saying that he shouldn't evaluate an input character in a while loop, just saying that he shouldn't change its value inside the loop. – Jamiro14 Jun 6 '12 at 14:25
sorry, I thought you were referring to how he changed the value inside of the evaluation. ;) – Dave Jun 6 '12 at 14:26

if you would hang up your program until there is a serial command:

use this by example

Serial.flush() //flush all previous received and transmitted data
while(!Serial.available()) ; // hang program until a byte is received notice the ; after the while()
/*continue program ...*/

and maybe you can write a function

void displayMenu(){
...
}

and call that whenever you want

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.