Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /********* Pleasedontcode.com **********
- Pleasedontcode thanks you for automatic code generation! Enjoy your code!
- - Terms and Conditions:
- You have a non-exclusive, revocable, worldwide, royalty-free license
- for personal and commercial use. Attribution is optional; modifications
- are allowed, but you're responsible for code maintenance. We're not
- liable for any loss or damage. For full terms,
- please visit pleasedontcode.com/termsandconditions.
- - Project: Button Cycle
- - Source Code compiled for: Arduino Uno
- - Source Code created on: 2023-12-09 19:34:15
- ********* Pleasedontcode.com **********/
- /****** SYSTEM REQUIREMENTS *****/
- /****** SYSTEM REQUIREMENT 1 *****/
- /* rgb led with 2 buttons the buttons run 3 colors */
- /* green yellow red respectively button 1 goes up the */
- /* colors button 2 goes down the colors and button */
- /* one stops at red and button 2 stops at green */
- /****** SYSTEM REQUIREMENT 2 *****/
- /* each button must watch the other to change colors */
- /* accordingly */
- /****** END SYSTEM REQUIREMENTS *****/
- /****** DEFINITION OF LIBRARIES *****/
- #include <Arduino.h>
- #include <EasyButton.h>
- /****** FUNCTION PROTOTYPES *****/
- void setup(void);
- void loop(void);
- void changeColor(int colorIndex);
- void button1Pressed();
- void button2Pressed();
- /***** DEFINITION OF DIGITAL INPUT PINS *****/
- const uint8_t button1_PushButton_PIN_D2 = 2;
- const uint8_t button2_PushButton_PIN_D3 = 3;
- /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
- const uint8_t led_LEDRGB_Red_PIN_D4 = 4;
- const uint8_t led_LEDRGB_Green_PIN_D5 = 5;
- const uint8_t led_LEDRGB_Blue_PIN_D6 = 6;
- /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
- EasyButton button1(button1_PushButton_PIN_D2);
- EasyButton button2(button2_PushButton_PIN_D3);
- // Define the RGB colors
- const int colors[][3] = {
- {0, 255, 0}, // Green
- {255, 255, 0}, // Yellow
- {255, 0, 0} // Red
- };
- int currentColorIndex = 0;
- void setup(void)
- {
- // put your setup code here, to run once:
- pinMode(button1_PushButton_PIN_D2, INPUT_PULLUP);
- pinMode(button2_PushButton_PIN_D3, INPUT_PULLUP);
- pinMode(led_LEDRGB_Red_PIN_D4, OUTPUT);
- pinMode(led_LEDRGB_Green_PIN_D5, OUTPUT);
- pinMode(led_LEDRGB_Blue_PIN_D6, OUTPUT);
- // Set initial color
- changeColor(currentColorIndex);
- // Attach button press event handlers
- button1.onPressed(button1Pressed);
- button2.onPressed(button2Pressed);
- }
- void loop(void)
- {
- // put your main code here, to run repeatedly:
- button1.read();
- button2.read();
- }
- void changeColor(int colorIndex)
- {
- analogWrite(led_LEDRGB_Red_PIN_D4, colors[colorIndex][0]);
- analogWrite(led_LEDRGB_Green_PIN_D5, colors[colorIndex][1]);
- analogWrite(led_LEDRGB_Blue_PIN_D6, colors[colorIndex][2]);
- }
- void button1Pressed()
- {
- if (currentColorIndex < 2)
- {
- currentColorIndex++;
- }
- changeColor(currentColorIndex);
- }
- void button2Pressed()
- {
- if (currentColorIndex > 0)
- {
- currentColorIndex--;
- }
- changeColor(currentColorIndex);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement