Advertisement
pleasedontcode

"RGB Control" rev_02

Dec 9th, 2023
78
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: "RGB Control"
  13.     - Source Code compiled for: Arduino Uno
  14.     - Source Code created on: 2023-12-09 18:59:22
  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.     /* the buttons can only go up or down the colors and */
  24.     /* nothing else */
  25. /****** END SYSTEM REQUIREMENTS *****/
  26.  
  27. /****** DEFINITION OF LIBRARIES *****/
  28. #include <Arduino.h>
  29. #include <EasyButton.h>
  30.  
  31. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  32. const uint8_t button1_PushButton_PIN_D2 = 2;
  33. const uint8_t button2_PushButton_PIN_D3 = 3;
  34.  
  35. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  36. const uint8_t led_LEDRGB_Red_PIN_D4 = 4;
  37. const uint8_t led_LEDRGB_Green_PIN_D5 = 5;
  38. const uint8_t led_LEDRGB_Blue_PIN_D6 = 6;
  39.  
  40. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  41. EasyButton button1(button1_PushButton_PIN_D2);
  42. EasyButton button2(button2_PushButton_PIN_D3);
  43.  
  44. enum RGBColor
  45. {
  46.   GREEN,
  47.   YELLOW,
  48.   RED
  49. };
  50.  
  51. RGBColor currentColor = GREEN;
  52.  
  53. void setup(void)
  54. {
  55.   // put your setup code here, to run once:
  56.  
  57.   pinMode(button1_PushButton_PIN_D2, INPUT_PULLUP);
  58.   pinMode(button2_PushButton_PIN_D3, INPUT_PULLUP);
  59.  
  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.  
  65. void changeColorUp()
  66. {
  67.   if (currentColor == GREEN)
  68.   {
  69.     currentColor = YELLOW;
  70.   }
  71.   else if (currentColor == YELLOW)
  72.   {
  73.     currentColor = RED;
  74.   }
  75.   else if (currentColor == RED)
  76.   {
  77.     currentColor = GREEN;
  78.   }
  79. }
  80.  
  81. void changeColorDown()
  82. {
  83.   if (currentColor == GREEN)
  84.   {
  85.     currentColor = RED;
  86.   }
  87.   else if (currentColor == YELLOW)
  88.   {
  89.     currentColor = GREEN;
  90.   }
  91.   else if (currentColor == RED)
  92.   {
  93.     currentColor = YELLOW;
  94.   }
  95. }
  96.  
  97. void updateLEDColor()
  98. {
  99.   if (currentColor == GREEN)
  100.   {
  101.     digitalWrite(led_LEDRGB_Red_PIN_D4, LOW);
  102.     digitalWrite(led_LEDRGB_Green_PIN_D5, HIGH);
  103.     digitalWrite(led_LEDRGB_Blue_PIN_D6, LOW);
  104.   }
  105.   else if (currentColor == YELLOW)
  106.   {
  107.     digitalWrite(led_LEDRGB_Red_PIN_D4, HIGH);
  108.     digitalWrite(led_LEDRGB_Green_PIN_D5, HIGH);
  109.     digitalWrite(led_LEDRGB_Blue_PIN_D6, LOW);
  110.   }
  111.   else if (currentColor == RED)
  112.   {
  113.     digitalWrite(led_LEDRGB_Red_PIN_D4, HIGH);
  114.     digitalWrite(led_LEDRGB_Green_PIN_D5, LOW);
  115.     digitalWrite(led_LEDRGB_Blue_PIN_D6, LOW);
  116.   }
  117. }
  118.  
  119. void loop(void)
  120. {
  121.   // put your main code here, to run repeatedly:
  122.  
  123.   button1.read();
  124.   button2.read();
  125.  
  126.   if (button1.wasPressed())
  127.   {
  128.     changeColorUp();
  129.   }
  130.   else if (button2.wasPressed())
  131.   {
  132.     changeColorDown();
  133.   }
  134.  
  135.   updateLEDColor();
  136. }
  137.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement