Canada
Offline
Full Member
Karma: 0
Posts: 138
Power Level is futile (if < 9 000)
|
 |
« on: October 11, 2010, 11:05:47 pm » |
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 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"); } } }
|
|
|
|
« Last Edit: October 11, 2010, 11:07:10 pm by kraig »
|
Logged
|
|
|
|
|
Central Europe
Offline
Edison Member
Karma: 5
Posts: 1220
Use the Source, Luke.
|
 |
« Reply #1 on: October 12, 2010, 02:19:19 am » |
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
|
|
|
|
|
Logged
|
|
|
|
|
Central Europe
Offline
Edison Member
Karma: 5
Posts: 1220
Use the Source, Luke.
|
 |
« Reply #2 on: October 12, 2010, 02:33:06 am » |
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: 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
|
|
|
|
« Last Edit: October 12, 2010, 02:35:07 am by Korman »
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 316
Posts: 35535
Seattle, WA USA
|
 |
« Reply #3 on: October 12, 2010, 04:25:10 am » |
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: 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/HomePageand learn exactly what bytes and ints are and can hold.
|
|
|
|
|
Logged
|
|
|
|
|
Canada
Offline
Full Member
Karma: 0
Posts: 138
Power Level is futile (if < 9 000)
|
 |
« Reply #4 on: October 12, 2010, 11:16:04 am » |
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.
|
|
|
|
« Last Edit: October 12, 2010, 11:56:09 am by kraig »
|
Logged
|
|
|
|
|
Central Europe
Offline
Edison Member
Karma: 5
Posts: 1220
Use the Source, Luke.
|
 |
« Reply #5 on: October 12, 2010, 04:14:15 pm » |
Sorry for the sarcasm, but sometimes it's just the small joys of life that makes one go on and reply. It's nothing personal, it was just a too nice opportunity to let pass up unused. If the stuff above wasn't detailed enough, feel free to ask again - for more abuse and solutions.
Korman
|
|
|
|
|
Logged
|
|
|
|
|
Canada
Offline
Full Member
Karma: 0
Posts: 138
Power Level is futile (if < 9 000)
|
 |
« Reply #6 on: October 12, 2010, 05:12:46 pm » |
well I did made some progress. Same problem, but was able to finally get a string.
|
|
|
|
|
Logged
|
|
|
|
|
Central Europe
Offline
Edison Member
Karma: 5
Posts: 1220
Use the Source, Luke.
|
 |
« Reply #7 on: October 12, 2010, 05:53:40 pm » |
Here's something that could work - I'm just to lazy to test it out. It considers the first non-digit character to be the end of the number. void loop() { static int readval = 0; static int readstatus = 0;
if (Serial.available() > 0) { char c = Serial.read(); if (isdigit (c)) { // We have a digit! readval = readval * 10 + (c - '0'); // Remember that we have at least one good digit. readstatus = 1; } else if (readstatus) { // We got something else. Reading the number is over.
// Process it if ([glow]readval > bla bla bla[/glow]) { [glow]....[/glow] }
// Reset read status and value for next number readstatus = 0; readval = 0; } } }
Korman
|
|
|
|
« Last Edit: October 12, 2010, 11:18:18 pm by Korman »
|
Logged
|
|
|
|
|
Canada
Offline
Full Member
Karma: 0
Posts: 138
Power Level is futile (if < 9 000)
|
 |
« Reply #8 on: October 12, 2010, 06:09:40 pm » |
:-/ Thats exactly what I was looking for since yesterday. I tried some methods with do/while and if, but I always ended up with a little bit of complication.
I see you edited your post.
|
|
|
|
« Last Edit: October 12, 2010, 06:10:08 pm by kraig »
|
Logged
|
|
|
|
|
Central Europe
Offline
Edison Member
Karma: 5
Posts: 1220
Use the Source, Luke.
|
 |
« Reply #9 on: October 12, 2010, 06:14:28 pm » |
Just make sure you copy the current version, I fixed a few little bugs.
Korman
|
|
|
|
« Last Edit: October 12, 2010, 06:14:38 pm by Korman »
|
Logged
|
|
|
|
|
Canada
Offline
Full Member
Karma: 0
Posts: 138
Power Level is futile (if < 9 000)
|
 |
« Reply #10 on: October 12, 2010, 06:32:46 pm » |
Thanks for you help.
But I feel uncomfortable saying that I'm still getting "echo" in the Serial. (entry: 45 Print: 1- 4 2- 45)
|
|
|
|
|
Logged
|
|
|
|
|
Central Europe
Offline
Edison Member
Karma: 5
Posts: 1220
Use the Source, Luke.
|
 |
« Reply #11 on: October 12, 2010, 11:16:26 pm » |
I'm a bit surprised, because if you do your processing only in the place where I put the "bla bla bla" and the ... (I went back and highlighted the spot in the sample), the code shouldn't be reached except after a non-digit character. If you put it somewhere else, it'll get executed more often.
Korman
|
|
|
|
« Last Edit: October 12, 2010, 11:19:17 pm by Korman »
|
Logged
|
|
|
|
|
Canada
Offline
Full Member
Karma: 0
Posts: 138
Power Level is futile (if < 9 000)
|
 |
« Reply #12 on: October 13, 2010, 10:46:54 am » |
Alright thanks, I own you one!  So far my code was like that: int buzzer = 11; //the pin with the piezo buzzer int v = 0; char p, *l; String number; int x = 0;
void setup() { Serial.begin(9600); Serial.println("Frequency: "); }
void frequencyPick() { if (Serial.available() > 0 && x < 3) // data available? { p = Serial.read(); *l = p; v = atoi(l); number= number + v; Serial.println(number); x = x + 1; } }
void loop() {
frequencyPick(); number = 0; x = 0; }
With little bugs as you can see. (Infinite loop, infinite resetting number value, etc.) But as I said, that wasnt the most important thing that annoyed me. Thank you for your time and patience. edit: I did some research and I understand your code, again, THANKS.
|
|
|
|
« Last Edit: October 13, 2010, 11:36:52 am by kraig »
|
Logged
|
|
|
|
|
|