Advertisement
pleasedontcode

LED Control rev_01

Sep 14th, 2024
50
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 Control
  13.     - Source Code NOT compiled for: Arduino Nano
  14.     - Source Code created on: 2024-09-15 01:07:05
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* buat animasi sein kiri kanan dan hazard dengan */
  21.     /* memakai input D2 D3 D4 sebagai trigger untuk */
  22.     /* aktivasi dengan menggunakan ledstrip ws2812b */
  23.     /* 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 signal button
  36. const uint8_t tombolkanan_PushButton_PIN_D3     = 3; // Right signal button
  37. const uint8_t tombolhazard_PushButton_PIN_D4        = 4; // Hazard button
  38.  
  39. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  40. // Instantiate EasyButton objects 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 the number of LEDs and the data pin for FastLED
  46. #define NUM_LEDS 24 // Set to 24 LEDs as per the requirement
  47. #define DATA_PIN 5
  48.  
  49. // Instantiate the LED array
  50. CRGB leds[NUM_LEDS];
  51.  
  52. /****** FUNCTION DEFINITIONS *****/
  53. void setup(void)
  54. {
  55.     // Initialize serial communication for debugging
  56.     Serial.begin(115200);
  57.    
  58.     // Set button pins as input with pull-up resistors
  59.     pinMode(tombolkiri_PushButton_PIN_D2, INPUT_PULLUP);
  60.     pinMode(tombolkanan_PushButton_PIN_D3, INPUT_PULLUP);
  61.     pinMode(tombolhazard_PushButton_PIN_D4, INPUT_PULLUP);
  62.  
  63.     // Initialize the EasyButton instances
  64.     tombolkiriButton.begin();
  65.     tombolkananButton.begin();
  66.     tombolhazardButton.begin();
  67.  
  68.     // Initialize the FastLED library
  69.     FastLED.addLeds<WS2812B, DATA_PIN, RGB>(leds, NUM_LEDS); // Initialize the LED strip
  70.     FastLED.setBrightness(84); // Set the brightness of the LEDs
  71. }
  72.  
  73. void loop(void)
  74. {
  75.     // Read the state of the buttons
  76.     tombolkiriButton.read();
  77.     tombolkananButton.read();
  78.     tombolhazardButton.read();
  79.  
  80.     // Check for left signal button press
  81.     if (tombolkiriButton.isPressed()) {
  82.         for (int i = 0; i < NUM_LEDS; i++) {
  83.             leds[i] = CRGB::Red; // Set all LEDs to red for left signal
  84.         }
  85.         FastLED.show(); // Update the LED strip
  86.         delay(500); // Delay to simulate blinking effect
  87.         for (int i = 0; i < NUM_LEDS; i++) {
  88.             leds[i] = CRGB::Black; // Turn off LEDs
  89.         }
  90.         FastLED.show(); // Update the LED strip
  91.         delay(500); // Delay to simulate blinking effect
  92.     }
  93.  
  94.     // Check for right signal button press
  95.     else if (tombolkananButton.isPressed()) {
  96.         for (int i = 0; i < NUM_LEDS; i++) {
  97.             leds[i] = CRGB::Green; // Set all LEDs to green for right signal
  98.         }
  99.         FastLED.show(); // Update the LED strip
  100.         delay(500); // Delay to simulate blinking effect
  101.         for (int i = 0; i < NUM_LEDS; i++) {
  102.             leds[i] = CRGB::Black; // Turn off LEDs
  103.         }
  104.         FastLED.show(); // Update the LED strip
  105.         delay(500); // Delay to simulate blinking effect
  106.     }
  107.  
  108.     // Check for hazard button press
  109.     else if (tombolhazardButton.isPressed()) {
  110.         for (int i = 0; i < NUM_LEDS; i++) {
  111.             leds[i] = CRGB::Blue; // Set all LEDs to blue for hazard
  112.         }
  113.         FastLED.show(); // Update the LED strip
  114.         delay(500); // Delay to simulate blinking effect
  115.         for (int i = 0; i < NUM_LEDS; i++) {
  116.             leds[i] = CRGB::Black; // Turn off LEDs
  117.         }
  118.         FastLED.show(); // Update the LED strip
  119.         delay(500); // Delay to simulate blinking effect
  120.     }
  121. }
  122.  
  123. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement