CCCPaste

Button Animation rev_01

/********* 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 Animation
	- Source Code compiled for: Arduino Nano
	- Source Code created on: 2023-12-07 01:49:47

********* Pleasedontcode.com **********/

/****** SYSTEM REQUIREMENTS *****/
/****** SYSTEM REQUIREMENT 1 *****/
	/* i need a button have animation can slide left to */
	/* right */
/****** END SYSTEM REQUIREMENTS *****/

/****** DEFINITION OF LIBRARIES *****/
#include 

/****** FUNCTION PROTOTYPES *****/
void setup(void);
void loop(void);

/***** DEFINITION OF DIGITAL OUTPUT PINS *****/
const uint8_t myButton_LEDRGB_Red_PIN_D2 = 2;
const uint8_t myButton_LEDRGB_Green_PIN_D3 = 3;
const uint8_t myButton_LEDRGB_Blue_PIN_D4 = 4;

/***** DEFINITION OF DIGITAL INPUT PINS *****/
const uint8_t myButton_PIN_D5 = 5;

/****** GLOBAL VARIABLES *****/
bool buttonState = false; // Variable to store the state of the button
bool animationDirection = true; // Variable to store the direction of the animation

void setup(void)
{
  // put your setup code here, to run once:
  pinMode(myButton_LEDRGB_Red_PIN_D2, OUTPUT);
  pinMode(myButton_LEDRGB_Green_PIN_D3, OUTPUT);
  pinMode(myButton_LEDRGB_Blue_PIN_D4, OUTPUT);
  
  pinMode(myButton_PIN_D5, INPUT_PULLUP); // Set the button pin as input with internal pull-up resistor
}

void loop(void)
{
  // put your main code here, to run repeatedly:
  
  buttonState = digitalRead(myButton_PIN_D5); // Read the state of the button
  
  // Check if the button is pressed
  if (buttonState == LOW) {
    // Slide the animation to the right
    if (animationDirection == true) {
      digitalWrite(myButton_LEDRGB_Red_PIN_D2, HIGH);
      delay(100);
      digitalWrite(myButton_LEDRGB_Red_PIN_D2, LOW);
      digitalWrite(myButton_LEDRGB_Green_PIN_D3, HIGH);
      delay(100);
      digitalWrite(myButton_LEDRGB_Green_PIN_D3, LOW);
      digitalWrite(myButton_LEDRGB_Blue_PIN_D4, HIGH);
      delay(100);
      digitalWrite(myButton_LEDRGB_Blue_PIN_D4, LOW);
      animationDirection = false;
    }
    // Slide the animation to the left
    else {
      digitalWrite(myButton_LEDRGB_Blue_PIN_D4, HIGH);
      delay(100);
      digitalWrite(myButton_LEDRGB_Blue_PIN_D4, LOW);
      digitalWrite(myButton_LEDRGB_Green_PIN_D3, HIGH);
      delay(100);
      digitalWrite(myButton_LEDRGB_Green_PIN_D3, LOW);
      digitalWrite(myButton_LEDRGB_Red_PIN_D2, HIGH);
      delay(100);
      digitalWrite(myButton_LEDRGB_Red_PIN_D2, LOW);
      animationDirection = true;
    }
  }
}