Advertisement
pleasedontcode

Button Sequencer rev_04

Dec 9th, 2023
76
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: Button Sequencer
  13.     - Source Code compiled for: Arduino Uno
  14.     - Source Code created on: 2023-12-09 19:21:49
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* rgb led with 2 buttons button 1 goes from off to */
  21.     /* green to yellow to red then stops button 2 runs */
  22.     /* from red to yellow to green to off and stops */
  23. /****** END SYSTEM REQUIREMENTS *****/
  24.  
  25. /****** DEFINITION OF LIBRARIES *****/
  26. #include <Arduino.h>
  27. #include <EasyButton.h>
  28.  
  29. /****** FUNCTION PROTOTYPES *****/
  30. void setup(void);
  31. void loop(void);
  32.  
  33. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  34. const uint8_t button1_PushButton_PIN_D2 = 2;
  35. const uint8_t button2_PushButton_PIN_D3 = 3;
  36.  
  37. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  38. const uint8_t led_LEDRGB_Red_PIN_D4 = 4;
  39. const uint8_t led_LEDRGB_Green_PIN_D5 = 5;
  40. const uint8_t led_LEDRGB_Blue_PIN_D6 = 6;
  41.  
  42. // Button states
  43. enum ButtonState {
  44.   OFF,
  45.   GREEN,
  46.   YELLOW,
  47.   RED
  48. };
  49.  
  50. /****** DEFINITION OF LIBRARY CLASS INSTANCES *****/
  51. EasyButton button1(button1_PushButton_PIN_D2);
  52. EasyButton button2(button2_PushButton_PIN_D3);
  53.  
  54. ButtonState button1State = OFF;
  55. ButtonState button2State = RED;
  56.  
  57. void setup(void)
  58. {
  59.   // put your setup code here, to run once:
  60.   pinMode(led_LEDRGB_Red_PIN_D4, OUTPUT);
  61.   pinMode(led_LEDRGB_Green_PIN_D5, OUTPUT);
  62.   pinMode(led_LEDRGB_Blue_PIN_D6, OUTPUT);
  63.  
  64.   button1.begin();
  65.   button2.begin();
  66. }
  67.  
  68. void loop(void)
  69. {
  70.   // put your main code here, to run repeatedly:
  71.   button1.read();
  72.   button2.read();
  73.  
  74.   // Button 1 sequence
  75.   if (button1.wasPressed()) {
  76.     switch (button1State) {
  77.       case OFF:
  78.         digitalWrite(led_LEDRGB_Green_PIN_D5, HIGH);
  79.         digitalWrite(led_LEDRGB_Blue_PIN_D6, LOW);
  80.         digitalWrite(led_LEDRGB_Red_PIN_D4, LOW);
  81.         button1State = GREEN;
  82.         break;
  83.       case GREEN:
  84.         digitalWrite(led_LEDRGB_Green_PIN_D5, LOW);
  85.         digitalWrite(led_LEDRGB_Blue_PIN_D6, HIGH);
  86.         digitalWrite(led_LEDRGB_Red_PIN_D4, LOW);
  87.         button1State = YELLOW;
  88.         break;
  89.       case YELLOW:
  90.         digitalWrite(led_LEDRGB_Green_PIN_D5, LOW);
  91.         digitalWrite(led_LEDRGB_Blue_PIN_D6, LOW);
  92.         digitalWrite(led_LEDRGB_Red_PIN_D4, HIGH);
  93.         button1State = RED;
  94.         break;
  95.       case RED:
  96.         digitalWrite(led_LEDRGB_Green_PIN_D5, LOW);
  97.         digitalWrite(led_LEDRGB_Blue_PIN_D6, LOW);
  98.         digitalWrite(led_LEDRGB_Red_PIN_D4, LOW);
  99.         button1State = OFF;
  100.         break;
  101.     }
  102.   }
  103.  
  104.   // Button 2 sequence
  105.   if (button2.wasPressed()) {
  106.     switch (button2State) {
  107.       case RED:
  108.         digitalWrite(led_LEDRGB_Green_PIN_D5, LOW);
  109.         digitalWrite(led_LEDRGB_Blue_PIN_D6, LOW);
  110.         digitalWrite(led_LEDRGB_Red_PIN_D4, HIGH);
  111.         button2State = YELLOW;
  112.         break;
  113.       case YELLOW:
  114.         digitalWrite(led_LEDRGB_Green_PIN_D5, LOW);
  115.         digitalWrite(led_LEDRGB_Blue_PIN_D6, HIGH);
  116.         digitalWrite(led_LEDRGB_Red_PIN_D4, LOW);
  117.         button2State = GREEN;
  118.         break;
  119.       case GREEN:
  120.         digitalWrite(led_LEDRGB_Green_PIN_D5, HIGH);
  121.         digitalWrite(led_LEDRGB_Blue_PIN_D6, LOW);
  122.         digitalWrite(led_LEDRGB_Red_PIN_D4, LOW);
  123.         button2State = OFF;
  124.         break;
  125.       case OFF:
  126.         digitalWrite(led_LEDRGB_Green_PIN_D5, LOW);
  127.         digitalWrite(led_LEDRGB_Blue_PIN_D6, LOW);
  128.         digitalWrite(led_LEDRGB_Red_PIN_D4, LOW);
  129.         button2State = RED;
  130.         break;
  131.     }
  132.   }
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement