Advertisement
pleasedontcode

Button Animation rev_01

Dec 6th, 2023
97
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 Animation
  13.     - Source Code compiled for: Arduino Nano
  14.     - Source Code created on: 2023-12-07 01:49:47
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* i need a button have animation can slide left to */
  21.     /* right */
  22. /****** END SYSTEM REQUIREMENTS *****/
  23.  
  24. /****** DEFINITION OF LIBRARIES *****/
  25. #include <Arduino.h>
  26.  
  27. /****** FUNCTION PROTOTYPES *****/
  28. void setup(void);
  29. void loop(void);
  30.  
  31. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  32. const uint8_t myButton_LEDRGB_Red_PIN_D2 = 2;
  33. const uint8_t myButton_LEDRGB_Green_PIN_D3 = 3;
  34. const uint8_t myButton_LEDRGB_Blue_PIN_D4 = 4;
  35.  
  36. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  37. const uint8_t myButton_PIN_D5 = 5;
  38.  
  39. /****** GLOBAL VARIABLES *****/
  40. bool buttonState = false; // Variable to store the state of the button
  41. bool animationDirection = true; // Variable to store the direction of the animation
  42.  
  43. void setup(void)
  44. {
  45.   // put your setup code here, to run once:
  46.   pinMode(myButton_LEDRGB_Red_PIN_D2, OUTPUT);
  47.   pinMode(myButton_LEDRGB_Green_PIN_D3, OUTPUT);
  48.   pinMode(myButton_LEDRGB_Blue_PIN_D4, OUTPUT);
  49.  
  50.   pinMode(myButton_PIN_D5, INPUT_PULLUP); // Set the button pin as input with internal pull-up resistor
  51. }
  52.  
  53. void loop(void)
  54. {
  55.   // put your main code here, to run repeatedly:
  56.  
  57.   buttonState = digitalRead(myButton_PIN_D5); // Read the state of the button
  58.  
  59.   // Check if the button is pressed
  60.   if (buttonState == LOW) {
  61.     // Slide the animation to the right
  62.     if (animationDirection == true) {
  63.       digitalWrite(myButton_LEDRGB_Red_PIN_D2, HIGH);
  64.       delay(100);
  65.       digitalWrite(myButton_LEDRGB_Red_PIN_D2, LOW);
  66.       digitalWrite(myButton_LEDRGB_Green_PIN_D3, HIGH);
  67.       delay(100);
  68.       digitalWrite(myButton_LEDRGB_Green_PIN_D3, LOW);
  69.       digitalWrite(myButton_LEDRGB_Blue_PIN_D4, HIGH);
  70.       delay(100);
  71.       digitalWrite(myButton_LEDRGB_Blue_PIN_D4, LOW);
  72.       animationDirection = false;
  73.     }
  74.     // Slide the animation to the left
  75.     else {
  76.       digitalWrite(myButton_LEDRGB_Blue_PIN_D4, HIGH);
  77.       delay(100);
  78.       digitalWrite(myButton_LEDRGB_Blue_PIN_D4, LOW);
  79.       digitalWrite(myButton_LEDRGB_Green_PIN_D3, HIGH);
  80.       delay(100);
  81.       digitalWrite(myButton_LEDRGB_Green_PIN_D3, LOW);
  82.       digitalWrite(myButton_LEDRGB_Red_PIN_D2, HIGH);
  83.       delay(100);
  84.       digitalWrite(myButton_LEDRGB_Red_PIN_D2, LOW);
  85.       animationDirection = true;
  86.     }
  87.   }
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement