Go Down

Topic: Difficulty- Serial Read/Serial Print (Read 1 time) previous topic - next topic

sxdemon

Oct 12, 2010, 06:05 am Last Edit: Oct 12, 2010, 06:07 am by kraig Reason: 1
Hello,
I'm trying to enter a number ranged between 250 and 523 in the Serial Monitor. Then, I want the Arduino to sent back the same number (as a confirmation). The only problem is that it see each numbers as separated entries.

So if I write down: 255 I will get three Serial Print saying : Unknown Entry. ( "2" "5" and "5")

Can you point me in the correct direction?
I have read from the documentation that the last character to be printed is transmitted over the serial port after Serial.print() has returned, but that didn't help  :-[

Thanks


Quote

int buzzer = 11; //the pin with the piezo buzzer
int v = 0;
void setup()
{
  Serial.begin(9600);
  Serial.println("Frequency: ");
  
}


void loop()
{
  if (Serial.available() > 0)
 
  {
  v = Serial.read();
  
  
  if (v <= 250 && v >=523)
  {
    Serial.print("Frequency Set to: ");
    Serial.println(v, BYTE);
  }
  else
  {
    Serial.println("Unknown Entry");
  }
  
  
  }
  
}
  


Korman

Very easy to explain. The program does what you told it to do, not what you imagined it will do. If you you align your wishes with your commands, your commands will make your wishes true. No more to say about that.

Korman

Korman

#2
Oct 12, 2010, 09:33 am Last Edit: Oct 12, 2010, 09:35 am by Korman Reason: 1
Oh, you wanted to know what you told the program to to?

Well, lets look at this part of the code, which creates the confusion for you:
Quote
Code: [Select]

 if (Serial.available() > 0)  {
 v = Serial.read();
   if (v <= 250 && v >=523) {
   Serial.print("Frequency Set to: ");
   Serial.println(v, BYTE);
 }
 else {
   Serial.println("Unknown Entry");
 }
}


In the first two lines you say: If there's data to read, read one character. It will then check if that character is lower than 250 and at the same time higher than 253. As this is a rather unlikely proposition, your program will tell you to get stuffed.

Now assume you fix your compares so that it checks for between 250 (character ü) and 253 (character ý), it still won't like your input. Why? Because you type a "2" (ascii 50) then a "5" (ascii 53) and then a "3" (ascii 51). All three characters aren't in the range 250-253.

Either you change the data you send to you program or you change your program to read an ascii string by collecting all characters to the end of the message, then convert the string to a number (for example with atoi) before doing your test.

This small exercise could come straight out of a textbook about programming. You'd probably find it as "My third program - handling input". I leave it to you to solve that, it's very educative.

Korman

PaulS

Quote
I leave it to you to solve that, it's very educative.

Spend 5 minutes googling the forum to find an answer.

Suppose that you did figure out how to collect the incoming string as a number, and that number was 475. Think about what these statements are going to do:
Code: [Select]
   Serial.print("Frequency Set to: ");
   Serial.println(v, BYTE);

The variable v is declared as an int, so it is perfectly capable of holding 475. But, you say that you want v printed as a byte (i.e. not converted to an ascii string representing the value). But, v is not a byte sized variable. You might want to experiment to see what happens when you try to print a non-byte sized variable as a byte.

Perhaps this means that you need to have a peak here:
http://arduino.cc/en/Reference/HomePage
and learn exactly what bytes and ints are and can hold.

sxdemon

#4
Oct 12, 2010, 06:16 pm Last Edit: Oct 12, 2010, 06:56 pm by kraig Reason: 1
Quote
Spend 5 minutes googling the forum to find an answer.

Thanks, but I've spent 2 hours (yesterday) 3-4 hours trying to find how to implement correctly atoi/string, thats why I came here for help.

Anyway, thanks for pointing out some of my errors.

Go Up