const int pinLedR = 17, pinLedY = 18, pinLedG = 19;
const int pinLedR1 = 13, pinLedG1 = 12, pinLedB1 = 11, pinBtnR = 10;
const int pinLedR2 = 7, pinLedG2 = 6, pinLedB2 = 5, pinBtnY = 4;
const int pinLedR3 = 3, pinLedG3 = 2, pinLedB3 = 9, pinBtnG = 8;
const int longPressDuration = 3000; // 3 seconds for a long press
unsigned long buttonPressTime = 0;
void setup() {
pinMode(pinLedR, OUTPUT);
pinMode(pinLedY, OUTPUT);
pinMode(pinLedG, OUTPUT);
pinMode(pinLedR1, OUTPUT);
pinMode(pinLedG1, OUTPUT);
pinMode(pinLedB1, OUTPUT);
pinMode(pinLedR2, OUTPUT);
pinMode(pinLedG2, OUTPUT);
pinMode(pinLedB2, OUTPUT);
pinMode(pinLedR3, OUTPUT);
pinMode(pinLedG3, OUTPUT);
pinMode(pinLedB3, OUTPUT);
pinMode(pinBtnR, INPUT_PULLUP);
pinMode(pinBtnY, INPUT_PULLUP);
pinMode(pinBtnG, INPUT_PULLUP);
Serial.begin(9600);
}
void loop() {
// membaca keadaan tombol
if (digitalRead(pinBtnG) == LOW) {
// Long press on green button turns off all LEDs
if (millis() - buttonPressTime >= longPressDuration) {
illuminateRGB(0, 0, 0); // Turn off all LEDs
digitalWrite(pinLedR, LOW);
digitalWrite(pinLedY, LOW);
digitalWrite(pinLedG, LOW);
} else {
illuminateRGB(0, 255, 0); // Green
digitalWrite(pinLedR, LOW);
digitalWrite(pinLedY, LOW);
digitalWrite(pinLedG, HIGH);
}
} else {
if (digitalRead(pinBtnR) == LOW) {
illuminateRGB(255, 0, 0); // Red
digitalWrite(pinLedR, HIGH);
digitalWrite(pinLedY, LOW);
digitalWrite(pinLedG, LOW);
} else if (digitalRead(pinBtnY) == LOW) {
illuminateRGB(255, 255, 0); // Yellow
digitalWrite(pinLedR, LOW);
digitalWrite(pinLedY, HIGH);
digitalWrite(pinLedG, LOW);
}
}
delay(10);
}
void illuminateRGB(int red, int green, int blue) {
analogWrite(pinLedR1, red);
analogWrite(pinLedG1, green);
analogWrite(pinLedB1, blue);
analogWrite(pinLedR2, red);
analogWrite(pinLedG2, green);
analogWrite(pinLedB2, blue);
analogWrite(pinLedR3, red);
analogWrite(pinLedG3, green);
analogWrite(pinLedB3, blue);
// Update button press time when any button is pressed
if (red == 255 || green == 255 || blue == 255) {
buttonPressTime = millis();
}
}