Go Down

Topic: Wait on Serial.read(); (Read 73 times) previous topic - next topic

Digitalis

Jun 29, 2015, 10:13 pm Last Edit: Jun 29, 2015, 10:16 pm by Digitalis
Hi! I'm kind of new, but I've been playing with arduino for a few months now. So here's my problem:
I am attempting to make a kind of "program loader" type sketch that will read data from EEPROM or an SD shield and execute it, as a way of extending sketch size. Of course I don't plan on directly writing arduino inside of arduino, just a custom opcode approach.

Enough about the project, though; here's the thing I need advice with. Becuase I'm trying to make it somewhat user-friendly and executed via the serial monitor, I'm trying to have a Serial.read() system that waits for a user response. The problem, of course, is that I don't know of any way of making the system wait until an input is given, so I'm having to do something like this:

------------------------------------
label:

Serial.println("prompt 0 or 1");
Serial.println("0 | option a");
Serial.println("1 | option b");
int answer = Serial.read();

//Notice I have "48" and "148." The reasoning behind this is that at the start of a Serial.read() command
//it will put a "1" bundled with the ASCII character sent.

if(answer = 48 || answer == 148){
// do stuff
goto labeltwo;
}else if(answer == 49 || answer == 149) {
// do stuff
goto labeltwo;
}

goto label;

labeltwo:
---------------------------------
Any ideas on how I could compact the code further or maybe even a "wait for response" function?
Thanks in advance.

septillion

Goto's will just make a mess of the code. Just
while(!Serial.available() ){
}

Digitalis

So something like:
-----------------------------------------------
Serial.print("a or b")
Serial.print("0| a")
Serial.print("1| b")
label:
while(!Serial.available()){}
int read = Serial.read();
if(read == 48 || read == 148){

}else if(read == 49 || read == 149){

}else{
goto label;
}
--------------------------------------------------
?

septillion

Almost, just drop the labels and make it a function. And just re-enter the function if the user input isn't what you want.




Digitalis

I also just learned about ParseInt, so I can reduce redundancy! :D
so:

--------------------------------------

int read;

void setup(){
Serial.begin(9600);

int out = prompt(
"A or B",
"0 | A",
"1 | B",
0,
1,
0,
1
)

}

void loop(){

}

void prompt(String m1,String m2,String m3,int i1,int i2,int o1,int o2)
 while(!Serial){}
 while(!Serial.available){}
 Serial.println(m1)
 Serial.println(m2)
 Serial.println(m3)
 int read = Serial.parseInt();
 if(read == i1) {
  return o1
 }else if(read == i2) {
  return o2
 }else{
  prompt(m1,m2,m3,i1,i2,o1,o2)
 }
}

Digitalis

I'm having odd "Return" problems. Here's the code:

int read;

void setup(){
Serial.begin(9600);
prompt("A or B", "0 | A", "1 | B", 0, 1, 0, 1);
}

void loop(){

}

void prompt(String m1,String m2,String m3,int i1,int i2,int o1,int o2){
  int a = o1;
  int b = o2;
  while(!Serial){}
  Serial.println(m1);
  Serial.println(m2);
  Serial.println(m3);
  while(!Serial.available()){}
  int input = Serial.parseInt();
  if(input == i1){
    return a;
  }else if(input == i2){
    return b;
  }
}

For some reason it's giving an error on return saying:

Arduino: 1.6.5 (Mac OS X), Board: "Arduino Uno"

testytest.ino: In function 'void prompt(String, String, String, int, int, int, int)':
testytest:22: error: return-statement with a value, in function returning 'void' [-fpermissive]
testytest:24: error: return-statement with a value, in function returning 'void' [-fpermissive]
return-statement with a value, in function returning 'void' [-fpermissive]

Robin2

There is a simple user input function in planning and implementing a program and you may find the examples in serial input basics useful.

...R

Go Up