Landing : Athabascau University
  • Blogs
  • Circuit 1D: RGB Night-Light

Circuit 1D: RGB Night-Light

I had no issues building the circuit. I decided to use some of the SIK Guide example code to modify my morse code program. The program makes the LED blink my name (Adam) in morse code only when the photoresistor senses that it is dark, and the potentiometer changes the colour of the LED. Here is the code:

int photoThreshold = 400; //if the photoresistor reading is below this value the the light will turn on
//LEDs are connected to these pins
int RedPin = 9;
int GreenPin = 10;
int BluePin = 11;
void setup() {
// put your setup code here, to run once:
//set the LED pins to output
pinMode(RedPin, OUTPUT);
pinMode(GreenPin, OUTPUT);
pinMode(BluePin, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
letterA();
longSpace();
letterD();
longSpace();
letterA();
longSpace();
letterM();
longerSpace();
}
boolean isDark(){
if (analogRead(A0) < photoThreshold) {
return true;
}
else {
return false;
}
}
int ditDuration(){
return 100; //100 milliseconds
}
void letterA(){
dit();
space();
dah();
}
void letterD(){
dah();
space();
dit();
space();
dit();
}
void letterM(){
dah();
space();
dah();
}
void dit(){
if(isDark()){
LedOn(); // Turn on the LED.
delay(ditDuration()); // The duration of a dit.
}
}
void dah(){
if(isDark()){
LedOn(); // Turn on the LED.
delay(3*ditDuration()); // The duration of a dah is 3 times that of a dit.
}
}
void space(){
LedOff(); // Turn off the LED.
if(isDark()){
delay(ditDuration()); // The duration of a space is equal to that of a dit.
}
}
void longSpace(){
LedOff(); // Turn off the LED.
if(isDark()){
delay(3*ditDuration()); // The duration is 3 times that of a dit, used between characters.
}
}
void longerSpace(){
LedOff(); // Turn off the LED.
if(isDark()){
delay(7*ditDuration()); // The duration is 7 times that of a dit, used between words.
}
}
void LedOff(){
//set all three LED pins to 0 or OFF
analogWrite(RedPin, 0);
analogWrite(GreenPin, 0);
analogWrite(BluePin, 0);
}
void LedOn(){
int potentiometer = analogRead(A1);
//These if statements check for a variety of ranges and
//call different functions based on the current potentiometer value.
//Those functions are found at the bottom of the sketch.
if (potentiometer > 0 && potentiometer <= 150)
red();
if (potentiometer > 150 && potentiometer <= 300)
orange();
if (potentiometer > 300 && potentiometer <= 450)
yellow();
if (potentiometer > 450 && potentiometer <= 600)
green();
if (potentiometer > 600 && potentiometer <= 750)
cyan();
if (potentiometer > 750 && potentiometer <= 900)
blue();
if (potentiometer > 900)
magenta();
}
void red () {
//set the LED pins to values that make red
analogWrite(RedPin, 100);
analogWrite(GreenPin, 0);
analogWrite(BluePin, 0);
}
void orange () {
//set the LED pins to values that make orange
analogWrite(RedPin, 100);
analogWrite(GreenPin, 50);
analogWrite(BluePin, 0);
}
void yellow () {
//set the LED pins to values that make yellow
analogWrite(RedPin, 100);
analogWrite(GreenPin, 100);
analogWrite(BluePin, 0);
}
void green () {
//set the LED pins to values that make green
analogWrite(RedPin, 0);
analogWrite(GreenPin, 100);
analogWrite(BluePin, 0);
}
void cyan () {
//set the LED pins to values that make cyan
analogWrite(RedPin, 0);
analogWrite(GreenPin, 100);
analogWrite(BluePin, 100);
}
void blue () {
//set the LED pins to values that make blue
analogWrite(RedPin, 0);
analogWrite(GreenPin, 0);
analogWrite(BluePin, 100);
}
void magenta () {
//set the LED pins to values that make magenta
analogWrite(RedPin, 100);
analogWrite(GreenPin, 0);
analogWrite(BluePin, 100);
}

The main changes from my previous morse code program from Circuit 1C are the addition of int variables and pinMode statements for each of the red, green, and blue pins, the LedOff() method that sets all three colour pins to 0, the LedOn() method that reads the potentiometer value and calls one of the colour methods, and the colour methods themselves. The code inside the LedOn() method and the colour methods themselves were copied from the SIK Guide code. The colour methods include one method each for setting the LED to the colours red, orange, yellow, green, cyan, blue, and magenta.

Here is a video of the code in action:  MorseCodeNightLight

I did not have any trouble writing the program, it worked correctly the first time I ran it.