Part II Write a program that implements a simple color selector using the RGB LE
ID: 2267158 • Letter: P
Question
Part II Write a program that implements a simple color selector using the RGB LED Components: -1x Pushbutton -3x 220 resistors (for the LEDs) 1x RGB LED -1x10 k resistor (for the pushbutton) 2 Connect RGB LED and joystick as follows: a. Rx Arduino The joystick is simply two potentiometers, one for the X axis and one for the Y axis, set up as 0-5V voltage dividers. According to the diagram above, the x and y outputs are connected to ADC inputs 0 and 1. oltageand1. se the xs tee5re o he se b. Write a program to use the joystick's displacement to gradually select the brightness (i.e. change the PWM duty cycle) of the Blue and Red LED. Green is not used in this circuit. 2 i. Blue is controlled with the X axis of the joystick. ii. Red is controlled with the Y axis of the joystick. iii. When the joystick is centered, both LEDs are 50% on. When in top right position, both are fully on. In the lower left corner, both LEDs are of. The program should use a pushbutton to "save" the color and print the selected color details to the Serial monitor. c.Explanation / Answer
int redPin = 3; // Red RGB pin -> D3
int bluePin = 5; // Blue RGB pin -> D5
int swit = 4; //setting up our switch
int potBlue = A0; // Potentiometer controls Blue pin -> A0
int potRed = A1; // Potentiometer controls Red pin -> A1
void setup() {
pinMode(redPin,OUTPUT);
pinMode(bluePin,OUTPUT);
pinMode(potRed, INPUT);
pinMode(potBlue, INPUT);
pinMode(swit, INPUT );
Serial.begin(9600);
}
void loop() {
// Reads the current position of the potentiometer and converts
// to a value between 0 and 255 to control the according RGB pin with PWM
// RGB LED COMMON ANODE
analogWrite(redPin,(255./1023.)*analogRead(potRed));
analogWrite(bluePin,(255./1023.)*analogRead(potBlue));
// Uncomment for RGB LED COMMON CATHODE
/*
analogWrite(redPin,255-(255./1023.)*analogRead(potRed));
analogWrite(bluePin,255-(255./1023.)*analogRead(potBlue));
*/
delay(1);
}