/*
Project: LED / Button Demo
Description: Mode button controls 4 LED effects:
On, Fade, Blink, and Chase.
Power button sets the LEDs on or off.
Creation date: 5/6/23
Author: AnonEngineering
Wokwi | Simulation
srii — 5/6/23 at 12:12 AM:
Membuat 5 LED lampu dengan 2 tombol button
matikan dan hidupkan tolong di bantu ka?
Make 5 LED lights with 2 buttons to
turn off and on, can you help, please?
*/
const int NUM_LEDS = 5;
const int FADE_INTERVAL = 100;
const int BLINK_INTERVAL = 500;
const int CHASE_INTERVAL = 250;
const int BTN1_PIN = 12;
const int BTN2_PIN = 11;
const int LED_PINS[NUM_LEDS] = {10, 9, 6, 5, 3};
const char* MODE_NAME[] = {"All on", "Fade", "Blink", "Chase"};
void ledsOff() {
for (int ledOff = 0; ledOff < NUM_LEDS; ledOff++) {
digitalWrite(LED_PINS[ledOff], LOW);
}
}
void ledsOn() {
for (int ledOn = 0; ledOn < NUM_LEDS; ledOn++) {
digitalWrite(LED_PINS[ledOn], HIGH);
}
}
void ledsFade() {
static unsigned long oldFade = 0;
static int fadeVal = 10;
static int fadeAdd = 0;
if (millis() - oldFade >= FADE_INTERVAL) {
oldFade = millis();
fadeVal += fadeAdd;
//Serial.println(fadeVal);
if (fadeVal >= 120) fadeAdd = -10;
if (fadeVal <= 10) fadeAdd = 10;
for (int led = 0; led < NUM_LEDS; led++) {
analogWrite(LED_PINS[led], fadeVal);
}
}
}
void ledsBlink() {
static unsigned long oldBlink = 0;
static bool blinkState = false;
if (millis() - oldBlink >= BLINK_INTERVAL) {
oldBlink = millis();
blinkState = !blinkState;
}
for (int ledOn = 0; ledOn < NUM_LEDS; ledOn++) {
digitalWrite(LED_PINS[ledOn], blinkState);
}
}
void ledsChase() {
static unsigned long oldChase = 0;
static int chaseNum = 0;
if (millis() - oldChase >= CHASE_INTERVAL) {
oldChase = millis();
ledsOff();
digitalWrite(LED_PINS[chaseNum], HIGH);
chaseNum++;
if (chaseNum >= NUM_LEDS) chaseNum = 0;
}
}
bool checkPowerBtn() {
static bool powerOn = false;
static int oldPowerState = true; // pullup
int powerState = digitalRead(BTN1_PIN);
if (powerState != oldPowerState) {
if (powerState == LOW) {
powerOn = !powerOn;
Serial.print("Power: ");
Serial.println(powerOn ? "On" : "Off");
}
delay(20); // debounce
}
oldPowerState = powerState;
return powerOn;
}
int setMode() {
static int mode = 0;
static int oldModeState = true; // pullup
int modeState = digitalRead(BTN2_PIN);
if (modeState != oldModeState) {
if (modeState == LOW) {
mode++;
if (mode >= 4) mode = 0;
Serial.print("Mode : ");
Serial.println(MODE_NAME[mode]);
}
delay(20); // debounce
}
oldModeState = modeState;
return mode;
}
void setup() {
Serial.begin(9600);
// set up the LED output pins
for (int pinNum = 0; pinNum < NUM_LEDS; pinNum++) {
pinMode(LED_PINS[pinNum], OUTPUT);
}
// set up the button input pins
pinMode(BTN1_PIN, INPUT_PULLUP);
pinMode(BTN2_PIN, INPUT_PULLUP);
// show start up state
Serial.println("Power: Off");
Serial.println("Mode : All on");
}
void loop() {
bool power = checkPowerBtn();
int mode = setMode();
if (power == true) {
if (mode == 0) { // All on
ledsOn();
} else if (mode == 1) { // Fade
ledsFade();
} else if (mode == 2) { // Blink
ledsBlink();
} else if (mode == 3) { // Chase
ledsChase();
}
} else {
ledsOff();
}
}