Go Down

Topic: Problem in Tutorial Code re ReadASCIIString (Read 120 times) previous topic - next topic

wrishel

Please advise if this is the appropriate forum for reporting what are likely minor corrections.

Referring to http://arduino.cc/en/Tutorial/ReadASCIIString

Using Arduino 1.0.6 with an Uno Rev 3 the code shown below does not work. No changes to the LED are create in response to serial input and the hex settings are not sent to serial output. However if the "if (Serial.read() == '\n')" is removed, then it executes as expected.

Apparently, the author of this example expects parseInt to leave the trailing delimiter in the input buffer, but it currently does not.



Code: [Select]
/*
  Reading a serial ASCII-encoded string.
 
 This sketch demonstrates the Serial parseInt() function.
 It looks for an ASCII string of comma-separated values.
 It parses them into ints, and uses those to fade an RGB LED.
 
 Circuit: Common-anode RGB LED wired like so:
 * Red cathode: digital pin 3
 * Green cathode: digital pin 5
 * blue cathode: digital pin 6
 * anode: +5V
 
 created 13 Apr 2012
 by Tom Igoe
 
 This example code is in the public domain.
 */

// pins for the LEDs:
const int redPin = 3;
const int greenPin = 5;
const int bluePin = 6;

void setup() {
  // initialize serial:
  Serial.begin(9600);
  // make the pins outputs:
  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(bluePin, OUTPUT);

}

void loop() {
  // if there's any serial available, read it:
  while (Serial.available() > 0) {

    // look for the next valid integer in the incoming serial stream:
    int red = Serial.parseInt();
    // do it again:
    int green = Serial.parseInt();
    // do it again:
    int blue = Serial.parseInt();

    // look for the newline. That's the end of your
    // sentence:
    if (Serial.read() == '\n') {
      // constrain the values to 0 - 255 and invert
      // if you're using a common-cathode LED, just use "constrain(color, 0, 255);"
      red = 255 - constrain(red, 0, 255);
      green = 255 - constrain(green, 0, 255);
      blue = 255 - constrain(blue, 0, 255);

      // fade the red, green, and blue legs of the LED:
      analogWrite(redPin, red);
      analogWrite(greenPin, green);
      analogWrite(bluePin, blue);

      // print the three numbers in one string as hexadecimal:
      Serial.print(red, HEX);
      Serial.print(green, HEX);
      Serial.println(blue, HEX);
    }
  }
}

Robin2

#1
Nov 19, 2014, 11:19 am Last Edit: Nov 19, 2014, 11:21 am by Robin2
Using Arduino 1.0.6 with an Uno Rev 3 the code shown below does not work. No changes to the LED are create in response to serial input and the hex settings are not sent to serial output. However if the "if (Serial.read() == '\n')" is removed, then it executes as expected.

It is certainly possible that you have found a bug but I wonder if the problem might be influenced by the line-ending setting of your Serial Monitor?

Unfortunately, nobody takes any notice of suggestions for improvements.


...R

Jantje

Looking at the code of parseInt (Arduino IDE 1.5.8) I don't think CR characters are removed.
From what I see it should work.
Do a check as Robin proposes.

if you want to read the serial line by line you may be interested in this blog post of me
http://blog.baeyens.it/#post16
The intention the post is to document until the web interface I'm using but it all is based on a line by line read from the serial queue. So this blog post starts there.

Best regards
Jantje
Do not PM me a question unless you are prepared to pay for consultancy.
Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -

I just used this code you posted as a starting point and it works perfectly controlling dimming on individual colors of an RGB LED using an iPhone app communicating with Bluetooth LE.

If you're still looking to solve this problem let me know and I'll help as much as I can.

BTW, are you send a new line at the end of your rgb values because it's required? e.g.: for  HIGH or full brightness with Red on the rgb led the string i send from iOS is "255,0,0\n". I'm using a slider for all three channells eg: R,G,B\n

Code: [Select]
let result = self.writeString("\(yourRedValue),0,0\n")

Go Up