SlideShare a Scribd company logo
Program Structure: declare ButtonState: Global variable holding status of button unsigned long
Button Time: Holds time to debounce Switch int Button NextState( input) function that is to be
called in loop to service the switch switch based on ButtonState Idle: State where nothing has
been happening. if input is low, Button Time millis0: Record time of high to low transition. set
ButtonState to Wait Move to Wait state. Wait: Button has gone low and we are waiting for it to
remain low for 5 milliseconds if input is high. l f button has gone high. set ButtonState to Idle
Reset back to Idle else if (millis0- Button Time 5 if 5 milliseconds has passed. set ButtonState to
Low Move to low state. return 1 indicating that button has been pressed Low Button is low and
has been so for 5 milliseconds. if input is high Once button released. set ButtonState to Idle: end
of switch return 0 By default return 0 indicating nothing is happening. setup: Function run at the
start of the program. Set pin as input Check Schematic in Lab 4 for pin number. Set ButtonState
to Idle Initialize state loop Function continuously called Check status of button. if
ButtonNextState( digitalRead( input)) Send serial message indicating button press Indicate
button has been pressed end of if
Solution
//initialize and declare variables
const int ledPin = 13; //led attached to this pin
const int buttonPin = 2; //push button attached to this pin
int buttonState = LOW; //this variable tracks the state of the button, low if not pressed, high if
pressed
int ledState = -1; //this variable tracks the state of the LED, negative if off, positive if on
long lastDebounceTime = 0; // the last time the output pin was toggled
long debounceDelay = 50; // the debounce time; increase if the output flickers
void setup() {
//set the mode of the pins...
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT);
}//close void setup
void loop() {
//sample the state of the button - is it pressed or not?
buttonState = digitalRead(buttonPin);
//filter out any noise by setting a time buffer
if ( (millis() - lastDebounceTime) > debounceDelay) {
//if the button has been pressed, lets toggle the LED from "off to on" or "on to off"
if ( (buttonState == HIGH) && (ledState < 0) ) {
digitalWrite(ledPin, HIGH); //turn LED on
ledState = -ledState; //now the LED is on, we need to change the state
lastDebounceTime = millis(); //set the current time
}
else if ( (buttonState == HIGH) && (ledState > 0) ) {
digitalWrite(ledPin, LOW); //turn LED off
ledState = -ledState; //now the LED is off, we need to change the state
lastDebounceTime = millis(); //set the current time
}//close if/else
}//close if(time buffer)
}//close void loop

More Related Content

Similar to Program Structure declare ButtonState Global variable holding statu.pdf (20)

PPT
arduinoSimon.ppt
ZainIslam20
 
PPT
arduinoSimon.ppt
AkhandPratapSingh86
 
PDF
Microcontroller operations in arduino.PPT.pdf
DanielMwembia
 
DOCX
Michael kontopoulos
josnihmurni2907
 
PDF
publish manual
John Webster
 
PDF
antisleepalarm-230417034941-79209def.pdf
rahuljanawad17
 
PPTX
ANTI SLEEP ALARM
JagannathDansana
 
PPTX
ee2004Asg_five_SemB25 - Read-Only.pptx
chuigary70866
 
PPTX
Introduction to arduino Programming with
likhithkumpala159
 
PPTX
Fun with arduino
Ravikumar Tiwari
 
PPT
arduino.ppt
sunilkumar652338
 
PPTX
arduino simulators OR microcontroller.pptx
JohnMarkCatalua1
 
PPT
arduino Simon power point presentation.ppt
JuniorAsong
 
PPTX
Sensors and Actuators in Arduino, Introduction
BibekPokhrel13
 
PPTX
An Introduction to Microcontrollers part 2
khaledsoradi
 
DOCX
7segment scetch
Bang Igo
 
PDF
antisleepalarm-230417034941-79209def (3).pdf
ADARSHSHUKLA12A
 
PPTX
Temperature sensor with a led matrix display (arduino controlled)
TechLeap
 
PPTX
antisleepalarm-230417034941-79209def (1).pptx
SumitKumar236277
 
DOCX
Arm7 Interfacing examples
Dr.YNM
 
arduinoSimon.ppt
ZainIslam20
 
arduinoSimon.ppt
AkhandPratapSingh86
 
Microcontroller operations in arduino.PPT.pdf
DanielMwembia
 
Michael kontopoulos
josnihmurni2907
 
publish manual
John Webster
 
antisleepalarm-230417034941-79209def.pdf
rahuljanawad17
 
ANTI SLEEP ALARM
JagannathDansana
 
ee2004Asg_five_SemB25 - Read-Only.pptx
chuigary70866
 
Introduction to arduino Programming with
likhithkumpala159
 
Fun with arduino
Ravikumar Tiwari
 
arduino.ppt
sunilkumar652338
 
arduino simulators OR microcontroller.pptx
JohnMarkCatalua1
 
arduino Simon power point presentation.ppt
JuniorAsong
 
Sensors and Actuators in Arduino, Introduction
BibekPokhrel13
 
An Introduction to Microcontrollers part 2
khaledsoradi
 
7segment scetch
Bang Igo
 
antisleepalarm-230417034941-79209def (3).pdf
ADARSHSHUKLA12A
 
Temperature sensor with a led matrix display (arduino controlled)
TechLeap
 
antisleepalarm-230417034941-79209def (1).pptx
SumitKumar236277
 
Arm7 Interfacing examples
Dr.YNM
 

More from rajkumarm401 (20)

PDF
Having issues with passing my values through different functions aft.pdf
rajkumarm401
 
PDF
Explain the characterstic of web service techonolgy.SolutionT.pdf
rajkumarm401
 
PDF
Executive Summary i. Provide a succinct overview of your strategic p.pdf
rajkumarm401
 
PDF
eee230 Instruction details Answer the following questions in a typed.pdf
rajkumarm401
 
PDF
Essay questionPorter Combining business strategy What is itmeani.pdf
rajkumarm401
 
PDF
During oogenesis, will the genotypes of the first and second polar b.pdf
rajkumarm401
 
PDF
Does personal information available on the Internet make an employee.pdf
rajkumarm401
 
PDF
Determine whether each series is convergent or divergent..pdf
rajkumarm401
 
PDF
Describe the primary differences between WEP, WPA, and WPA2 protocol.pdf
rajkumarm401
 
PDF
Complete the provided partial C++ Linked List program. Main.cpp is g.pdf
rajkumarm401
 
PDF
Click the desktop shortcut icon that you created in this module, and .pdf
rajkumarm401
 
PDF
YOU DO IT 4! create an named YouDolt 4 and save it in the vB 2015Chap.pdf
rajkumarm401
 
PDF
What was the causes of the Vietnam war What was the causes of .pdf
rajkumarm401
 
PDF
Transactions costs are zero in financial markets. zero in financial i.pdf
rajkumarm401
 
PDF
This is for an homework assignment using Java code. Here is the home.pdf
rajkumarm401
 
PDF
Question 34 (1 point) D A check is 1) not money because it is not off.pdf
rajkumarm401
 
PDF
ObjectiveCreate a graphical game of minesweeper IN JAVA. The boar.pdf
rajkumarm401
 
PDF
Need help in assembly. Thank you Implement the following expression .pdf
rajkumarm401
 
PDF
mine whether the following s are true or false False A parabola has e.pdf
rajkumarm401
 
PDF
Locate an article where the technique of Polymerase chain reaction (.pdf
rajkumarm401
 
Having issues with passing my values through different functions aft.pdf
rajkumarm401
 
Explain the characterstic of web service techonolgy.SolutionT.pdf
rajkumarm401
 
Executive Summary i. Provide a succinct overview of your strategic p.pdf
rajkumarm401
 
eee230 Instruction details Answer the following questions in a typed.pdf
rajkumarm401
 
Essay questionPorter Combining business strategy What is itmeani.pdf
rajkumarm401
 
During oogenesis, will the genotypes of the first and second polar b.pdf
rajkumarm401
 
Does personal information available on the Internet make an employee.pdf
rajkumarm401
 
Determine whether each series is convergent or divergent..pdf
rajkumarm401
 
Describe the primary differences between WEP, WPA, and WPA2 protocol.pdf
rajkumarm401
 
Complete the provided partial C++ Linked List program. Main.cpp is g.pdf
rajkumarm401
 
Click the desktop shortcut icon that you created in this module, and .pdf
rajkumarm401
 
YOU DO IT 4! create an named YouDolt 4 and save it in the vB 2015Chap.pdf
rajkumarm401
 
What was the causes of the Vietnam war What was the causes of .pdf
rajkumarm401
 
Transactions costs are zero in financial markets. zero in financial i.pdf
rajkumarm401
 
This is for an homework assignment using Java code. Here is the home.pdf
rajkumarm401
 
Question 34 (1 point) D A check is 1) not money because it is not off.pdf
rajkumarm401
 
ObjectiveCreate a graphical game of minesweeper IN JAVA. The boar.pdf
rajkumarm401
 
Need help in assembly. Thank you Implement the following expression .pdf
rajkumarm401
 
mine whether the following s are true or false False A parabola has e.pdf
rajkumarm401
 
Locate an article where the technique of Polymerase chain reaction (.pdf
rajkumarm401
 

Recently uploaded (20)

PPTX
How to Configure Lost Reasons in Odoo 18 CRM
Celine George
 
PDF
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
PPTX
Explorando Recursos do Summer '25: Dicas Essenciais - 02
Mauricio Alexandre Silva
 
PPTX
Pyhton with Mysql to perform CRUD operations.pptx
Ramakrishna Reddy Bijjam
 
PPTX
Accounting Skills Paper-I, Preparation of Vouchers
Dr. Sushil Bansode
 
PPTX
How to Create Rental Orders in Odoo 18 Rental
Celine George
 
PDF
CEREBRAL PALSY: NURSING MANAGEMENT .pdf
PRADEEP ABOTHU
 
PPTX
Capitol Doctoral Presentation -July 2025.pptx
CapitolTechU
 
PPTX
Optimizing Cancer Screening With MCED Technologies: From Science to Practical...
i3 Health
 
PPTX
Optimizing Cancer Screening With MCED Technologies: From Science to Practical...
i3 Health
 
PPTX
Unit 2 COMMERCIAL BANKING, Corporate banking.pptx
AnubalaSuresh1
 
PPTX
Growth and development and milestones, factors
BHUVANESHWARI BADIGER
 
PPTX
Presentation: Climate Citizenship Digital Education
Karl Donert
 
PPTX
PPT on the Development of Education in the Victorian England
Beena E S
 
PPTX
How to Configure Access Rights of Manufacturing Orders in Odoo 18 Manufacturing
Celine George
 
PPTX
Views on Education of Indian Thinkers J.Krishnamurthy..pptx
ShrutiMahanta1
 
PDF
Federal dollars withheld by district, charter, grant recipient
Mebane Rash
 
PPT
digestive system for Pharm d I year HAP
rekhapositivity
 
PDF
Zoology (Animal Physiology) practical Manual
raviralanaresh2
 
PPTX
ROLE OF ANTIOXIDANT IN EYE HEALTH MANAGEMENT.pptx
Subham Panja
 
How to Configure Lost Reasons in Odoo 18 CRM
Celine George
 
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
Explorando Recursos do Summer '25: Dicas Essenciais - 02
Mauricio Alexandre Silva
 
Pyhton with Mysql to perform CRUD operations.pptx
Ramakrishna Reddy Bijjam
 
Accounting Skills Paper-I, Preparation of Vouchers
Dr. Sushil Bansode
 
How to Create Rental Orders in Odoo 18 Rental
Celine George
 
CEREBRAL PALSY: NURSING MANAGEMENT .pdf
PRADEEP ABOTHU
 
Capitol Doctoral Presentation -July 2025.pptx
CapitolTechU
 
Optimizing Cancer Screening With MCED Technologies: From Science to Practical...
i3 Health
 
Optimizing Cancer Screening With MCED Technologies: From Science to Practical...
i3 Health
 
Unit 2 COMMERCIAL BANKING, Corporate banking.pptx
AnubalaSuresh1
 
Growth and development and milestones, factors
BHUVANESHWARI BADIGER
 
Presentation: Climate Citizenship Digital Education
Karl Donert
 
PPT on the Development of Education in the Victorian England
Beena E S
 
How to Configure Access Rights of Manufacturing Orders in Odoo 18 Manufacturing
Celine George
 
Views on Education of Indian Thinkers J.Krishnamurthy..pptx
ShrutiMahanta1
 
Federal dollars withheld by district, charter, grant recipient
Mebane Rash
 
digestive system for Pharm d I year HAP
rekhapositivity
 
Zoology (Animal Physiology) practical Manual
raviralanaresh2
 
ROLE OF ANTIOXIDANT IN EYE HEALTH MANAGEMENT.pptx
Subham Panja
 

Program Structure declare ButtonState Global variable holding statu.pdf

  • 1. Program Structure: declare ButtonState: Global variable holding status of button unsigned long Button Time: Holds time to debounce Switch int Button NextState( input) function that is to be called in loop to service the switch switch based on ButtonState Idle: State where nothing has been happening. if input is low, Button Time millis0: Record time of high to low transition. set ButtonState to Wait Move to Wait state. Wait: Button has gone low and we are waiting for it to remain low for 5 milliseconds if input is high. l f button has gone high. set ButtonState to Idle Reset back to Idle else if (millis0- Button Time 5 if 5 milliseconds has passed. set ButtonState to Low Move to low state. return 1 indicating that button has been pressed Low Button is low and has been so for 5 milliseconds. if input is high Once button released. set ButtonState to Idle: end of switch return 0 By default return 0 indicating nothing is happening. setup: Function run at the start of the program. Set pin as input Check Schematic in Lab 4 for pin number. Set ButtonState to Idle Initialize state loop Function continuously called Check status of button. if ButtonNextState( digitalRead( input)) Send serial message indicating button press Indicate button has been pressed end of if Solution //initialize and declare variables const int ledPin = 13; //led attached to this pin const int buttonPin = 2; //push button attached to this pin int buttonState = LOW; //this variable tracks the state of the button, low if not pressed, high if pressed int ledState = -1; //this variable tracks the state of the LED, negative if off, positive if on long lastDebounceTime = 0; // the last time the output pin was toggled long debounceDelay = 50; // the debounce time; increase if the output flickers void setup() { //set the mode of the pins... pinMode(ledPin, OUTPUT); pinMode(buttonPin, INPUT); }//close void setup void loop() { //sample the state of the button - is it pressed or not? buttonState = digitalRead(buttonPin); //filter out any noise by setting a time buffer if ( (millis() - lastDebounceTime) > debounceDelay) { //if the button has been pressed, lets toggle the LED from "off to on" or "on to off"
  • 2. if ( (buttonState == HIGH) && (ledState < 0) ) { digitalWrite(ledPin, HIGH); //turn LED on ledState = -ledState; //now the LED is on, we need to change the state lastDebounceTime = millis(); //set the current time } else if ( (buttonState == HIGH) && (ledState > 0) ) { digitalWrite(ledPin, LOW); //turn LED off ledState = -ledState; //now the LED is off, we need to change the state lastDebounceTime = millis(); //set the current time }//close if/else }//close if(time buffer) }//close void loop