storing on a  integer variable from serial read.

i am trying to store numbers like 1,2,3, on integer variables (using serial communication on serial monitor).

The problem is that when i print (serial.println) these variables it shows 49,50,51 and not 1,2,3.

the strange thing is that if i make the varialbe byte (and not int) it works!

any help?

The really strange thing is that this comes up about once a week, and no-one ever does a forum search before posting.

ASCII decimal digits: 0x30 = '0' , 0x31 = '1'...0x39 = '9'

And how do i convert it?

int val = Serial.read() - '0'; will convert any character that represents a digit to the number it represents.

will convert any character that represents a decimal digit

Thanks pauls, very helpfull

does it matter that i have many questions?

will convert any character that represents a decimal digit

What other kinds of digits are there?

does it matter that i have many questions?

No. If it bothered us, we wouldn't be reading them or answering them.

What other kinds of digits are there?

Hex - A to F. ;)

Hex - A to F.

Oh, yeah. But, they presuppose that the data was sent as based 16 strings, which is kind of unusual.

int nextStep; int previousStep = 0; int position; int search; int rpm100; int rpm10; int rpm1; int rpm;

void setup() { Serial.begin(9600); pinMode(7,OUTPUT); stepper.setSpeed(300);

}

void loop() {

search = Serial.read(); if (search == 'A') {

rpm100 = Serial.read()- '0'; rpm10 = Serial.read()- '0'; rpm1 = Serial.read()- '0';

rpm = (100*rpm100) + (10*rpm10) + rpm1;

i am not getting the number

i am sending in a demical/binary/hexademical format from X-sim profiller

the X-sim sends

"A" and a number from 127 to 255

but i am not getting the number

search = Serial.read(); if (search == 'A') {

rpm100 = Serial.read()- '0'; rpm10 = Serial.read()- '0'; rpm1 = Serial.read()- '0';

rpm = (100*rpm100) + (10*rpm10) + rpm1;

Serial.write(rpm);

i am typing A200 on the serial monitor and i am not getting "Á" back

Try using Serial.print(rpm), and see what you get. Yes, it will convert the value to a string, but that will tell you whether the value is good.

search = Serial.read(); if (search == 'A') {

rpm100 = Serial.read()- '0'; rpm10 = Serial.read()- '0'; rpm1 = Serial.read()- '0';

rpm = (100*rpm100) + (10*rpm10) + rpm1;

Serial.print(rpm);

A000 gives -5439 A001 gives -5439 A220 gives -5439 A255 gives -5439 A2200 gives -5439

A gives -5439 A22 gives -5439

200 gives nothing

WHY?

also when i connect the io bridge Serial LCD to the RX

and send data from the X-Sim i am reading

A125A125A125A125A125

when i connect it to the TX pin so i read the Serial.print(rpm)

i am getting only once -5439

Are you initializing rpm100, rpm10, and rpm1 before storing the Serial.read data into them? Are you waiting for 4 characters to arrive before reading anything? The Serial.read function will return -1 if there is nothing to read. -1 - 48 = -49. -4900 - -490 - -49 = -5439.

Use Serial.available() to ensure that there is data to read, or test that the value read from the serial port is positive before using it.

search = Serial.read(); if (search == 'A') {

rpm100 = Serial.read()- '0'; rpm10 = Serial.read()- '0'; rpm1 = Serial.read()- '0';

Serial.print(rpm100); Serial.print(rpm10); Serial.print(rpm1);

rpm = (100*rpm100) + (10*rpm10) + rpm1;

Serial.print(rpm);

[ch913]220 gives -49-49-49-5439 A000 gives -49-49-49-5439

i just see the second page

#include <Stepper.h>

Stepper stepper(48, 12, 11, 10, 9);

int nextStep;
int previousStep = 0;
int position;
int search;
int rpm100;
int rpm10;
int rpm1;
int rpm;

void setup()
{
Serial.begin(9600);
pinMode(7,OUTPUT);
stepper.setSpeed(300);

}

void loop()
{

search = Serial.read();
if (search == ‘A’) {

rpm100 = Serial.read()- ‘0’;
rpm10 = Serial.read()- ‘0’;
rpm1 = Serial.read()- ‘0’;

Serial.print(rpm100);
Serial.print(rpm10);
Serial.print(rpm1);

rpm = (100rpm100) + (10rpm10) + rpm1;

Serial.print(rpm);

position = map(rpm,127,255,40,0);
if (position >= 39) digitalWrite(7, HIGH);
nextStep = position - previousStep;
stepper.step(nextStep);
previousStep = position;
if (position < 39) digitalWrite(7, LOW);
}

}

what do you mean to intiallise

sry for my english

the X-sim

send with a 33ms delay (i can change that) the “A” and three numbers all the time

thanks

If you are always sending 4 characters, you need to wait until there are 4 characters to read:

void loop()
{
   if(Serial.available() >= 4)
   {
      // Now, we can read the data
   }
}

If you are not always sending 4 characters, you need to send some start and end of packet markers:

Then, you need to look for these markers.
http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1273230655/2#2
(Read the responses that follow too. I had an error in the code).

i didnt get it

i am always send one character and 3 numbers

those i post above were just tests

where is my mistake

the rpm100 rpm10 and rpm1 should be char and not int ?