0% found this document useful (0 votes)
6 views

COMP 1045 Lab 6 F24

The document outlines a lab exercise for creating a circuit with an RGB LED and controlling its brightness using a rotation sensor and push button. It describes multiple levels of complexity, including mapping brightness to the rotation sensor, locking brightness for each color, and allowing user-defined color sequences. The source code provided demonstrates how to fade the RGB LED and manage its brightness through PWM control.

Uploaded by

fetat29455
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

COMP 1045 Lab 6 F24

The document outlines a lab exercise for creating a circuit with an RGB LED and controlling its brightness using a rotation sensor and push button. It describes multiple levels of complexity, including mapping brightness to the rotation sensor, locking brightness for each color, and allowing user-defined color sequences. The source code provided demonstrates how to fade the RGB LED and manage its brightness through PWM control.

Uploaded by

fetat29455
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

COMP 1045 Lab 6

Circuit: Create the circuit below with the RGB led.

Level 1: Build the circuit and run the source code.

Level 2: Map the brightness of the green channel of the RGB led to the rotation sensor A0. As
you rotate clockwise the light will get brighter.

Level 3: Write a program that allows the user to set the brightness of each of the RGB channels
by using the rotation dial to control brightness and a push button to select the channel. This
should be done using functions to manage the various aspects of the program. For example, the
operation of this program could be as follows.

1. User turns the dial to set the brightness of the red LED. The function setBrightness()
should be used to adjust and store the brightness value based on the rotation dial's
input.

2. The User pushes button 1. A function lockBrightness() should detect the button press
and lock the red brightness in.

3. User turns the dial to set the brightness of the green LED, using setBrightness()
function again to adjust the green LED's brightness.

4. The User pushes button 1 and the green brightness stays locked in using
lockBrightness() function.
5. User turns the dial to set the brightness of the blue LED. The setBrightness() function is
used again for the blue LED.

6. If the user pushes button 1, the blue brightness stays locked in using lockBrightness()
function.

7. The lights should stay on as you move from one color to the next. The goal is to be able
to make any color.

8. You should also use the SerialMonitorPrint() function to display the value of each light
on the serial monitor.

Functions are an excellent way to organize your code and make it reusable. By abstracting
some logic into a function, you can call that function whenever you need to perform that
operation. This makes your code easier to understand and maintain.

Level 4: Modify the code from Level 3 to incorporate a user option. This time, the user should
be able to define the order in which the colors will be set. Here's a detailed breakdown of testing
the code:

1. When the code starts, the rotation dial does nothing, the light is off, and a message
appears once in the form: "Please input the sequence of colors you would like to change
in the format RGB, R = Red, G = Green, B = Blue. For example, RGB, GRB, BGR, etc."

2. If the user enters a sequence (let's say GRB), then the rotation dial will be altering the
brightness of the green LED first, with the value being printed to the serial monitor.

3. Once the user hits the button, the brightness for the green LED is stored in memory and
the rotation dial now controls the red LED.

4. The user adjusts the brightness of the red LED with the rotation dial, and once satisfied,
presses the button to lock the brightness of the red LED and move on to the blue LED.

5. The user now repeats the process for the blue LED. The rotation dial controls the
brightness of the blue LED, and once the user hits the button, the brightness of the blue
LED is locked. At this point, the rotation dial can no longer change the brightness of any
LED.

6. The code should continue with this sequence, allowing the user to enter a new sequence
of colors after setting the brightness for all three LEDs.
7. Remember to implement the proper validations in the code, such as ensuring the input
string contains only the characters R, G, and B and each character only once. You can
assume the user will always input 3 letters.

SOURCE CODE:

/*
Title:Experiment #006 - RGB Fade
Description: This program will fade the Red, Green, Blue(RGB) LED Instructions: The
RGB LED is connected to a Pulse Width Modulation(PWM) pin.
You can use analogWrite to control brightness.
*/
int RGBRedPin = 9; //The red RGB LED is connected pin 9 of the Arduino.
int RGBGreenPin = 10; //The green RGB LED is connected pin 10 of the Arduino.
int RGBBluePin = 11; //The blue RGB LED is connected pin 11 of the Arduino.
int fadeDelay = 5; //This is the number of milliseconds that it will take between steps

void setup() { //The Setup function runs once.


pinMode(RGBRedPin, OUTPUT); //Setup red RGB LED pin as an output pin.
pinMode(RGBGreenPin, OUTPUT); //Setup green RGB LED pin as an output pin.
pinMode(RGBBluePin, OUTPUT); //Setup blue RGB LED pin as an output pin.
}

void loop() { //The loop function runs forever.

for (int x = 0; x <= 255; x++) { //Fade in red.


analogWrite(RGBRedPin, x);
delay(fadeDelay);
}

for (int x = 255; x >= 0; x--) { //Fade out red.


analogWrite(RGBRedPin, x);
delay(fadeDelay);
}

for (int x = 0; x <= 255; x++) { //Fade in green.


analogWrite(RGBGreenPin, x);
delay(fadeDelay);
}

for (int x = 255; x >= 0; x--) { //Fade out green.


analogWrite(RGBGreenPin, x);
delay(fadeDelay);
}

for (int x = 0; x <= 255; x++) { //Fade in blue.


analogWrite(RGBBluePin, x);
delay(fadeDelay);
}

for (int x = 255; x >= 0; x--) { //Fade out blue.


analogWrite(RGBBluePin, x);
delay(fadeDelay);
}

for (int x = 0; x <= 255; x++) { //Fade in red and blue.


analogWrite(RGBRedPin, x);
analogWrite(RGBBluePin, x);
delay(fadeDelay);
}

for (int x = 255; x >= 0; x--) { //Fade out red and blue.
analogWrite(RGBRedPin, x);
analogWrite(RGBBluePin, x);
delay(fadeDelay);
}

for (int x = 0; x <= 255; x++) { //Fade in red and green.


analogWrite(RGBRedPin, x);
analogWrite(RGBGreenPin, x);
delay(fadeDelay);
}

for (int x = 255; x >= 0; x--) { //Fade out red and green.
analogWrite(RGBRedPin, x);
analogWrite(RGBGreenPin, x);
delay(fadeDelay);
}

for (int x = 0; x <= 255; x++) { //Fade in green and blue.


analogWrite(RGBGreenPin, x);
analogWrite(RGBBluePin, x);
delay(fadeDelay);
}

for (int x = 255; x >= 0; x--) { //Fade out green and blue.
analogWrite(RGBGreenPin, x);
analogWrite(RGBBluePin, x);
delay(fadeDelay);
}

for (int x = 0; x <= 255; x++) { //Fade in red, green and blue.
analogWrite(RGBRedPin, x);
analogWrite(RGBGreenPin, x);
analogWrite(RGBBluePin, x);
delay(fadeDelay);
}

for (int x = 255; x >= 0; x--) { //Fade out red, green and blue.
analogWrite(RGBRedPin, x);
analogWrite(RGBGreenPin, x);
analogWrite(RGBBluePin, x);
delay(fadeDelay);
}
}

You might also like