Serial.read into integer - syntax migration probs

Hello!

this is my first code. I know a little of other languages like mel and AS2, but here, there are some tricks I gotta get used to.

The simple code:
the idea is to send a number(id) via serial, and arduino will recognize it as an integer and work around this number.

By far I have this

int id = 0;

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

void loop(){
  
   id = Serial.read() - '0';
    
   for(int i=0; i<id ; i++){
     Serial.print(i);
     Serial.print(", id: ");
     Serial.println(id,DEC);  
   }

}

which prints, if I send 8:
0, id: 8
1, id: 8
2, id: 8
3, id: 8
4, id: 8
5, id: 8
6, id: 8
7, id: 8

I can, then, use as an integer and its ok.

But when a send a number like greater than 1 digit(12, 145…), it messes everything of course.

In short, I’d like to know how to convert the data incoming via serial into a integer. If I send (12, it will be “12”, not “1” + “2”).

there are other issues that I thought would be easier to solve, but syntax won’t let me move further. Lots of old examples including libraries mostly out of date, and I get a little lost…

Thanks in advance!
Btp

First off you can't just do a serial read, you have to wait until there is something in the buffer to read. Then you read the ASCII bytes one at a time to make it up into your number. Something tells you to stop, like a non ASCII character. There is not much that is out of date regarding syntax. Look at :- http://www.arduino.cc/playground/Main/InterfacingWithSoftware For examples of how to do this sort of thing. look at the Processing entry.

Hi! thanks for the fast reply.

I have checked this page once, but I will read it again. Actually, I was sending data from the "serial monitor" itself while testing.

I understand the buffering thing, but since I was testing on the serial monitor I thought it wasnt need.

Else, your comments means I have to treat each number at time? for example, if it incomes "12", I have to treat 1 as 10 and sum with 2? I thought that maybe there was a way, or function, or library which converts it =/

There is a function that will take a string of characters and convert it to an integer - atoi. But, you don't have a string of characters.

For dealing with ints, multiplying the previous value by 10 and adding the new value is the easiest way to go.

If you never need to deal with values > 255 then changing i to type byte will send the raw value instead of the ASCII equivalent…

for(byte i=0; i<id ; i++){
     Serial.print(i);
   }

If you do need values > 255 then you’ll have to send it in pieces like this…

for(int i=0; i<id ; i++){
     Serial.print(lowByte(i));
     Serial.print(highByte(i));
   }

and then reassemble it at the receiving end.


Re-reading now, I see that what you’re actually after is how to receive 2-digit numbers and loop that many times. In that case you’ll need to wait on Serial.available() until you can Serial.read() twice. Re-assemble each received byte into Integer i.

I’m using the messenger library and its working for integers now. But when parsing a string… no good. I’d like to share what I have tried so far.

At now, I am using my own code in arduino (not firmata as before) + serproxy + Beltran Berrocal’s arduino.as class (which is in as2) to get flash and arduino communication.

//INTEGER…(this is working, thanks a lot Kasper Kamperman and people here for that!!)

my arduino runs this

#include <Messenger.h>
int id = 0;  <--- default
Messenger message = Messenger();

void messageReady() {
  while ( message.available() ) {
    id = message.readInt();
  }
}

void setup() {
  Serial.begin(9600);
  message.attach(messageReady);
}

void loop() {
   while ( Serial.available() ) message.process(Serial.read());
  Serial.println(id);
}

now flash can send any 2, 3… digits int ID (a.send(“456”+chr(13))) it will replace default 0.
This is working fine. In flash it outputs “456” as I needed.

//CHARACTERS…(NEED HELP HERE) :wink:

Now, for the chars… I am trying to use the message.readChar() (messenger library), but not succefully…
Here are a few things I’ve tried… I’ve read a lot but I’ve not much experience:

char str[10];  //the string "works out" will come from flash --> a.send("works out" + chr(13));

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

void loop() {
  delay(1000);
  int i = 0; 
  while ( Serial.available() ){     
    str[i] = Serial.read();
    i++;
  }
 
  Serial.println(str);  //<--- WEIRD!  if I comment this line, nothing prints. So its reading itself?.
}

The code above produces the following strange output in flash, when interval = 500 ms (half what arduino is working).
This is the closer I’ve got to get a whole string response from arduino.

** Arduino ** Connecting to 127.0.0.1:5332 . . .
** Arduino ** Connection established.
Arduino says :
Arduino says : [ch833]out


Arduino says :  out


Arduino says : works out

$oun

Arduino says : works out

'ouh

Now, this other code uses the Messenger library, but I cant make it work. Its very funny why it does print more than two characters…
I am leaving some commented lines which I alternatively tried.

#include <Messenger.h>

char str[100]; //the string "works out" will come from flash --> a.send("works out" + chr(13);

Messenger message = Messenger();

void messageReady(){
  //Serial.print("return: ");
  //int i = 0;
  while ( message.available() ){
     //str[i] = message.readChar();
     //i++;
     Serial.print(message.readChar()); //<--- it is receiving, or storying only 2 characters as showing in the flash output
  }
  Serial.print(";");
  //Serial.print(13,BYTE);
}

void setup() {
  Serial.begin(9600);
  message.attach(messageReady);
}

void loop() {
  delay(1000); 
  while ( Serial.available() ) message.process(Serial.read());   
  Serial.println(str);  //<--- if I comment this line, nothing prints. So its reading itself?.
}

The code above produces the following strange output in flash, when interval = 500 ms (half what arduino is working).

** Arduino ** Connecting to 127.0.0.1:5332 . . .
** Arduino ** Connection established.
Arduino says : [ch763]wo;

Arduino says : wo;wo;wo;

Arduino says : wo;wo;

which is still buggy, but more stable than the code before where no messenger methods were used.

I’ll have some lunch. I’m trying to solve this issue psychopathly, hehe.
Thanks so much for the attention and directions

Btp~

void loop() {
  delay(1000);
  int i = 0;
  while ( Serial.available() ){    
    str[i] = Serial.read();
    i++;
  }

  Serial.println(str);  //<--- WEIRD!  if I comment this line, nothing prints. So its reading itself?.
}

One thing I’d say here is, unless a null character is being transmitted and received, you’re not terminating the string “str”.
So, as soon as you don’t have a character available, you’ll print whatever happens to already have been in “str”.

Thanks Awol!

You are right. As long as I have tested, it is also necessary to call messageReady(). I think I know arduino is receiving it, or the refered function wouldnt be called. Flash is sending the carriage return "chr(13)" in the end of the string...

Still, I couldnt see why arduino is not firstly recognizing the inputs in serial incoming from flash but his own serial.print =/

Regards Btp~