Getting an integer of the serial?

Hi All,

Im just starting to use Arduino, and so far, so good. However, I do run into an issue with the serial communiation. I thought it would be cool to have a visual VU meter of my CPU usage with arduino. The problem I'm having with this, is that I cant seem to manage to get a value into the Arduino.

I can send anything between 0-9 from the command line by sending either and check for the correct . But when I try to send more then 1 digit, I run into problems. I' tried so far looping trough the serial input, and converting the input to int, but that doesnt seem to work as expected...

My bes try so far is below, but again, this is not really working, some values are simply skewed.

So, what I'd like to know, is there not an easier way to get a two or three digit INT over the serial line to the Arduino, in such a way that the arduino actually sees the correct value, and as an INT?

Any tips or ideas would be more then welcome!

 void loop() {
  if (Serial.available() == 2) {
    // read the incoming byte:
     one = Serial.read();
     two = Serial.read();
  } 
  total = ( one - 48) *10 + (two - 48);
  if ( total  > 0 ) {
    Serial.println(total);
  }
}

The serial read only works in bytes and an integer requires two bytes.

You can split an integer into two

msb = int1 / 256;
lsb = int1 % 256;

and then send them as two seperate bytes and reassemble them at the other end.

Alternatively you can adjust your data so it will fit into a byte and send it in one. It looks like you;re using char instead of byte, char is an ascii variant of byte. Byte can hold a value between 0 and 255.

Im sure if im getting this…

I have a value between 0 and 100, so thats fits in one byte, right?

If I use the following code:

 const int ledPin = 11;      
 void setup()
 {
   Serial.begin(9600);
   pinMode(ledPin, OUTPUT);
 }

 void loop() {
   byte brightness;
   if (Serial.available()) {
     brightness = Serial.read();
     analogWrite(ledPin, brightness);
     Serial.print(brightness);
   }
 }

And then run this from a terminal
echo -n -e 255 > /dev/ttyUSB0
then I do see the “255” in the serial monitor, but the LED is not set to max. If I send a zero, I see the zero in the serial monitor, but LED is not switched off…

However, if I user Processing to send values, I do get the LED dimmed correctly…

import processing.serial.*;
Serial port;

void setup() {
  port = new Serial(this, "/dev/ttyUSB0", 9600);
}
  
void draw() {
  for (int i = 0; i < 256; i++) {
  port.write(i);
  }
  for (int i = 255; i > 0; i--) {
  port.write(i);
  }
}}

So, Im really wondering wether Processing does something with the serial data, since it is working as expected there?

Note: ran the following to setup tty stty -F /dev/ttyUSB0 cs8 9600 ignbrk -brkint -icrnl -imaxbel -opost -onlcr -isig -icanon -iexten -echo -echoe -echok -echoctl -echoke noflsh -ixon -crtscts

I think that the characters coming in the serial port are just that.... characters.

Try converting the string "255" to the integer 255 using the atoi() function.

e.g. int TestVar = atoi(CharacterStringFromSerial)

using atoi() on the bytes/chars that come from Serial.read() results in conversion errors..

using atoi() on the bytes/chars that come from Serial.read() results in conversion errors..

Not in my experience - can you post examples please? Are you sure the characters you're feeding to atoi are ASCII digits, and that the string is correctly terminated?

To be honest, I dont know wether im sending ASCII chars or not.. I just do an echo.. In previous tests with just 0/1 I did get it working by sending a 48 or 49 via the same procedure. I could do the same by breaking op the INT in seperate digits...

I just cant believe how hard this is... Iwould expect it to be fairly straight forward to send the value "81" and have the arduino pick it up as the INT 81?

This is what my latest "version"... seems to work, for the fist digit I send

int ledPin = 11;
char* val = "0";

void setup()
{
 // begin the serial communication
 Serial.begin(9600);
 pinMode(ledPin, OUTPUT);
}

void loop()
{
 // check if data has been sent from the computer
 if (Serial.available()) {
  // read the most recent byte (which will be from 0 to 255)
   val[0] = Serial.read();
   
   Serial.println(val);
   
   int a = atoi(val);
   Serial.println(a);

// analogWrite(ledPin, var);
   Serial.flush();
 }
}
I just cant believe how hard this is

Well, you’re not making it easy for yourself:

 Serial.flush();

int a = atoi(val);

val hasn’t been terminated
(uncompiled, untested)

const int ledPin = 11;
char val [10];
int index;

void setup()
{
 // begin the serial communication
 Serial.begin(9600);
 pinMode(ledPin, OUTPUT);
  index = 0;
}

void loop()
{
 // check if data has been sent from the computer
 if (Serial.available()) {
  // read the most recent byte (which will be from 0 to 255)
   byte ch = Serial.read ();
   val[index++] = ch;
   val [index] = '\0';

   Serial.println(val);

   int a = atoi(val);
   Serial.println(a);

  if (ch <= '0' || ch >= '9') {
    index = 0;
  }

// analogWrite(ledPin, var);
 }
}