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.