SlideShare a Scribd company logo
1 of 2
Download to read offline
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

Wall Controller 4 Buttons rechargeable Remote Z-Wave Plus - Hank manual
Wall Controller	4 Buttons rechargeable Remote Z-Wave Plus - Hank manualWall Controller	4 Buttons rechargeable Remote Z-Wave Plus - Hank manual
Wall Controller 4 Buttons rechargeable Remote Z-Wave Plus - Hank manualDomotica daVinci
 
How can i get the code below to have one light remain on continuosly u.pdf
How can i get the code below to have one light remain on continuosly u.pdfHow can i get the code below to have one light remain on continuosly u.pdf
How can i get the code below to have one light remain on continuosly u.pdfThomasXUMParsonsx
 
Semester project report_Prafulla_Kumar_Shahi
Semester project report_Prafulla_Kumar_ShahiSemester project report_Prafulla_Kumar_Shahi
Semester project report_Prafulla_Kumar_ShahiPrafulla Kumar Shahi
 
Exercise: Redesigning a remote control
Exercise: Redesigning a remote controlExercise: Redesigning a remote control
Exercise: Redesigning a remote controlkitlin
 
Quick guide for the countdown clock
Quick guide for the countdown clockQuick guide for the countdown clock
Quick guide for the countdown clockroy xiao
 
POPP. 4-button remote control key Z-WAVE
POPP. 4-button remote control key Z-WAVEPOPP. 4-button remote control key Z-WAVE
POPP. 4-button remote control key Z-WAVEDomotica daVinci
 
Tlk43 Controller
Tlk43 ControllerTlk43 Controller
Tlk43 ControllerJoseph
 
Display saune-finlandese-eng
Display saune-finlandese-engDisplay saune-finlandese-eng
Display saune-finlandese-engVincenzoMaugeri2
 

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

Wall Controller 4 Buttons rechargeable Remote Z-Wave Plus - Hank manual
Wall Controller	4 Buttons rechargeable Remote Z-Wave Plus - Hank manualWall Controller	4 Buttons rechargeable Remote Z-Wave Plus - Hank manual
Wall Controller 4 Buttons rechargeable Remote Z-Wave Plus - Hank manual
 
85 0065-e deluxe-installation insert_web
85 0065-e deluxe-installation insert_web85 0065-e deluxe-installation insert_web
85 0065-e deluxe-installation insert_web
 
Instruction-Manual-ICT India.pdf
Instruction-Manual-ICT India.pdfInstruction-Manual-ICT India.pdf
Instruction-Manual-ICT India.pdf
 
In k450 for xe quick reference card
In k450 for xe quick reference cardIn k450 for xe quick reference card
In k450 for xe quick reference card
 
ZME WALLC-S manual
ZME WALLC-S manualZME WALLC-S manual
ZME WALLC-S manual
 
Wh7016 c instructions
Wh7016 c instructionsWh7016 c instructions
Wh7016 c instructions
 
85 0064-e standard-installation insert_web
85 0064-e standard-installation insert_web85 0064-e standard-installation insert_web
85 0064-e standard-installation insert_web
 
How can i get the code below to have one light remain on continuosly u.pdf
How can i get the code below to have one light remain on continuosly u.pdfHow can i get the code below to have one light remain on continuosly u.pdf
How can i get the code below to have one light remain on continuosly u.pdf
 
Mic1462 quick start_[1738]
Mic1462 quick start_[1738]Mic1462 quick start_[1738]
Mic1462 quick start_[1738]
 
Semester project report_Prafulla_Kumar_Shahi
Semester project report_Prafulla_Kumar_ShahiSemester project report_Prafulla_Kumar_Shahi
Semester project report_Prafulla_Kumar_Shahi
 
Exercise: Redesigning a remote control
Exercise: Redesigning a remote controlExercise: Redesigning a remote control
Exercise: Redesigning a remote control
 
Quick guide for the countdown clock
Quick guide for the countdown clockQuick guide for the countdown clock
Quick guide for the countdown clock
 
POPP. 4-button remote control key Z-WAVE
POPP. 4-button remote control key Z-WAVEPOPP. 4-button remote control key Z-WAVE
POPP. 4-button remote control key Z-WAVE
 
Fadal Keyboard User Manual
Fadal Keyboard User ManualFadal Keyboard User Manual
Fadal Keyboard User Manual
 
Fadal keyboard user manual
Fadal keyboard user manualFadal keyboard user manual
Fadal keyboard user manual
 
85 0066-e ultimate-installation insert_web
85 0066-e ultimate-installation insert_web85 0066-e ultimate-installation insert_web
85 0066-e ultimate-installation insert_web
 
Tlk43 Controller
Tlk43 ControllerTlk43 Controller
Tlk43 Controller
 
Display saune-finlandese-eng
Display saune-finlandese-engDisplay saune-finlandese-eng
Display saune-finlandese-eng
 
LaunchPad MSP 430
LaunchPad MSP 430LaunchPad MSP 430
LaunchPad MSP 430
 
In k450 for xm quick reference card
In k450 for xm quick reference cardIn k450 for xm quick reference card
In k450 for xm quick reference card
 

More from rajkumarm401

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

More from rajkumarm401 (20)

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

Recently uploaded

Simple, Complex, and Compound Sentences Exercises.pdf
Simple, Complex, and Compound Sentences Exercises.pdfSimple, Complex, and Compound Sentences Exercises.pdf
Simple, Complex, and Compound Sentences Exercises.pdfstareducators107
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxDr. Ravikiran H M Gowda
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxJisc
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Pooja Bhuva
 
Introduction to TechSoup’s Digital Marketing Services and Use Cases
Introduction to TechSoup’s Digital Marketing  Services and Use CasesIntroduction to TechSoup’s Digital Marketing  Services and Use Cases
Introduction to TechSoup’s Digital Marketing Services and Use CasesTechSoup
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfDr Vijay Vishwakarma
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSCeline George
 
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...EADTU
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxheathfieldcps1
 
How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17Celine George
 
UGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdf
UGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdfUGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdf
UGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdfNirmal Dwivedi
 
OSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsOSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsSandeep D Chaudhary
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jisc
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024Elizabeth Walsh
 
Tatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf artsTatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf artsNbelano25
 
What is 3 Way Matching Process in Odoo 17.pptx
What is 3 Way Matching Process in Odoo 17.pptxWhat is 3 Way Matching Process in Odoo 17.pptx
What is 3 Way Matching Process in Odoo 17.pptxCeline George
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxEsquimalt MFRC
 
AIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptAIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptNishitharanjan Rout
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - Englishneillewis46
 

Recently uploaded (20)

Simple, Complex, and Compound Sentences Exercises.pdf
Simple, Complex, and Compound Sentences Exercises.pdfSimple, Complex, and Compound Sentences Exercises.pdf
Simple, Complex, and Compound Sentences Exercises.pdf
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
Introduction to TechSoup’s Digital Marketing Services and Use Cases
Introduction to TechSoup’s Digital Marketing  Services and Use CasesIntroduction to TechSoup’s Digital Marketing  Services and Use Cases
Introduction to TechSoup’s Digital Marketing Services and Use Cases
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17
 
UGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdf
UGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdfUGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdf
UGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdf
 
OSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsOSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & Systems
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
Tatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf artsTatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf arts
 
What is 3 Way Matching Process in Odoo 17.pptx
What is 3 Way Matching Process in Odoo 17.pptxWhat is 3 Way Matching Process in Odoo 17.pptx
What is 3 Way Matching Process in Odoo 17.pptx
 
OS-operating systems- ch05 (CPU Scheduling) ...
OS-operating systems- ch05 (CPU Scheduling) ...OS-operating systems- ch05 (CPU Scheduling) ...
OS-operating systems- ch05 (CPU Scheduling) ...
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
AIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptAIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.ppt
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 

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