Advertisement
pleasedontcode

Button Cycle rev_05

Dec 9th, 2023
87
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 19:34:15
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* rgb led with 2 buttons the buttons run 3 colors */
  21.     /* green yellow red respectively button 1 goes up the */
  22.     /* colors button 2 goes down the colors and button */
  23.     /* one stops at red and button 2 stops at green */
  24. /****** SYSTEM REQUIREMENT 2 *****/
  25.     /* each button must watch the other to change colors */
  26.     /* accordingly */
  27. /****** END SYSTEM REQUIREMENTS *****/
  28.  
  29. /****** DEFINITION OF LIBRARIES *****/
  30. #include <Arduino.h>
  31. #include <EasyButton.h>
  32.  
  33. /****** FUNCTION PROTOTYPES *****/
  34. void setup(void);
  35. void loop(void);
  36. void changeColor(int colorIndex);
  37. void button1Pressed();
  38. void button2Pressed();
  39.  
  40. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  41. const uint8_t button1_PushButton_PIN_D2 = 2;
  42. const uint8_t button2_PushButton_PIN_D3 = 3;
  43.  
  44. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  45. const uint8_t led_LEDRGB_Red_PIN_D4 = 4;
  46. const uint8_t led_LEDRGB_Green_PIN_D5 = 5;
  47. const uint8_t led_LEDRGB_Blue_PIN_D6 = 6;
  48.  
  49. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  50. EasyButton button1(button1_PushButton_PIN_D2);
  51. EasyButton button2(button2_PushButton_PIN_D3);
  52.  
  53. // Define the RGB colors
  54. const int colors[][3] = {
  55.     {0, 255, 0},   // Green
  56.     {255, 255, 0}, // Yellow
  57.     {255, 0, 0}    // Red
  58. };
  59.  
  60. int currentColorIndex = 0;
  61.  
  62. void setup(void)
  63. {
  64.     // put your setup code here, to run once:
  65.     pinMode(button1_PushButton_PIN_D2, INPUT_PULLUP);
  66.     pinMode(button2_PushButton_PIN_D3, INPUT_PULLUP);
  67.  
  68.     pinMode(led_LEDRGB_Red_PIN_D4, OUTPUT);
  69.     pinMode(led_LEDRGB_Green_PIN_D5, OUTPUT);
  70.     pinMode(led_LEDRGB_Blue_PIN_D6, OUTPUT);
  71.  
  72.     // Set initial color
  73.     changeColor(currentColorIndex);
  74.  
  75.     // Attach button press event handlers
  76.     button1.onPressed(button1Pressed);
  77.     button2.onPressed(button2Pressed);
  78. }
  79.  
  80. void loop(void)
  81. {
  82.     // put your main code here, to run repeatedly:
  83.     button1.read();
  84.     button2.read();
  85. }
  86.  
  87. void changeColor(int colorIndex)
  88. {
  89.     analogWrite(led_LEDRGB_Red_PIN_D4, colors[colorIndex][0]);
  90.     analogWrite(led_LEDRGB_Green_PIN_D5, colors[colorIndex][1]);
  91.     analogWrite(led_LEDRGB_Blue_PIN_D6, colors[colorIndex][2]);
  92. }
  93.  
  94. void button1Pressed()
  95. {
  96.     if (currentColorIndex < 2)
  97.     {
  98.         currentColorIndex++;
  99.     }
  100.     changeColor(currentColorIndex);
  101. }
  102.  
  103. void button2Pressed()
  104. {
  105.     if (currentColorIndex > 0)
  106.     {
  107.         currentColorIndex--;
  108.     }
  109.     changeColor(currentColorIndex);
  110. }
  111.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement