// Fnu Shoaib Ahmed, 652420658, fahme8
// Lab 9 - RGB LED with Joystick and Potentiometer Control
// Description - This code reads values from a two-axis joystick and a potentiometer to control an RGB LED’s color intensity.
// The joystick’s X-axis controls the Red component, the Y-axis controls the Green component, and the potentiometer controls the Blue component.
// Separate Red, Green, and Blue LEDs display individual color intensities based on PWM values.
// Assumptions - The circuit includes 220-ohm resistors for each LED to prevent damage, and the PWM pins (9, 10, 11) are used for LED control as specified in the lab requirements.
// References - I referred to lab-provided links for setting up the joystick and potentiometer and for understanding PWM control on Arduino.
// Pin assignments
const int joystickXPin = A0; // Joystick X-axis for Red control
const int joystickYPin = A1; // Joystick Y-axis for Green control
const int potentiometerPin = A2; // Potentiometer for Blue control
const int redLEDPin = 9; // PWM pin for Red (both RGB and Red LED)
const int greenLEDPin = 10; // PWM pin for Green (both RGB and Green LED)
const int blueLEDPin = 11; // PWM pin for Blue (both RGB and Blue LED)
// Variables to store color values
int redValue = 0;
int greenValue = 0;
int blueValue = 0;
// Timing variables for millis-based delay
unsigned long previousMillis = 0;
const long interval = 10; // Interval for updating LED values
void setup() {
// Set LED pins as outputs
pinMode(redLEDPin, OUTPUT);
pinMode(greenLEDPin, OUTPUT);
pinMode(blueLEDPin, OUTPUT);
}
void loop() {
// Check if the time interval has passed
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
// Read analog inputs
int joystickXValue = analogRead(joystickXPin); // Read X-axis value
int joystickYValue = analogRead(joystickYPin); // Read Y-axis value
int potentiometerValue = analogRead(potentiometerPin); // Read potentiometer value
// Map the analog values (0-1023) to PWM values (0-255)
redValue = map(joystickXValue, 0, 1023, 0, 255);
greenValue = map(joystickYValue, 0, 1023, 0, 255);
blueValue = map(potentiometerValue, 0, 1023, 0, 255);
// Set PWM values to RGB LED and individual LEDs
analogWrite(redLEDPin, redValue); // Red LED intensity
analogWrite(greenLEDPin, greenValue); // Green LED intensity
analogWrite(blueLEDPin, blueValue); // Blue LED intensity
}
}