CCCPaste

LED Signaling rev_02

/********* Pleasedontcode.com **********

	Pleasedontcode thanks you for automatic code generation! Enjoy your code!

	- Terms and Conditions:
	You have a non-exclusive, revocable, worldwide, royalty-free license
	for personal and commercial use. Attribution is optional; modifications
	are allowed, but you're responsible for code maintenance. We're not
	liable for any loss or damage. For full terms,
	please visit pleasedontcode.com/termsandconditions.

	- Project: LED Signaling
	- Source Code NOT compiled for: Arduino Nano
	- Source Code created on: 2024-09-15 01:10:46

********* Pleasedontcode.com **********/

/****** SYSTEM REQUIREMENTS *****/
/****** SYSTEM REQUIREMENT 1 *****/
	/* buat animasi flowing  warna orange sein kiri kanan */
	/* dan hazard dengan memakai input D2 D3 D4 sebagai */
	/* trigger untuk aktivasi dengan menggunakan ledstrip */
	/* ws2812b sebanyak 24 led */
/****** END SYSTEM REQUIREMENTS *****/

/****** DEFINITION OF LIBRARIES *****/
#include     // https://github.com/evert-arias/EasyButton
#include        // https://github.com/FastLED/FastLED

/****** FUNCTION PROTOTYPES *****/
void setup(void);
void loop(void);

/***** DEFINITION OF DIGITAL INPUT PINS *****/
const uint8_t tombolkiri_PushButton_PIN_D2        = 2; // Left button pin
const uint8_t tombolkanan_PushButton_PIN_D3       = 3; // Right button pin
const uint8_t tombolhazard_PushButton_PIN_D4       = 4; // Hazard button pin

/****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
// Create instances of EasyButton for each button
EasyButton tombolkiriButton(tombolkiri_PushButton_PIN_D2);
EasyButton tombolkananButton(tombolkanan_PushButton_PIN_D3);
EasyButton tombolhazardButton(tombolhazard_PushButton_PIN_D4);

// Define LED strip parameters
#define NUM_LEDS 24       // Number of LEDs in the strip
#define LED_PIN 5        // Pin where the LED strip is connected
CRGB leds[NUM_LEDS];     // Array to hold LED color values

/****** SETUP FUNCTION *****/
void setup(void)
{
    // Initialize Serial for debugging purposes
    Serial.begin(115200);
    Serial.println("Starting setup...");

    // Set button pins as input with pull-up resistors
    pinMode(tombolkiri_PushButton_PIN_D2, INPUT_PULLUP);
    pinMode(tombolkanan_PushButton_PIN_D3, INPUT_PULLUP);
    pinMode(tombolhazard_PushButton_PIN_D4, INPUT_PULLUP);

    // Initialize each button
    tombolkiriButton.begin();
    tombolkananButton.begin();
    tombolhazardButton.begin();

    // Add callback functions for button presses
    tombolkiriButton.onPressed([]() {
        Serial.println("Tombol Kiri has been pressed");
    });
    tombolkananButton.onPressed([]() {
        Serial.println("Tombol Kanan has been pressed");
    });
    tombolhazardButton.onPressed([]() {
        Serial.println("Tombol Hazard has been pressed");
    });

    // Initialize the LED strip
    FastLED.addLeds(leds, NUM_LEDS); // Initialize the LED strip
    FastLED.setBrightness(84); // Set brightness of the LEDs
}

/****** LOOP FUNCTION *****/
void loop(void)
{
    // Continuously read the status of the buttons
    tombolkiriButton.read();
    tombolkananButton.read();
    tombolhazardButton.read();
    
    // Check if left button is pressed
    if (tombolkiriButton.isPressed()) {
        // Animate flowing orange color for left turn signal
        for (int i = 0; i < NUM_LEDS; i++) {
            leds[i] = CRGB::Orange; // Set LED color to orange
            FastLED.show(); // Update the LED strip
            delay(50); // Delay for animation effect
            leds[i] = CRGB::Black; // Turn off the LED after delay
        }
    }

    // Check if right button is pressed
    if (tombolkananButton.isPressed()) {
        // Animate flowing orange color for right turn signal
        for (int i = NUM_LEDS - 1; i >= 0; i--) {
            leds[i] = CRGB::Orange; // Set LED color to orange
            FastLED.show(); // Update the LED strip
            delay(50); // Delay for animation effect
            leds[i] = CRGB::Black; // Turn off the LED after delay
        }
    }

    // Check if hazard button is pressed
    if (tombolhazardButton.isPressed()) {
        // Activate hazard lights (all LEDs blinking orange)
        for (int i = 0; i < 5; i++) { // Blink 5 times
            for (int j = 0; j < NUM_LEDS; j++) {
                leds[j] = CRGB::Orange; // Set all LEDs to orange
            }
            FastLED.show(); // Update the LED strip
            delay(500); // Keep on for half a second
            for (int j = 0; j < NUM_LEDS; j++) {
                leds[j] = CRGB::Black; // Turn off all LEDs
            }
            FastLED.show(); // Update the LED strip
            delay(500); // Keep off for half a second
        }
    }
    
    // Add a small delay to prevent button bouncing issues
    delay(10);
}

/* END CODE */