// Pin definitions for single-color LEDs
const int led1Pin = 2;
const int led2Pin = 3;
const int led3Pin = 4;
// Pin definitions for RGB LEDs
const int rgb1RedPin = 5;
const int rgb1GreenPin = 6;
const int rgb1BluePin = 7;
const int rgb2RedPin = 8;
const int rgb2GreenPin = 9;
const int rgb2BluePin = 10;
const int rgb3RedPin = 11;
const int rgb3GreenPin = 12;
const int rgb3BluePin = 13;
// Pin definition for push button
const int btnPin = 22;
// Variables for tracking the mode and timing
int currentMode = 0; // 0: Off (initial), 1-4: Modes 1-4
unsigned long lastButtonPressTime = 0;
const unsigned long buttonDebounceDelay = 50; // 50ms debounce
const unsigned long modeChangeThreshold = 2000; // 2 seconds minimum per mode
// Variables for button state
int buttonState = HIGH; // Initial state (not pressed)
int lastButtonState = HIGH;
unsigned long lastDebounceTime = 0;
// Variables for rainbow effect
int rainbowPhase = 0;
unsigned long lastRainbowUpdate = 0;
const unsigned long rainbowUpdateInterval = 10; // Control rainbow animation speed
// Function to turn off all LEDs
void turnOffAllLEDs() {
// Turn off single-color LEDs
digitalWrite(led1Pin, LOW);
digitalWrite(led2Pin, LOW);
digitalWrite(led3Pin, LOW);
// Turn off RGB LEDs
analogWrite(rgb1RedPin, 0);
analogWrite(rgb1GreenPin, 0);
analogWrite(rgb1BluePin, 0);
analogWrite(rgb2RedPin, 0);
analogWrite(rgb2GreenPin, 0);
analogWrite(rgb2BluePin, 0);
analogWrite(rgb3RedPin, 0);
analogWrite(rgb3GreenPin, 0);
analogWrite(rgb3BluePin, 0);
}
// Mode 1: RGB LEDs display red, green, and blue colors respectively
void mode1() {
// Set indicator LED
digitalWrite(led1Pin, HIGH);
digitalWrite(led2Pin, LOW);
digitalWrite(led3Pin, LOW);
// Set RGB LEDs
// RGB1 - Red
analogWrite(rgb1RedPin, 255);
analogWrite(rgb1GreenPin, 0);
analogWrite(rgb1BluePin, 0);
// RGB2 - Green
analogWrite(rgb2RedPin, 0);
analogWrite(rgb2GreenPin, 255);
analogWrite(rgb2BluePin, 0);
// RGB3 - Blue
analogWrite(rgb3RedPin, 0);
analogWrite(rgb3GreenPin, 0);
analogWrite(rgb3BluePin, 255);
}
// Mode 2: RGB LEDs display a rainbow effect (yellow, cyan, purple)
void mode2() {
// Set indicator LED
digitalWrite(led1Pin, LOW);
digitalWrite(led2Pin, HIGH);
digitalWrite(led3Pin, LOW);
// Update rainbow effect at specified interval
unsigned long currentMillis = millis();
if (currentMillis - lastRainbowUpdate >= rainbowUpdateInterval) {
lastRainbowUpdate = currentMillis;
// Calculate the three color positions in the cycle
int pos1 = rainbowPhase % 600;
int pos2 = (rainbowPhase + 200) % 600; // Offset by 1/3 of the cycle
int pos3 = (rainbowPhase + 400) % 600; // Offset by 2/3 of the cycle
// Set the colors for each RGB LED
setRainbowColor(rgb1RedPin, rgb1GreenPin, rgb1BluePin, pos1);
setRainbowColor(rgb2RedPin, rgb2GreenPin, rgb2BluePin, pos2);
setRainbowColor(rgb3RedPin, rgb3GreenPin, rgb3BluePin, pos3);
// Increment phase for animation
rainbowPhase = (rainbowPhase + 1) % 600;
}
}
// Helper function for rainbow effect
void setRainbowColor(int redPin, int greenPin, int bluePin, int position) {
// This function cycles through Yellow -> Cyan -> Purple -> Yellow
// based on the position in a 0-599 cycle
int red, green, blue;
if (position < 200) {
// Yellow to Cyan transition
float ratio = position / 200.0;
red = 255 * (1 - ratio);
green = 255;
blue = 255 * ratio;
} else if (position < 400) {
// Cyan to Purple transition
float ratio = (position - 200) / 200.0;
red = 255 * ratio;
green = 255 * (1 - ratio);
blue = 255;
} else {
// Purple to Yellow transition
float ratio = (position - 400) / 200.0;
red = 255;
green = 255 * ratio;
blue = 255 * (1 - ratio);
}
analogWrite(redPin, red);
analogWrite(greenPin, green);
analogWrite(bluePin, blue);
}
// Mode 3: RGB LEDs display a warm white color
void mode3() {
// Set indicator LED
digitalWrite(led1Pin, LOW);
digitalWrite(led2Pin, LOW);
digitalWrite(led3Pin, HIGH);
// Set all RGB LEDs to warm white (equal R, G, B values as specified)
analogWrite(rgb1RedPin, 255);
analogWrite(rgb1GreenPin, 255);
analogWrite(rgb1BluePin, 255);
analogWrite(rgb2RedPin, 255);
analogWrite(rgb2GreenPin, 255);
analogWrite(rgb2BluePin, 255);
analogWrite(rgb3RedPin, 255);
analogWrite(rgb3GreenPin, 255);
analogWrite(rgb3BluePin, 255);
}
// Mode 4: All LEDs turn off
void mode4() {
turnOffAllLEDs();
}
void setup() {
// Initialize serial communication for debugging
Serial.begin(9600);
// Initialize single-color LED pins as outputs
pinMode(led1Pin, OUTPUT);
pinMode(led2Pin, OUTPUT);
pinMode(led3Pin, OUTPUT);
// Initialize RGB LED pins as outputs
pinMode(rgb1RedPin, OUTPUT);
pinMode(rgb1GreenPin, OUTPUT);
pinMode(rgb1BluePin, OUTPUT);
pinMode(rgb2RedPin, OUTPUT);
pinMode(rgb2GreenPin, OUTPUT);
pinMode(rgb2BluePin, OUTPUT);
pinMode(rgb3RedPin, OUTPUT);
pinMode(rgb3GreenPin, OUTPUT);
pinMode(rgb3BluePin, OUTPUT);
// Initialize button pin as input with pull-up resistor
pinMode(btnPin, INPUT_PULLUP);
// Turn off all LEDs initially
turnOffAllLEDs();
}
void loop() {
// Button debouncing logic
int reading = digitalRead(btnPin);
if (reading != lastButtonState) {
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > buttonDebounceDelay) {
if (reading != buttonState) {
buttonState = reading;
// If the button is pressed (LOW due to pull-up resistor)
if (buttonState == LOW) {
// Check if enough time has passed since the last mode change
if (millis() - lastButtonPressTime >= modeChangeThreshold) {
// Cycle to the next mode
currentMode = (currentMode % 4) + 1; // Cycle from 1 to 4
lastButtonPressTime = millis();
// Debug output
Serial.print("Mode changed to: ");
Serial.println(currentMode);
}
}
}
}
lastButtonState = reading;
// Execute the current mode
switch (currentMode) {
case 1:
mode1();
break;
case 2:
mode2();
break;
case 3:
mode3();
break;
case 4:
mode4();
break;
default:
// Initial state or invalid mode, turn everything off
turnOffAllLEDs();
break;
}
// Small delay for stability
delay(5);
}
mega:SCL
mega:SDA
mega:AREF
mega:GND.1
mega:13
mega:12
mega:11
mega:10
mega:9
mega:8
mega:7
mega:6
mega:5
mega:4
mega:3
mega:2
mega:1
mega:0
mega:14
mega:15
mega:16
mega:17
mega:18
mega:19
mega:20
mega:21
mega:5V.1
mega:5V.2
mega:22
mega:23
mega:24
mega:25
mega:26
mega:27
mega:28
mega:29
mega:30
mega:31
mega:32
mega:33
mega:34
mega:35
mega:36
mega:37
mega:38
mega:39
mega:40
mega:41
mega:42
mega:43
mega:44
mega:45
mega:46
mega:47
mega:48
mega:49
mega:50
mega:51
mega:52
mega:53
mega:GND.4
mega:GND.5
mega:IOREF
mega:RESET
mega:3.3V
mega:5V
mega:GND.2
mega:GND.3
mega:VIN
mega:A0
mega:A1
mega:A2
mega:A3
mega:A4
mega:A5
mega:A6
mega:A7
mega:A8
mega:A9
mega:A10
mega:A11
mega:A12
mega:A13
mega:A14
mega:A15
led1:A
led1:C
led2:A
led2:C
led3:A
led3:C
btn1:1.l
btn1:2.l
btn1:1.r
btn1:2.r
rgb1:R
rgb1:COM
rgb1:G
rgb1:B
rgb2:R
rgb2:COM
rgb2:G
rgb2:B
rgb3:R
rgb3:COM
rgb3:G
rgb3:B