Reading data from Arduino not work

Discussion in 'UDOO NEO' started by Andres Rodriguez, Jan 4, 2016.

  1. Andres Rodriguez

    Andres Rodriguez New Member

    Joined:
    Dec 28, 2015
    Messages:
    5
    Likes Received:
    2
    I've been searching in the forum but I do not find something to help me, I'm trying to read data from Arduino with Serial.read(), but has not worked for me.

    It is a simple sketch:
    Code:
    int ledPin = 9;
    
    void setup() {
      pinMode(ledPin, OUTPUT);
      Serial.begin(115200);
    }
    
    void loop() {
      if (Serial.available() > 0) {
        int data = Serial.read();
        if(data == 0)
          digitalWrite(ledPin, LOW);
        else
          digitalWrite(ledPin, HIGH);
      }
    }
    to send data to Arduino I do using
    but it does not work and console freezes. I could read data from Arduino and if it works.
    Thanks for any help.
     
  2. klakier2121

    klakier2121 Member

    Joined:
    Dec 5, 2015
    Messages:
    48
    Likes Received:
    15
    If you will write 0 in console
    Code:
    int data = Serial.read();
    it will write 48 because 0 in ASCII is 48(int), so you have to subtract this 48, so code is:
    Code:
    int data = Serial.read() - '0';
    Command I always use is
    Code:
    minicom -D /dev/ttyMCC
     
  3. Andres Rodriguez

    Andres Rodriguez New Member

    Joined:
    Dec 28, 2015
    Messages:
    5
    Likes Received:
    2
    Thanks @klakier2121 that was my mistake, for this test use Serial.parseInt() to get the digit, on the other hand, I did not know minicom -D /dev/ttyMCC although at times it is frozen and I have to turn off and on again.
     

Share This Page