Sketch that once worked, then stopped.

Hi All,

I wonder if anyone can help. Below is the code for a sketch that worked except for the output on pin 11, the red of an RGB LED. It’s from the book Exploring Arduino by Jeremy Blum.

//Sending multiple variables at once

//Define LED pins

const int RED =11;

const int GREEN =10;

const int BLUE =9;

//Variables for RGB levels

int rval = 0;

int gval = 0;

int bval = 0;

void setup()

{

Serial.begin(9600);

//Set pins as Outputs

pinMode (RED, OUTPUT);

pinMode (GREEN, OUTPUT);

pinMode (BLUE, OUTPUT);

}

void loop()

{

//Keep working as long as data is in buffer

while (Serial.available() > 0)

{

rval =

Serial.parseInt(); //First valid integer

gval =

Serial.parseInt(); //Second integer

bval =

Serial.parseInt(); //Third integer

if (Serial.read() == ‘\n’)// Done transmitting

{

//set LED

analogWrite (RED, rval);

analogWrite (GREEN, gval);

analogWrite (BLUE, bval);

}

}

}

As I have stated above it worked with the exception of pin 11, but now doesn’t do anything at all.

I have changed all the leads, the LED, and even tried output pins 3, 5, and 6 (the other three PWM pins) but it still only lit the green and blue, never the red. Then it stopped lighting at all.

So I tested the RGB LED, cabling and the individual outputs by loading the sketch below and changing the output pin from 9 to 10 then to 11. This checks all three colours, the connecting leads and the actual outputs without disturbing anything on the breadboard.

// single character control of an LED

const int LED=9; // Not in sketch, added for this post also tried pins 10, and 11 and they both lit the LED R,G,B respectively

char data; //Holds incoming data

void setup()

{

Serial.begin(9600);

pinMode(LED, OUTPUT);

}

void loop()

{

//Only act when data is available in buffer

if(Serial.available() >0)

{

data=Serial.read(); //Read byte of data

//Turn LED on

if (data==‘1’)

{

digitalWrite(LED, HIGH);

Serial.println(“LED is ON”);

}

//Turn LED off

else if (data ==‘0’)

{

digitalWrite(LED, LOW);

Serial.println(“LED is OFF”);

}

}

}

Needless to say, all three colours are present and correct on the correct pins, the leads all do there thing and all the three outputs work.

Other information that might be relevant, initially when I first uploaded and subsequently tried to start using the sketch, the Arduino had an ethernet shield onboard. I took this off (after powering down) but it made no difference. I’m now trying it out without the ethernet shield.

I’ve since used the same Arduino in a sketch switching two LED’s via a two way 5v relay board. although i’ve not used pin 11 - the pin that has never worked in the first sketch, but works in the second.

Any ideas would be greatly appreciated.

Hope it all makes sense…

Regards,

michael.

There’s a default 1 sec timeout on the Serial.parseInt() function. How are you sending the data ? Could it’s timing miss the 1’st character but catch the later ones ? Perhaps it’s the test conditions / input that’s changed from working to non-working ?

http://arduino.cc/en/Serial/ParseInt

Hi Mee n Mac,

Thanks for your reply. I tried the following sketch off the Arduino site and I had no problems whatsoever :

/*

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);

}

}

}

I tried it with and without the ‘setTimeout’ function and it made no difference either way. It’s almost the same as the original, in fact it’s better because it prints out your input.

So I can tag it solved, and thanks again.

Regards ,

michael.