Difficulty- Serial Read/Serial Print

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

}

}

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

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

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:

and learn exactly what bytes and ints are and can hold.

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.

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

well I did made some progress.
Same problem, but was able to finally get a string.

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

:-/ 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.

Just make sure you copy the current version, I fixed a few little bugs.

Korman

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)

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

Alright thanks,

I own you one! :smiley:

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.