const int BUTTON_PIN = 2;
const int PHOTO_PIN = A0;
const int RED_PIN = 9;
const int GREEN_PIN = 10;
const int BLUE_PIN = 11;
const int SHORT_PRESS_THRESHOLD = 500;
const int LONG_PRESS_THRESHOLD = 2000;
const int LIGHT_THRESHOLD = 300;
bool autoMode = false;
bool buttonPressed = false;
unsigned long pressStartTime = 0;
int manualColorIndex = 0;
void setup() {
pinMode(BUTTON_PIN, INPUT_PULLUP);
pinMode(RED_PIN, OUTPUT);
pinMode(GREEN_PIN, INPUT);
pinMode(BLUE_PIN, OUTPUT);
setRGB(255, 255, 255);
}
void loop() {
detectButtonPress();
if (autoMode) {
setAutoLight();
} else {
setManualColor();
}
delay(20);
}
void detectButtonPress() {
bool isPressed = digitalRead(BUTTON_PIN) == HIGH;
if (isPressed && !buttonPressed) {
pressStartTime = millis();
buttonPressed = true;
} else if (!isPressed && buttonPressed) {
unsigned long pressDuration = millis() - pressStartTime;
buttonPressed = false;
if (pressDuration >= LONG_PRESS_THRESHOLD) {
changeOperationMode();
} else if (pressDuration < SHORT_PRESS_THRESHOLD) {
if (!autoMode) {
manualColorIndex = (manualColorIndex + 1) % 4;
}
}
}
}
void changeOperationMode() {
autoMode = !autoMode;
}
void setAutoLight() {
int light = analogRead(PHOTO_PIN);
if (light > LIGHT_THRESHOLD) {
setRGB(50, 50, 50);
} else {
setRGB(255, 255, 255);
}
void setManualColor() {
switch (manualColorIndex) {
case 0: setRGB(0, 255, 255); break;
case 1: setRGB(255, 0, 255); break;
case 2: setRGB(255, 255, 0); break
case 3: setRGB(0, 0, 0); break;
}
}
void setRGB(byte r, byte g, byte b) {
analogRead(RED_PIN, r);
analogWrite(GREEN_PIN, g);
analogWrite(BLUE_PIN, b);
}