InnoVech
Published

Lesson 5 - Button States

Lesson 5 will be covering how to use a push button as a toggle switch.

IntermediateProtip1 hour12,483
Lesson 5 - Button States

Things used in this project

Story

Read more

Code

Button States

Arduino
Lesson 5 Source Code
//Complete Guide to Arduino
//Created by Zaqyas Mahmood, InnoVech
//Lesson 5 Button States


/////// Constant Variables Intialised ///////
const int LED     = 13;
const int Button  = A1;

/////// Changeable Variables Intialised ///////
int ButtonState     = 0;   // take current button state
int LastButtonState = 0;   // take last button state
int LEDState        = 0;   // take light status

void setup() 
{
 Serial.begin (9600);
 pinMode(LED, OUTPUT);
 pinMode(Button, INPUT_PULLUP);
}

void loop() 
{

  ButtonState= digitalRead(Button);
  
  if (LastButtonState == 0 && ButtonState == 1)
  {
    if (LEDState == 0)
    {
      digitalWrite(LED, HIGH);
      LEDState = 1;
    }
    
    else
    {
      digitalWrite(LED, LOW);
      LEDState = 0;
    }
  }
  LastButtonState = ButtonState;
  delay(100);

}

Credits

InnoVech
11 projects • 8 followers
We strive to share our projects with engineering hobbyists and students who are driven to play their roles in the future of engineering.
Contact

Comments

Please log in or sign up to comment.