#include <Arduino.h>
// int redPin = 3;
// int greenPin = 5;
// int bluePin = 6;
// int potPin = A0;
// int buttonPin = 2; // Change this to the pin where your push button is connected
// int redLEDPin = 9; // Red LED pin
// int greenLEDPin = 10; // Green LED pin
// boolean buttonState = LOW;
// boolean lastButtonState = LOW;
// unsigned long lastDebounceTime = 0;
// unsigned long debounceDelay = 50; // Adjust as needed
// int redValue = 0;
// int greenValue = 0;
// int blueValue = 0;
// void setup() {
// pinMode(redPin, OUTPUT);
// pinMode(greenPin, OUTPUT);
// pinMode(bluePin, OUTPUT);
// pinMode(redLEDPin, OUTPUT); // Red LED pin as output
// pinMode(greenLEDPin, OUTPUT); // Green LED pin as output
// pinMode(buttonPin, INPUT_PULLUP); // Use internal pull-up resistor
// }
// void blinkLED(int times, int interval, int red, int green, int blue) {
// for (int i = 0; i < times; i++) {
// analogWrite(redPin, red);
// analogWrite(greenPin, green);
// analogWrite(bluePin, blue);
// delay(interval);
// analogWrite(redPin, 0);
// analogWrite(greenPin, 0);
// analogWrite(bluePin, 0);
// delay(interval);
// }
// }
// void loop() {
// int potValue = analogRead(potPin);
// // Calculate RGB values based on potentiometer
// if (potValue < 256) {
// redValue = 255 - potValue;
// greenValue = potValue;
// blueValue = 0;
// } else if (potValue < 512) {
// potValue -= 256;
// redValue = 0;
// greenValue = 255 - potValue;
// blueValue = potValue;
// } else if (potValue < 768) {
// potValue -= 512;
// redValue = potValue;
// greenValue = potValue;
// blueValue = potValue;
// } else {
// redValue = 255;
// greenValue = 255;
// blueValue = 255;
// }
// analogWrite(redPin, redValue);
// analogWrite(greenPin, greenValue);
// analogWrite(bluePin, blueValue);
// // Check if the push button is pressed
// int reading = digitalRead(buttonPin);
// if (reading != lastButtonState) {
// lastDebounceTime = millis();
// }
// if ((millis() - lastDebounceTime) > debounceDelay) {
// if (reading != buttonState) {
// buttonState = reading;
// if (buttonState == LOW) {
// // Button was pressed, make the RGB LED blink with the current color
// blinkLED(2, 500, redValue, greenValue, blueValue);
// digitalWrite(redLEDPin, LOW); // Turn off the red LED
// digitalWrite(greenLEDPin, HIGH); // Turn on the green LED
// }
// else {
// digitalWrite(redLEDPin, HIGH); // Turn on the red LED
// digitalWrite(greenLEDPin, LOW); // Turn off the green LED
// }
// }
// }
// lastButtonState = reading;
// }
int redPin = 3;
int greenPin = 5;
int bluePin = 6;
int potPin = A0;
int buttonPin = 2;
int redLEDPin = 9;
int greenLEDPin = 10;
int switchPin = 4; // New switch pin
boolean buttonState = LOW;
boolean lastButtonState = LOW;
boolean switchState = LOW; // New switch state
unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 50;
int redValue = 0;
int greenValue = 0;
int blueValue = 0;
void setup() {
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
pinMode(redLEDPin, OUTPUT);
pinMode(greenLEDPin, OUTPUT);
pinMode(buttonPin, INPUT_PULLUP);
pinMode(switchPin, INPUT_PULLUP); // Set switch pin as input with pull-up resistor
}
void blinkLED(int times, int interval, int red, int green, int blue) {
for (int i = 0; i < times; i++) {
analogWrite(redPin, red);
analogWrite(greenPin, green);
analogWrite(bluePin, blue);
delay(interval);
analogWrite(redPin, 0);
analogWrite(greenPin, 0);
analogWrite(bluePin, 0);
delay(interval);
}
}
void loop() {
switchState = digitalRead(switchPin); // Read the switch state
int potValue = analogRead(potPin);
if (switchState == LOW) { // If the switch is ON
if (potValue < 341) {
redValue = 255;
greenValue = 0;
blueValue = 0;
} else if (potValue < 683) {
redValue = 0;
greenValue = 255;
blueValue = 0;
} else {
redValue = 0;
greenValue = 0;
blueValue = 255;
}
} else { // If the switch is OFF
if (potValue < 256) {
redValue = 255 - potValue;
greenValue = potValue;
blueValue = 0;
} else if (potValue < 512) {
potValue -= 256;
redValue = 0;
greenValue = 255 - potValue;
blueValue = potValue;
} else if (potValue < 768) {
potValue -= 512;
redValue = potValue;
greenValue = 0;
blueValue = 255 - potValue;
} else {
redValue = 0;
greenValue = 0;
blueValue = 0;
}
}
analogWrite(redPin, redValue);
analogWrite(greenPin, greenValue);
analogWrite(bluePin, blueValue);
// Button debounce and action
int reading = digitalRead(buttonPin);
if (reading != lastButtonState) {
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
if (reading != buttonState) {
buttonState = reading;
if (buttonState == LOW) {
blinkLED(2, 500, redValue, greenValue, blueValue);
digitalWrite(redLEDPin, LOW);
digitalWrite(greenLEDPin, HIGH);
} else {
digitalWrite(redLEDPin, HIGH);
digitalWrite(greenLEDPin, LOW);
}
}
}
lastButtonState = reading;
}