Using serial communication in Arduino Uno

Thread Starter

pujulde

Joined Jul 24, 2013
111
Having established communication between PC and Arduino board, i send data from PC and then back to the Serial monitor from board. Using function Serial.read() for reading incoming data we got data in ASCII. So to read and then ECHO entering information on serial monitor I use function Serial.parseINT(), everything work, but the time information appear on serial monitor is late, what is the reason can anybody explain? What process is happening?
 

Thread Starter

pujulde

Joined Jul 24, 2013
111
void setup()
{
Serial.begin(9600);
}
void loop()
{
int data;
if(Serial.available() > 0)
{
data = Serial.parseInt();
Serial.println(data);
}
}
I mean that the information i want to display on serial monitor is late a little, it happens when i use Serial.parseInt(), but Serial.read() displays in no time. May be function Serial.purseInt() needs more executing time
 

sirch2

Joined Jan 21, 2013
1,037
Unless you are running your processor at a very low clock rate I'd be surprised if parsing an int caused a noticeable delay. The docs say that

Serial.parseInt() is terminated by the first character that is not a digit
So maybe it is waiting for a non-digit
 

Thread Starter

pujulde

Joined Jul 24, 2013
111
I want to turn on and off led on Arduino board by transferring from serial monitor numbers 1 and 0 respectively. The code is working, but when i transfer letters from keyboard it reads them as zero and turn off led. Can anyone suggest me the issue? How to make Arduino not to take into account the incoming letters. To read incoming data from serial monitor I use function Serial.parseInt(), because function Serial.read() represents data in ASCII . Thanks in advance
 

sirch2

Joined Jan 21, 2013
1,037
Serial.read() reads a byte it's up to you how you treat it. I think something like this should work:

Rich (BB code):
int value = Serial.read();
if(value == 1) {
   ...
}
 

Thread Starter

pujulde

Joined Jul 24, 2013
111
Thats a code
int led = 8;
int num = 0;
void setup()
{
pinMode(led, OUTPUT);
Serial.begin(9600);
}

void loop()
{
if (Serial.available() > 0)
{
num = Serial.parseInt();
if(num == 1)
{
digitalWrite(led, HIGH);
Serial.println("led is on");
}
else if(num == 0)
{
Serial.println("led is off");
digitalWrite(led, LOW);
}
}}
 

adam555

Joined Aug 17, 2013
858
It could be that parseInt() works on a 1 second interval. You can change that with Serial.setTimeout(). Go for a something like 100ms or 10ms, and see what happens.

Why do you need parseInt() instead of read()?
 
Last edited:

panic mode

Joined Oct 10, 2011
2,703
do you understand hexadecimal and know what ASCII code is?
Arduino receives what you send but... "1" can be different things.
For example normally that would be either:
a) ASCII "1" which is hexadecimal value 0x31 (or decimal value 49).
b) numeric value 1 which is hexadecimal value 0x01 (or decimal value 1)

your IF statement is evaluating second case so try changing it to
if (num == 49)


...or, don't bother with int conversion and just use ASCI commands (something unambiguous, such as "A" for on and "B" for off).
 
Last edited:

Thread Starter

pujulde

Joined Jul 24, 2013
111
You are right, i knew that to every character there are respective number in ASCII, but i wanted to write application which would work with 1 and 0 and simultaneously show the numbers 1 and 0 on serial monitor, so i couldn't do it. If do as you said we get 49 and 48 respectively on serial monitor. But writing this reply i come to some conclusion, I can use Serial.println(" pushed 1 ") and Serial.println("pushed 0") when getting 1 and 0. Thank you, now i know how to do it.
 

Austin Clark

Joined Dec 28, 2011
412
num = Serial.read()

if (char(num) == "1"){//Turn ON LED}

else if(char(num) == "0"){//Turn OFF LED}

else {//Do nothing, or blink LED, or whatever you want to happen when anything other than a 1 or a 0 is recieved.}

Does exactly what you want.
 
Top