ARDUINO SERIAL READ.....

Discussion in 'Embedded Systems and Microcontrollers' started by AJIN NADH, Jan 12, 2015.

  1. AJIN NADH

    Thread Starter Member

    Dec 18, 2014
    79
    4
    HI Team,
    I tried to read a string send through the GPS device and on processing this String using the RX pin of ARDUINO UNO i tried to On/ off the led., But its Not working properly.
    I read the string through terminal, and its reading properly.What may be problem
    Regards,
    Ajin nadh
     
  2. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    Problems may lie in:
    1. Code
    2. Hardware
    For more specific answers, well need more information: schematic and code as a minimum.
     
  3. MikeML

    AAC Fanatic!

    Oct 2, 2009
    5,450
    1,066
    Use the Serial Monitor to display the characters actually being received by the Uno
     
    AJIN NADH likes this.
  4. AJIN NADH

    Thread Starter Member

    Dec 18, 2014
    79
    4
    Hi
    This is the code i used

    Code ( (Unknown Language)):
    1.  
    2. int RED =  13;
    3. int GREEN =  12;
    4. void setup() {
    5.   // Open serial communications and wait for port to open:
    6.   Serial.begin(19200);
    7.   pinMode(RED,OUTPUT);
    8.   pinMode(GREEN,OUTPUT);
    9. }
    10. void loop() {
    11.  
    12. String str = "";
    13.   if(Serial.available() > 0)
    14.   {
    15.     str = Serial.readString();
    16. if  (str.startsWith("Y", 5) )
    17.  
    18. {
    19.   digitalWrite(RED, HIGH);
    20.   digitalWrite(GREEN, LOW);
    21.   Serial.print(str);
    22. }
    23.   else if (str.startsWith("N", 5))
    24. {
    25.    digitalWrite(GREEN, HIGH);
    26.    digitalWrite(RED, LOW);
    27.    Serial.print(str);
    28. }
    29. }
    30. }
    31.  
    and the serial strings are "$W001N#" and $W001Y#
    ITS PLANED TO ON THE GREEN LED WHEN $W001N# RECEIVES.
    This code is getting when i receive using the Terminal. I think the code is not working, what may be the problem....

    Moderatots note : Please use code tags for pieces of code
     
    Last edited by a moderator: Jan 13, 2015
  5. AJIN NADH

    Thread Starter Member

    Dec 18, 2014
    79
    4
    AM GETTING THE STRING WHEN USE THE DISPLAY....
     
  6. MikeML

    AAC Fanatic!

    Oct 2, 2009
    5,450
    1,066
    I don't follow the code, but if I understand what you are doing, you have serial characters coming in at 19.2kbaud, and you are trying to display them on the serial monitor, which by default runs at 9.6kbaud. That will overrun, and you will garble or loose some of the characters...
     
  7. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    According to the documentation on the Arduino website, string.startsWith only accepts one parameter: a string.

    Provided your communications settings are correct, try using something like: str.startsWith("$W001N") and str.startsWith("$W001Y").

    As Mike pointed out, nothing will work unless you have the correct settings.
     
  8. AJIN NADH

    Thread Starter Member

    Dec 18, 2014
    79
    4
    Hi,
    This is working correctly when i use the arduino board with an external supply( Not through the usb).
    Thanks for the responses.
    Regards,
    Ajin nadh v a
     
Loading...