hello my names mark and I've just started arduino. i bought the programming arduino getting started with sketches by Simon Monk
on page 53 it shows code like this one below but i've modified it to work with a rgb led, it says you can do this in book but doesn't tell u the parameters to write
the code seems to work however the rgb led is white (RGB ON) and flashes the colour on and off that i pick
do i have to set all pins first before executing the code
i have 5v connected to anode of the led a 9,10 and 11 connected to the other pins on led
thanks again
int red = 9;
int green = 10;
int blue = 11;
int time= 250;
void setup ()
{
pinMode(red,OUTPUT);
pinMode(blue,OUTPUT);
pinMode(green,OUTPUT);
}
void loop()
{
flash(20, time, red);
delay(3000);
}
void flash(int numflashes, int d, int pin)
{
for (int i = 0; i < numflashes; i ++)
{
digitalWrite(pin, HIGH);
delay(d);
digitalWrite(pin, LOW);
delay(d);
}
}
5V for 1 LED is too much. use a resitor in serial.
Connect the anode directy to the port pin with resitor inbetween. The cathode to ground.
Calculate your resistor according R = U/ I, with I = 20 mA which is the recommenden current through the LED.
Because you have your LED between the output pin and +5 it will be OFF when the pin is HIGH (both pins are at +5 so no current flows) and ON when the pin is LOW (the anode is at +5 and the cathode is at Ground so current flows). You can't connect the common anode to Ground so you will have to invert the logic to the three outputs.
If you had a Common Cathode RGB LED you would connect the Cathode to Ground and the Anodes to output pins.
Also put in current limiting resistors for each LED.
int red = 9;
int green = 10;
int blue = 11;
int time= 250;
void setup ()
{
pinMode(red,OUTPUT);
pinMode(blue,OUTPUT);
pinMode(green,OUTPUT);
}
void loop()
{
digitalWrite(red,HIGH);
digitalWrite(blue,HIGH);
digitalWrite(green,HIGH);
flash(20, time, green);
delay(3000);
}
void flash(int numflashes, int d, int pin)
{
for (int i = 0; i < numflashes; i ++)
{
digitalWrite(pin, HIGH);
delay(d);
digitalWrite(pin, LOW);
delay(d);
}
}