//Define the ports that we are using
int RedPin = 6;
int BluePin = 10;
int GreenPin = 11;
//Define the mode for each port – these will all be used as Output.
void setup() {
pinMode(RedPin, OUTPUT);
pinMode(BluePin, OUTPUT);
pinMode(GreenPin, OUTPUT);
}
//Specify the order in which we want to the colours to appear on the LED, with a 1s delay between
//each colour. Note: this will not give the colours we expect… don’t panic.
void loop() {
setColor(255, 0, 0); // red
delay(1000);
setColor(0, 255, 0); // green
delay(1000);
setColor(0, 0, 255); // blue
delay(1000);
setColor(255, 255, 0); // yellow
delay(1000);
setColor(80, 0, 80); // purple
delay(1000);
setColor(0, 255, 255); // aqua
delay(1000);
}
//Using the code without the subtraction, we don’t get the colours we expect, i.e., the colours cycle through light blue;
//purple; yellow. This is because we are driving the anode. We simply need to invert the
//colours, i.e., by subtracting the colour from 255, which can easily do by modifying the final
//block of code to read:
void setColor(int red, int green, int blue)
{
analogWrite(RedPin, 255-red);
analogWrite(GreenPin, 255-green);
analogWrite(BluePin,255-blue);
}