Advertisement
pleasedontcode

Button Cycle rev_01

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 Cycle
  13.     - Source Code compiled for: Arduino Uno
  14.     - Source Code created on: 2023-12-09 18:37:21
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* RUN A RGB LED USING 2 BUTTONS the buttons run 3 */
  21.     /* colors green yellow red consecutively button 1 */
  22.     /* changes colors up and button 2 changes colors down */
  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. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  43. EasyButton button1(button1_PushButton_PIN_D2);
  44. EasyButton button2(button2_PushButton_PIN_D3);
  45.  
  46. uint8_t currentColor = 0;
  47.  
  48. void setup(void)
  49. {
  50.   // put your setup code here, to run once:
  51.  
  52.   pinMode(button1_PushButton_PIN_D2, INPUT_PULLUP);
  53.   pinMode(button2_PushButton_PIN_D3, INPUT_PULLUP);
  54.  
  55.   pinMode(led_LEDRGB_Red_PIN_D4, OUTPUT);
  56.   pinMode(led_LEDRGB_Green_PIN_D5, OUTPUT);
  57.   pinMode(led_LEDRGB_Blue_PIN_D6, OUTPUT);
  58.  
  59. }
  60.  
  61. void loop(void)
  62. {
  63.   // put your main code here, to run repeatedly:
  64.  
  65.   button1.read();
  66.   button2.read();
  67.  
  68.   // Change color up when button 1 is pressed
  69.   if (button1.isPressed()) {
  70.     currentColor++;
  71.     if (currentColor > 2) {
  72.       currentColor = 0;
  73.     }
  74.   }
  75.  
  76.   // Change color down when button 2 is pressed
  77.   if (button2.isPressed()) {
  78.     if (currentColor == 0) {
  79.       currentColor = 2;
  80.     } else {
  81.       currentColor--;
  82.     }
  83.   }
  84.  
  85.   // Display the current color
  86.   switch (currentColor) {
  87.     case 0:
  88.       digitalWrite(led_LEDRGB_Red_PIN_D4, HIGH);
  89.       digitalWrite(led_LEDRGB_Green_PIN_D5, LOW);
  90.       digitalWrite(led_LEDRGB_Blue_PIN_D6, LOW);
  91.       break;
  92.     case 1:
  93.       digitalWrite(led_LEDRGB_Red_PIN_D4, LOW);
  94.       digitalWrite(led_LEDRGB_Green_PIN_D5, HIGH);
  95.       digitalWrite(led_LEDRGB_Blue_PIN_D6, LOW);
  96.       break;
  97.     case 2:
  98.       digitalWrite(led_LEDRGB_Red_PIN_D4, LOW);
  99.       digitalWrite(led_LEDRGB_Green_PIN_D5, LOW);
  100.       digitalWrite(led_LEDRGB_Blue_PIN_D6, HIGH);
  101.       break;
  102.   }
  103. }
  104.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement