Advertisement
pleasedontcode

LED Signaling rev_02

Sep 14th, 2024
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: LED Signaling
  13.     - Source Code NOT compiled for: Arduino Nano
  14.     - Source Code created on: 2024-09-15 01:10:46
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* buat animasi flowing  warna orange sein kiri kanan */
  21.     /* dan hazard dengan memakai input D2 D3 D4 sebagai */
  22.     /* trigger untuk aktivasi dengan menggunakan ledstrip */
  23.     /* ws2812b sebanyak 24 led */
  24. /****** END SYSTEM REQUIREMENTS *****/
  25.  
  26. /****** DEFINITION OF LIBRARIES *****/
  27. #include <EasyButton.h>    // https://github.com/evert-arias/EasyButton
  28. #include <FastLED.h>       // https://github.com/FastLED/FastLED
  29.  
  30. /****** FUNCTION PROTOTYPES *****/
  31. void setup(void);
  32. void loop(void);
  33.  
  34. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  35. const uint8_t tombolkiri_PushButton_PIN_D2        = 2; // Left button pin
  36. const uint8_t tombolkanan_PushButton_PIN_D3       = 3; // Right button pin
  37. const uint8_t tombolhazard_PushButton_PIN_D4       = 4; // Hazard button pin
  38.  
  39. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  40. // Create instances of EasyButton for each button
  41. EasyButton tombolkiriButton(tombolkiri_PushButton_PIN_D2);
  42. EasyButton tombolkananButton(tombolkanan_PushButton_PIN_D3);
  43. EasyButton tombolhazardButton(tombolhazard_PushButton_PIN_D4);
  44.  
  45. // Define LED strip parameters
  46. #define NUM_LEDS 24       // Number of LEDs in the strip
  47. #define LED_PIN 5        // Pin where the LED strip is connected
  48. CRGB leds[NUM_LEDS];     // Array to hold LED color values
  49.  
  50. /****** SETUP FUNCTION *****/
  51. void setup(void)
  52. {
  53.     // Initialize Serial for debugging purposes
  54.     Serial.begin(115200);
  55.     Serial.println("Starting setup...");
  56.  
  57.     // Set button pins as input with pull-up resistors
  58.     pinMode(tombolkiri_PushButton_PIN_D2, INPUT_PULLUP);
  59.     pinMode(tombolkanan_PushButton_PIN_D3, INPUT_PULLUP);
  60.     pinMode(tombolhazard_PushButton_PIN_D4, INPUT_PULLUP);
  61.  
  62.     // Initialize each button
  63.     tombolkiriButton.begin();
  64.     tombolkananButton.begin();
  65.     tombolhazardButton.begin();
  66.  
  67.     // Add callback functions for button presses
  68.     tombolkiriButton.onPressed([]() {
  69.         Serial.println("Tombol Kiri has been pressed");
  70.     });
  71.     tombolkananButton.onPressed([]() {
  72.         Serial.println("Tombol Kanan has been pressed");
  73.     });
  74.     tombolhazardButton.onPressed([]() {
  75.         Serial.println("Tombol Hazard has been pressed");
  76.     });
  77.  
  78.     // Initialize the LED strip
  79.     FastLED.addLeds<WS2812, LED_PIN, RGB>(leds, NUM_LEDS); // Initialize the LED strip
  80.     FastLED.setBrightness(84); // Set brightness of the LEDs
  81. }
  82.  
  83. /****** LOOP FUNCTION *****/
  84. void loop(void)
  85. {
  86.     // Continuously read the status of the buttons
  87.     tombolkiriButton.read();
  88.     tombolkananButton.read();
  89.     tombolhazardButton.read();
  90.    
  91.     // Check if left button is pressed
  92.     if (tombolkiriButton.isPressed()) {
  93.         // Animate flowing orange color for left turn signal
  94.         for (int i = 0; i < NUM_LEDS; i++) {
  95.             leds[i] = CRGB::Orange; // Set LED color to orange
  96.             FastLED.show(); // Update the LED strip
  97.             delay(50); // Delay for animation effect
  98.             leds[i] = CRGB::Black; // Turn off the LED after delay
  99.         }
  100.     }
  101.  
  102.     // Check if right button is pressed
  103.     if (tombolkananButton.isPressed()) {
  104.         // Animate flowing orange color for right turn signal
  105.         for (int i = NUM_LEDS - 1; i >= 0; i--) {
  106.             leds[i] = CRGB::Orange; // Set LED color to orange
  107.             FastLED.show(); // Update the LED strip
  108.             delay(50); // Delay for animation effect
  109.             leds[i] = CRGB::Black; // Turn off the LED after delay
  110.         }
  111.     }
  112.  
  113.     // Check if hazard button is pressed
  114.     if (tombolhazardButton.isPressed()) {
  115.         // Activate hazard lights (all LEDs blinking orange)
  116.         for (int i = 0; i < 5; i++) { // Blink 5 times
  117.             for (int j = 0; j < NUM_LEDS; j++) {
  118.                 leds[j] = CRGB::Orange; // Set all LEDs to orange
  119.             }
  120.             FastLED.show(); // Update the LED strip
  121.             delay(500); // Keep on for half a second
  122.             for (int j = 0; j < NUM_LEDS; j++) {
  123.                 leds[j] = CRGB::Black; // Turn off all LEDs
  124.             }
  125.             FastLED.show(); // Update the LED strip
  126.             delay(500); // Keep off for half a second
  127.         }
  128.     }
  129.    
  130.     // Add a small delay to prevent button bouncing issues
  131.     delay(10);
  132. }
  133.  
  134. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement