Using serial communication in Arduino Uno

Discussion in 'Programmer's Corner' started by pujulde, Aug 29, 2013.

  1. pujulde

    Thread Starter Member

    Jul 24, 2013
    95
    1
    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?
     
  2. sirch2

    Well-Known Member

    Jan 21, 2013
    1,008
    351
    What do you mean by "late"? May be showing your full code would help
     
  3. pujulde

    Thread Starter Member

    Jul 24, 2013
    95
    1
    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
     
  4. sirch2

    Well-Known Member

    Jan 21, 2013
    1,008
    351
    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

    So maybe it is waiting for a non-digit
     
    pujulde likes this.
  5. pujulde

    Thread Starter Member

    Jul 24, 2013
    95
    1
    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
     
  6. sirch2

    Well-Known Member

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

    Code ( (Unknown Language)):
    1.  
    2. int value = Serial.read();
    3. if(value == 1) {
    4.    ...
    5. }
    6.  
     
  7. pujulde

    Thread Starter Member

    Jul 24, 2013
    95
    1
    Thats a code
     
  8. adam555

    Active Member

    Aug 17, 2013
    858
    39
    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: Sep 2, 2013
  9. pujulde

    Thread Starter Member

    Jul 24, 2013
    95
    1
    When I use Serial.read() and enter 1 it perceive it as ASCII and do nothing, so i use Serial.parseInt()
     
  10. pujulde

    Thread Starter Member

    Jul 24, 2013
    95
    1
    I have used Serial.setTimeout(10) and it works faster, thank
     
  11. adam555

    Active Member

    Aug 17, 2013
    858
    39
    You're welcome
     
  12. panic mode

    Senior Member

    Oct 10, 2011
    1,318
    304
    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: Sep 2, 2013
  13. pujulde

    Thread Starter Member

    Jul 24, 2013
    95
    1
    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.
     
  14. Austin Clark

    Member

    Dec 28, 2011
    409
    44
    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.
     
    absf likes this.
Loading...