ReadASCII String Example Problem

Hello,
I want to use the example to control the birghtness of 3 LEDs. The code is here:

/*
  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);
    }
  }
}

As the example in http://arduino.cc/en/Tutorial/ReadASCIIString says I should enter sth. like 5,220,70 to give an analog write function to drive LEDS. After I've done sending these numbers, there was no responded back and the 3 LEDs was always shine at their full power even I entered 0,0,0.

For the circuit, I connect pin 3,5,6 to each 3 resistors seperately and series the resistors to each LEDs' cathode. Each LEDs' anode are common which are connected to 5V.

Why there was nothing happen? Is there any update about the example

Thank you
BlackMelon

What do you see on the Serial monitor ?
What have you got the line ending set to in the Serial.monitor ?

Isn't this pretty much the same question as in your other Thread?

Have you looked at the demo code I posted in Reply #3 in that Thread?

DON'T DOUBLE POST it just wastes everyones time.

...R

What do you see on the Serial monitor ?
What have you got the line ending set to in the Serial.monitor ?

Absolutely nothing in the serial, even I've entered numbers.

I'm sorry for double post.... Won't do it again next time :smiley:

You didn't answer my question about how you have got the line ending set in the Serial monitor. The program is looking for a Newline character at the end of the input. If it is not there then the program will not work.

BlackMelon:

What do you see on the Serial monitor ?
What have you got the line ending set to in the Serial.monitor ?

Absolutely nothing in the serial, even I've entered numbers.

I'm sorry for double post.... Won't do it again next time :smiley:

try something like this by entering from your serial monitor data in this form:

R100G255B0

you will get output like this:

Red Pin = 100
Green Pin = 255
Blue Pin = 0

the code below looks for an integer following a colour

Can you modify whatever is transmitting the colours to your arduino to send in that fashion?

// pins for the LEDs:
const int redPin = 3;
const int greenPin = 5;
const int bluePin = 6;
byte red, green, blue;

void setup() 
{
  Serial.begin(115200);
  pinMode(redPin, OUTPUT); 
  pinMode(greenPin, OUTPUT); 
  pinMode(bluePin, OUTPUT); 
}

void loop() 
{
  while (Serial.available() > 0) 
  {
    char myChar = Serial.read();
    if (myChar == 'R')
    {
      red = Serial.parseInt(); 
      Serial.print("Red Pin = ");
      Serial.println(red);
    }
    if (myChar == 'G')
    {
      green = Serial.parseInt(); 
      Serial.print("Green Pin = ");
      Serial.println(green); 
    }
    if (myChar == 'B')
    {
      blue = Serial.parseInt(); 
      Serial.print("Blue Pin = ");
      Serial.println(blue);
    }
    /*  //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< what are you trying to do in the block that follows?
    red = 255 - constrain(red, 0, 255);
    green = 255 - constrain(green, 0, 255);
    blue = 255 - constrain(blue, 0, 255);
    */
    analogWrite(redPin, red);
    analogWrite(greenPin, green);
    analogWrite(bluePin, blue);
    /*  //<<<<<<<<<<<<<<<<<<<<<<<<<  what's this block all about?
    Serial.print(red, HEX);
    Serial.print(green, HEX);
    Serial.println(blue, HEX);
    */
  }
}