Computer Science Canada

Arduino IDE - RGB LED Method

Author:  CtrlAltDelicious [ Mon Oct 23, 2017 9:58 pm ]
Post subject:  Arduino IDE - RGB LED Method

Goal: Get the RGB light on the Arduino to blink Red, pause for a second, then turn Blue, pause for a second, and turn Green. This should occur just once, so it shouldn't be in a loop, as far as I know

Note: // are line comments, and the specific board I'm using is the Protosnap - Lilypad Development Board (https://www.sparkfun.com/products/11262), Arduino Software Program: https://www.arduino.cc/en/Main/Software,

What I've Done so Far:

int LED5 = 5;
int LED6 = 6;
int redPin = 9;
int greenPin = 11;
int bluePin = 10;
int delayTime = 200; // delay is 1/5 of a second

void setup() {

pinMode(LED5, OUTPUT); // sets the LED 5 to be an output
pinMode (LED6, OUTPUT); // sets the LED 6 to be an output
pinMode (A2, OUTPUT); // sets the LED A2 to be an output
pinMode (A4, OUTPUT); // sets the LED A4 to be an output
pinMode (A3, OUTPUT); // sets the LED A3 to be an output
pinMode(redPin, OUTPUT);// sets the redPin (9) to be an output
pinMode(greenPin, OUTPUT); // sets the greenPin (11) to be an output
pinMode(bluePin, OUTPUT); // sets the bluePin (10) to be an input
}

void loop(int red, int green, int blue) {

rgbLight(255,0,0); //turn the RGB LED red
delay(delayTime); //delay
rgbLight(0,0,0); //turn the RGB LED red
delay(delayTime); //delay
rgbLight(0,255,0); //turn the RGB LED green
delay(delayTime); //delay
rgbLight(0,0,0); //turn the RGB LED red
delay(delayTime); //delay
rgbLight(0,0,255); //turn the RGB LED blue
delay(delayTime); //delay
rgbLight(0,0,0); //turn the RGB LED red
delay(delayTime); //delay
digitalWrite (LED5, HIGH);
delay (delayTime);
digitalWrite (LED5, LOW);
delay (delayTime);
digitalWrite (LED6, HIGH);
delay (delayTime);
digitalWrite (LED6, LOW);
delay (delayTime);
digitalWrite (A2, HIGH);
delay (delayTime);
digitalWrite (A2, LOW);
delay (delayTime);
digitalWrite (A4, HIGH);
delay (delayTime);
digitalWrite (A4, LOW);
delay (delayTime);
digitalWrite (A3, HIGH);
delay (delayTime);
digitalWrite (A3, LOW);
delay (delayTime);
}

void rgbLight (int red, int green, int blue) {
analogWrite(redPin, 255-100);
analogWrite(bluePin, 255-50);
analogWrite(greenPin, 255-100);
}


I certainly appreciate any help I can get - thanks in advance! Canada

Author:  rdrake [ Tue Oct 24, 2017 6:57 am ]
Post subject:  Re: Arduino IDE - RGB LED Method

A few things stand out.
  • The loop() function shouldn't have any arguments.
  • You pass arguments into rgbLight() but that function ignores them.
  • Your code is in a loop but you don't want to loop.
  • Your delay is for 200 ms but you want it to be 1000 ms.
  • It looks like you're trying to blink the lights in the bottom portion of loop(). Do you mean to do that?

Author:  CtrlAltDelicious [ Tue Oct 24, 2017 10:11 am ]
Post subject:  Re: Arduino IDE - RGB LED Method

Ok, I took your suggestions into consideration, here is my new code:

int LED5 = 5; // LED is connected to digital pin 5
int LED6 = 6; // LED is connected to digital pin 6
int redPin = 9; // R petal on RGB LED module connected to digital pin 11
int greenPin = 11; // G petal on RGB LED module connected to digital pin 9
int bluePin = 10; // B petal on RGB LED module connected to digital pin 10
int delayTime = 1000; // delay is a second

void setup(){
pinMode(LED5, OUTPUT); // sets the LED 5 to be an output
pinMode (LED6, OUTPUT); // sets the LED 6 to be an output
pinMode (A2, OUTPUT); // sets the LED A2 to be an output
pinMode (A4, OUTPUT); // sets the LED A4 to be an output
pinMode (A3, OUTPUT); // sets the LED A3 to be an output
pinMode(redPin, OUTPUT);// sets the redPin (9) to be an output
pinMode(greenPin, OUTPUT); // sets the greenPin (11) to be an output
pinMode(bluePin, OUTPUT); // sets the bluePin (10) to be an input
}

void loop() { // run forever
rgbLight (0,0,0);
}

void rgbLight (int red, int blue, int green) {
analogWrite(redPin, 255-red);
analogWrite(bluePin, 255-blue);
analogWrite(greenPin, 255-green);
}

void LEDs (){
rgbLight(255,0,0); //turn the RGB LED red
delay(delayTime); //delay for 1 second
rgbLight(0,0,0); //turn the RGB LED red
delay(delayTime); //delay for 1 second
rgbLight(0,255,0); //turn the RGB LED green
delay(delayTime); //delay for 1 second
rgbLight(0,0,0); //turn the RGB LED red
delay(delayTime); //delay for 1 second
rgbLight(0,0,255); //turn the RGB LED blue
delay(delayTime); //delay for 1 second
rgbLight(0,0,0); //turn the RGB LED red
delay(delayTime); //delay for 1 second
digitalWrite (LED5, HIGH);
delay (delayTime);
digitalWrite (LED5, LOW);
delay (delayTime);
digitalWrite (LED6, HIGH);
delay (delayTime);
digitalWrite (LED6, LOW);
delay (delayTime);
digitalWrite (A2, HIGH);
delay (delayTime);
digitalWrite (A2, LOW);
delay (delayTime);
digitalWrite (A4, HIGH);
delay (delayTime);
digitalWrite (A4, LOW);
delay (delayTime);
digitalWrite (A3, HIGH);
delay (delayTime);
digitalWrite (A3, LOW);
delay (delayTime);
}

I'm not sure exactly sure why the RGB glows white, but I'm guessing it's because all the colours are on at once? How would I fix that so I can show different colours at different times


: