// // Define pins
// const int ledPins[] = {2, 4, 16};
// const int buttonPin = 5;
// const int rgbRedPin = 12;
// const int rgbGreenPin = 14;
// const int rgbBluePin = 27;
// const int potPin = 35;
// // Variables
// int buttonState = 0;
// int lastButtonState = 0;
// int clickCount = 0;
// unsigned long lastDebounceTime = 0;
// unsigned long debounceDelay = 50;
// void setup() {
// // Set up LED pins
// for (int i = 0; i < 3; i++) {
// pinMode(ledPins[i], OUTPUT);
// }
// // Set up button pin
// pinMode(buttonPin, INPUT_PULLUP);
// // Set up RGB LED pins
// pinMode(rgbRedPin, OUTPUT);
// pinMode(rgbGreenPin, OUTPUT);
// pinMode(rgbBluePin, OUTPUT);
// // Initialize Serial Monitor
// Serial.begin(115200);
// Serial.println("System Initialized");
// }
// void loop() {
// int reading = digitalRead(buttonPin);
// // Debounce the button
// if (reading != lastButtonState) {
// lastDebounceTime = millis();
// }
// if ((millis() - lastDebounceTime) > debounceDelay) {
// if (reading != buttonState) {
// buttonState = reading;
// if (buttonState == LOW) { // Button is pressed
// clickCount++;
// clickCount %= 3; // Cycle between 0, 1, and 2
// // Print the number of button pushes to the Serial Monitor
// Serial.print("Button Push Count: ");
// Serial.println(clickCount);
// if (clickCount == 0) {
// lightUpLEDsSequentially();
// } else if (clickCount == 1) {
// lightUpRGBRandomly();
// }
// }
// }
// }
// lastButtonState = reading;
// if (clickCount == 2) {
// controlRGBWithPot();
// }
// }
// void lightUpLEDsSequentially() {
// for (int i = 0; i < 3; i++) {
// digitalWrite(ledPins[i], HIGH);
// delay(500);
// digitalWrite(ledPins[i], LOW);
// delay(500);
// }
// }
// void lightUpRGBRandomly() {
// int redValue = random(0, 256);
// int greenValue = random(0, 256);
// int blueValue = random(0, 256);
// analogWrite(rgbRedPin, redValue);
// analogWrite(rgbGreenPin, greenValue);
// analogWrite(rgbBluePin, blueValue);
// }
// void controlRGBWithPot() {
// int potValue = analogRead(potPin);
// int redValue = map(potValue, 0, 4095, 0, 255); // Note: For ESP32, use 4095 as the max value
// int greenValue = map(potValue, 0, 4095, 0, 255);
// int blueValue = map(potValue, 0, 4095, 0, 255);
// analogWrite(rgbRedPin, redValue);
// analogWrite(rgbGreenPin, greenValue);
// analogWrite(rgbBluePin, blueValue);
// }
const int led1 = 2;
const int led2 = 4;
const int led3 = 16;
const int rgbRed = 12;
const int rgbGreen = 14;
const int rgbBlue = 27;
const int buttonPin = 5;
const int potPin = 35; // Potentiometer pin
int buttonState = 0;
int lastButtonState = 0;
int mode = 0;
void setup() {
Serial.begin(115200);
// Set up LED pins
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
// Set up RGB LED pins
pinMode(rgbRed, OUTPUT);
pinMode(rgbGreen, OUTPUT);
pinMode(rgbBlue, OUTPUT);
// Set up button pin
pinMode(buttonPin, INPUT_PULLUP);
// Set up potentiometer pin
pinMode(potPin, INPUT);
}
void loop() {
buttonState = digitalRead(buttonPin);
// Detect button press
if (buttonState == LOW && lastButtonState == HIGH) {
mode++;
Serial.print("Button press count: ");
Serial.println(mode);
if (mode > 3) mode = 1; // Reset mode after the third click
delay(200); // Debounce delay
}
lastButtonState = buttonState;
// Handle different modes
if (mode == 1) {
// Mode 1: Light 3 LEDs in sequence and then turn them off
digitalWrite(led1, HIGH);
delay(500);
digitalWrite(led1, LOW);
digitalWrite(led2, HIGH);
delay(500);
digitalWrite(led2, LOW);
digitalWrite(led3, HIGH);
delay(500);
digitalWrite(led3, LOW);
} else if (mode == 2) {
// Mode 2: Light all LEDs (RGB and individual LEDs) with random on/off states
// Randomize RGB LED colors
int redValue = random(0, 256);
int greenValue = random(0, 256);
int blueValue = random(0, 256);
// Set RGB LED
analogWrite(rgbRed, redValue);
analogWrite(rgbGreen, greenValue);
analogWrite(rgbBlue, blueValue);
// Optional: Add a delay to observe the change
delay(500);
analogWrite(rgbRed, 0);
analogWrite(rgbGreen, 0);
analogWrite(rgbBlue, 0);
} else if (mode == 3) {
// Mode 3: Control all LEDs with potentiometer
int potValue = analogRead(potPin);
int brightness = map(potValue, 0, 4095, 0, 255);
analogWrite(rgbRed, brightness);
analogWrite(rgbGreen, 255 - brightness);
analogWrite(rgbBlue, brightness / 2);
}
}
// // Define pins
// const int ledPins[] = {2, 4, 16};
// const int buttonPin = 5;
// const int rgbRedPin = 12;
// const int rgbGreenPin = 14;
// const int rgbBluePin = 27;
// const int potPin = 35;
/*
const int led1 = 2;
const int led2 = 4;
const int led3 = 16;
const int rgbRed = 12;
const int rgbGreen = 14;
const int rgbBlue = 27;
const int buttonPin = 5;
const int potPin = 35; // Potentiometer pin
int buttonState = 0;
int lastButtonState = 0;
int mode = 0;
void setup() {
Serial.begin(115200);
// Set up LED pins
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
// Set up RGB LED pins
pinMode(rgbRed, OUTPUT);
pinMode(rgbGreen, OUTPUT);
pinMode(rgbBlue, OUTPUT);
// Set up button pin
pinMode(buttonPin, INPUT_PULLUP);
// Set up potentiometer pin
pinMode(potPin, INPUT);
}
void loop() {
buttonState = digitalRead(buttonPin);
// Detect button press
if (buttonState == LOW && lastButtonState == HIGH) {
mode++;
Serial.print("Button press count: ");
Serial.println(mode);
if (mode > 3) mode = 1; // Reset mode after the third click
delay(200); // Debounce delay
}
lastButtonState = buttonState;
// Handle different modes
if (mode == 1) {
// Mode 1: Light 3 LEDs in sequence and then turn them off
digitalWrite(led1, HIGH);
delay(500);
digitalWrite(led1, LOW);
digitalWrite(led2, HIGH);
delay(500);
digitalWrite(led2, LOW);
digitalWrite(led3, HIGH);
delay(500);
digitalWrite(led3, LOW);
} else if (mode == 2) {
// Mode 2: Light all LEDs (RGB and individual LEDs) with random on/off states
// Randomize RGB LED colors
int redValue = random(0, 256);
int greenValue = random(0, 256);
int blueValue = random(0, 256);
// Set RGB LED
analogWrite(rgbRed, redValue);
analogWrite(rgbGreen, greenValue);
analogWrite(rgbBlue, blueValue);
// Randomize the state (on/off) of the 3 individual LEDs
digitalWrite(led1, random(0, 2) == 1 ? HIGH : LOW);
digitalWrite(led2, random(0, 2) == 1 ? HIGH : LOW);
digitalWrite(led3, random(0, 2) == 1 ? HIGH : LOW);
// Optional: Add a delay to observe the change
delay(500);
// Turn off the LEDs after a delay
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
analogWrite(rgbRed, 0);
analogWrite(rgbGreen, 0);
analogWrite(rgbBlue, 0);
} else if (mode == 3) {
// Mode 3: Control all LEDs with potentiometer
int potValue = analogRead(potPin);
int brightness = map(potValue, 0, 4095, 0, 255);
analogWrite(led1, brightness);
analogWrite(led2, brightness);
analogWrite(led3, brightness);
analogWrite(rgbRed, brightness);
analogWrite(rgbGreen, 255 - brightness);
analogWrite(rgbBlue, brightness / 2);
}
}*/