SlideShare a Scribd company logo
1 of 2
Simple timer code for Arduino
Raw
timer_test.pde
// Timer test
// Kevin Karplus
// 12 July 2011
// One of the simplest data acquisition tasks is
// to time the interval between two events.
// This program waits for pin 4 to go high,
// starts a timer, lights the on-board LED,
// then waits for pin 5 to go low,
// when it turns of the LED and resets the timer.
// It reports the time between pin 4 going high and pin 5 going low
// in microseconds.
// It is easy to change the code to use
// either transitions to high or transitions to low.
// Using a pull-up resistor can convert any make or break contact to
// a low-going or high-going edge.
// Opposite polarities were used in this example, so that the
// same square wave could be fed to both pins and the width of the
// positive pulse timed.
// The minimum time it can report (if pin 5 is already low when
// pin 4 goes high is about 12 microseconds +- 4 microseconds).
// The reported time seems to be always a multiple of 4 microseconds,
// which is most likely the resolution of the micros() call.
// Timing seems to be fairly reliable down to about 60 microseconds.
void setup()
{
pinMode(4, INPUT);
pinMode(5, INPUT);
pinMode(13, OUTPUT); // on-board LED
Serial.begin(115200);
}
void loop()
{
unsigned long start_time;
unsigned long stop_time;
digitalWrite(13,0); // Turn LED off
while(digitalRead(4)==0) {}
start_time=micros();
digitalWrite(13,1); // Turn LED on
while (digitalRead(5)) {}
stop_time=micros();
digitalWrite(13,0);
Serial.print("time hi->lo=");
Serial.print(stop_time-start_time);
Serial.println(" microsec");
delay(500);
}

More Related Content

Similar to Simple timer code for arduino

iptable casestudy by sans.pdf
iptable casestudy by sans.pdfiptable casestudy by sans.pdf
iptable casestudy by sans.pdfAdmin621695
 
A Review on Zigbee, Gsm and Wsn Based Home Security by Using Embedded Control...
A Review on Zigbee, Gsm and Wsn Based Home Security by Using Embedded Control...A Review on Zigbee, Gsm and Wsn Based Home Security by Using Embedded Control...
A Review on Zigbee, Gsm and Wsn Based Home Security by Using Embedded Control...ijesajournal
 
Oscon2008 network-troubleshooting-v1
Oscon2008 network-troubleshooting-v1Oscon2008 network-troubleshooting-v1
Oscon2008 network-troubleshooting-v1sabry khalil
 
Unit 4 - Transport Layer
Unit 4 - Transport LayerUnit 4 - Transport Layer
Unit 4 - Transport LayerKalpanaC14
 
International Journal of Engineering Research and Development (IJERD)
International Journal of Engineering Research and Development (IJERD)International Journal of Engineering Research and Development (IJERD)
International Journal of Engineering Research and Development (IJERD)IJERD Editor
 
Toxic Gas Detection-An Experiment with the Simulation Process
Toxic Gas Detection-An Experiment with the Simulation ProcessToxic Gas Detection-An Experiment with the Simulation Process
Toxic Gas Detection-An Experiment with the Simulation ProcessIRJET Journal
 
Wireshark udp
Wireshark udpWireshark udp
Wireshark udperombapu
 
COMPUTER NETWORKS NOTES Unit 4
COMPUTER NETWORKS NOTES  Unit 4 COMPUTER NETWORKS NOTES  Unit 4
COMPUTER NETWORKS NOTES Unit 4 sudhakce
 
Safety siren pro series 3 radon gas detector user guide user manual
Safety siren pro series 3 radon gas detector user guide  user manualSafety siren pro series 3 radon gas detector user guide  user manual
Safety siren pro series 3 radon gas detector user guide user manualAshish Kamble
 
PAM4 Analysis and Measurement Webinar Slidedeck
PAM4 Analysis and Measurement Webinar SlidedeckPAM4 Analysis and Measurement Webinar Slidedeck
PAM4 Analysis and Measurement Webinar Slidedeckteledynelecroy
 
PAM4 Analysis and Measurement Considerations Webinar
PAM4 Analysis and Measurement Considerations WebinarPAM4 Analysis and Measurement Considerations Webinar
PAM4 Analysis and Measurement Considerations WebinarHilary Lustig
 
Webinar: Is your cheese at the correct temperature?
Webinar: Is your cheese at the correct temperature?Webinar: Is your cheese at the correct temperature?
Webinar: Is your cheese at the correct temperature?Simple Hardware
 
OpenPOWER Application Optimization
OpenPOWER Application Optimization OpenPOWER Application Optimization
OpenPOWER Application Optimization Ganesan Narayanasamy
 
HARD COPY REPORT CDAC
HARD COPY REPORT CDACHARD COPY REPORT CDAC
HARD COPY REPORT CDACSarthak Dubey
 
Clap Pattern Based Electrical Appliance control
Clap Pattern Based Electrical Appliance controlClap Pattern Based Electrical Appliance control
Clap Pattern Based Electrical Appliance controlIRJET Journal
 
Gas sensor Alarm
Gas sensor AlarmGas sensor Alarm
Gas sensor AlarmUdit Jain
 
Lab 6 microcontroller
Lab 6 microcontrollerLab 6 microcontroller
Lab 6 microcontrollermkazree
 

Similar to Simple timer code for arduino (20)

iptable casestudy by sans.pdf
iptable casestudy by sans.pdfiptable casestudy by sans.pdf
iptable casestudy by sans.pdf
 
A Review on Zigbee, Gsm and Wsn Based Home Security by Using Embedded Control...
A Review on Zigbee, Gsm and Wsn Based Home Security by Using Embedded Control...A Review on Zigbee, Gsm and Wsn Based Home Security by Using Embedded Control...
A Review on Zigbee, Gsm and Wsn Based Home Security by Using Embedded Control...
 
Oscon2008 network-troubleshooting-v1
Oscon2008 network-troubleshooting-v1Oscon2008 network-troubleshooting-v1
Oscon2008 network-troubleshooting-v1
 
IoT in LTE
IoT in LTEIoT in LTE
IoT in LTE
 
Unit 4 - Transport Layer
Unit 4 - Transport LayerUnit 4 - Transport Layer
Unit 4 - Transport Layer
 
International Journal of Engineering Research and Development (IJERD)
International Journal of Engineering Research and Development (IJERD)International Journal of Engineering Research and Development (IJERD)
International Journal of Engineering Research and Development (IJERD)
 
Toxic Gas Detection-An Experiment with the Simulation Process
Toxic Gas Detection-An Experiment with the Simulation ProcessToxic Gas Detection-An Experiment with the Simulation Process
Toxic Gas Detection-An Experiment with the Simulation Process
 
Wireshark udp
Wireshark udpWireshark udp
Wireshark udp
 
COMPUTER NETWORKS NOTES Unit 4
COMPUTER NETWORKS NOTES  Unit 4 COMPUTER NETWORKS NOTES  Unit 4
COMPUTER NETWORKS NOTES Unit 4
 
ritesh (2)
ritesh (2)ritesh (2)
ritesh (2)
 
Safety siren pro series 3 radon gas detector user guide user manual
Safety siren pro series 3 radon gas detector user guide  user manualSafety siren pro series 3 radon gas detector user guide  user manual
Safety siren pro series 3 radon gas detector user guide user manual
 
PAM4 Analysis and Measurement Webinar Slidedeck
PAM4 Analysis and Measurement Webinar SlidedeckPAM4 Analysis and Measurement Webinar Slidedeck
PAM4 Analysis and Measurement Webinar Slidedeck
 
PAM4 Analysis and Measurement Considerations Webinar
PAM4 Analysis and Measurement Considerations WebinarPAM4 Analysis and Measurement Considerations Webinar
PAM4 Analysis and Measurement Considerations Webinar
 
Webinar: Is your cheese at the correct temperature?
Webinar: Is your cheese at the correct temperature?Webinar: Is your cheese at the correct temperature?
Webinar: Is your cheese at the correct temperature?
 
OpenPOWER Application Optimization
OpenPOWER Application Optimization OpenPOWER Application Optimization
OpenPOWER Application Optimization
 
HARD COPY REPORT CDAC
HARD COPY REPORT CDACHARD COPY REPORT CDAC
HARD COPY REPORT CDAC
 
Development of an App for Compressible Flow “App4Comp”
Development of an App for Compressible Flow “App4Comp”Development of an App for Compressible Flow “App4Comp”
Development of an App for Compressible Flow “App4Comp”
 
Clap Pattern Based Electrical Appliance control
Clap Pattern Based Electrical Appliance controlClap Pattern Based Electrical Appliance control
Clap Pattern Based Electrical Appliance control
 
Gas sensor Alarm
Gas sensor AlarmGas sensor Alarm
Gas sensor Alarm
 
Lab 6 microcontroller
Lab 6 microcontrollerLab 6 microcontroller
Lab 6 microcontroller
 

More from josnihmurni2907

Twin wheeler modified for arduino simplified serial protocol to sabertooth v22
Twin wheeler modified for arduino simplified serial protocol to sabertooth v22Twin wheeler modified for arduino simplified serial protocol to sabertooth v22
Twin wheeler modified for arduino simplified serial protocol to sabertooth v22josnihmurni2907
 
Twin wheeler modified for arduino simplified serial protocol to sabertooth v21
Twin wheeler modified for arduino simplified serial protocol to sabertooth v21Twin wheeler modified for arduino simplified serial protocol to sabertooth v21
Twin wheeler modified for arduino simplified serial protocol to sabertooth v21josnihmurni2907
 
Twin wheeler modified for arduino simplified serial protocol to sabertooth v2
Twin wheeler modified for arduino simplified serial protocol to sabertooth v2Twin wheeler modified for arduino simplified serial protocol to sabertooth v2
Twin wheeler modified for arduino simplified serial protocol to sabertooth v2josnihmurni2907
 
How to write timings and delays
How to write timings and delaysHow to write timings and delays
How to write timings and delaysjosnihmurni2907
 
Call and message using arduino and gsm module
Call and message using arduino and gsm moduleCall and message using arduino and gsm module
Call and message using arduino and gsm modulejosnihmurni2907
 
140813560 nota-kimia-tingkatan-4
140813560 nota-kimia-tingkatan-4140813560 nota-kimia-tingkatan-4
140813560 nota-kimia-tingkatan-4josnihmurni2907
 
132944997 rancangan-tahunan-mm-t5-2013
132944997 rancangan-tahunan-mm-t5-2013132944997 rancangan-tahunan-mm-t5-2013
132944997 rancangan-tahunan-mm-t5-2013josnihmurni2907
 
Agihan kertas 1 dan kertas 2
Agihan kertas 1 dan kertas 2Agihan kertas 1 dan kertas 2
Agihan kertas 1 dan kertas 2josnihmurni2907
 
Cara menghilangkan rasa ngantuk di kelas
Cara menghilangkan rasa ngantuk di kelasCara menghilangkan rasa ngantuk di kelas
Cara menghilangkan rasa ngantuk di kelasjosnihmurni2907
 
Bercakap mengenai nikmat allah s
Bercakap mengenai nikmat allah sBercakap mengenai nikmat allah s
Bercakap mengenai nikmat allah sjosnihmurni2907
 
Bandar purba dalam tasik di china
Bandar purba dalam tasik di chinaBandar purba dalam tasik di china
Bandar purba dalam tasik di chinajosnihmurni2907
 
12 kaum yang telah allah swt binasakan
12 kaum yang telah allah swt binasakan12 kaum yang telah allah swt binasakan
12 kaum yang telah allah swt binasakanjosnihmurni2907
 

More from josnihmurni2907 (20)

Twin wheeler modified for arduino simplified serial protocol to sabertooth v22
Twin wheeler modified for arduino simplified serial protocol to sabertooth v22Twin wheeler modified for arduino simplified serial protocol to sabertooth v22
Twin wheeler modified for arduino simplified serial protocol to sabertooth v22
 
Twin wheeler modified for arduino simplified serial protocol to sabertooth v21
Twin wheeler modified for arduino simplified serial protocol to sabertooth v21Twin wheeler modified for arduino simplified serial protocol to sabertooth v21
Twin wheeler modified for arduino simplified serial protocol to sabertooth v21
 
Twin wheeler modified for arduino simplified serial protocol to sabertooth v2
Twin wheeler modified for arduino simplified serial protocol to sabertooth v2Twin wheeler modified for arduino simplified serial protocol to sabertooth v2
Twin wheeler modified for arduino simplified serial protocol to sabertooth v2
 
Michael kontopoulos
Michael kontopoulosMichael kontopoulos
Michael kontopoulos
 
Michael kontopoulo1s
Michael kontopoulo1sMichael kontopoulo1s
Michael kontopoulo1s
 
How to write timings and delays
How to write timings and delaysHow to write timings and delays
How to write timings and delays
 
Define ba1
Define ba1Define ba1
Define ba1
 
Define ba
Define baDefine ba
Define ba
 
Call and message using arduino and gsm module
Call and message using arduino and gsm moduleCall and message using arduino and gsm module
Call and message using arduino and gsm module
 
Arduino
ArduinoArduino
Arduino
 
Arduino 101
Arduino 101Arduino 101
Arduino 101
 
140813560 nota-kimia-tingkatan-4
140813560 nota-kimia-tingkatan-4140813560 nota-kimia-tingkatan-4
140813560 nota-kimia-tingkatan-4
 
1.funtions (1)
1.funtions (1)1.funtions (1)
1.funtions (1)
 
132944997 rancangan-tahunan-mm-t5-2013
132944997 rancangan-tahunan-mm-t5-2013132944997 rancangan-tahunan-mm-t5-2013
132944997 rancangan-tahunan-mm-t5-2013
 
Agihan kertas 1 dan kertas 2
Agihan kertas 1 dan kertas 2Agihan kertas 1 dan kertas 2
Agihan kertas 1 dan kertas 2
 
Ciri pemimpin
Ciri pemimpinCiri pemimpin
Ciri pemimpin
 
Cara menghilangkan rasa ngantuk di kelas
Cara menghilangkan rasa ngantuk di kelasCara menghilangkan rasa ngantuk di kelas
Cara menghilangkan rasa ngantuk di kelas
 
Bercakap mengenai nikmat allah s
Bercakap mengenai nikmat allah sBercakap mengenai nikmat allah s
Bercakap mengenai nikmat allah s
 
Bandar purba dalam tasik di china
Bandar purba dalam tasik di chinaBandar purba dalam tasik di china
Bandar purba dalam tasik di china
 
12 kaum yang telah allah swt binasakan
12 kaum yang telah allah swt binasakan12 kaum yang telah allah swt binasakan
12 kaum yang telah allah swt binasakan
 

Recently uploaded

“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppCeline George
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfUmakantAnnand
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 

Recently uploaded (20)

“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website App
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.Compdf
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 

Simple timer code for arduino

  • 1. Simple timer code for Arduino Raw timer_test.pde // Timer test // Kevin Karplus // 12 July 2011 // One of the simplest data acquisition tasks is // to time the interval between two events. // This program waits for pin 4 to go high, // starts a timer, lights the on-board LED, // then waits for pin 5 to go low, // when it turns of the LED and resets the timer. // It reports the time between pin 4 going high and pin 5 going low // in microseconds. // It is easy to change the code to use // either transitions to high or transitions to low. // Using a pull-up resistor can convert any make or break contact to // a low-going or high-going edge. // Opposite polarities were used in this example, so that the // same square wave could be fed to both pins and the width of the // positive pulse timed. // The minimum time it can report (if pin 5 is already low when // pin 4 goes high is about 12 microseconds +- 4 microseconds). // The reported time seems to be always a multiple of 4 microseconds, // which is most likely the resolution of the micros() call. // Timing seems to be fairly reliable down to about 60 microseconds. void setup() { pinMode(4, INPUT); pinMode(5, INPUT);
  • 2. pinMode(13, OUTPUT); // on-board LED Serial.begin(115200); } void loop() { unsigned long start_time; unsigned long stop_time; digitalWrite(13,0); // Turn LED off while(digitalRead(4)==0) {} start_time=micros(); digitalWrite(13,1); // Turn LED on while (digitalRead(5)) {} stop_time=micros(); digitalWrite(13,0); Serial.print("time hi->lo="); Serial.print(stop_time-start_time); Serial.println(" microsec"); delay(500); }