Latest Buzz...
                  

Translate

Tuesday, April 3, 2018

Robots go faster with Interrupt Service Routines


Example of an Arduino Uno sketch that uses an interrupt service routine to measure distance with an ultrasonic sensor.
JSN-SR04T DC5V Waterproof Ultrasonic Module Distance Measuring Sensor
JSN-SR04T DC5V Waterproof Ultrasonic Module Distance Measuring Sensor

A small modification allows the sketch to use both the interrupt service routine and the blocking "pulseIn()" statement that causes the processor to wait for the measurement to be completed. Notice the number of times the loop() subroutine is executed while waiting for each measurement to be completed.

Sample output - 
Measurement obtained by interrupt service routine only
======================================================
Number of times loop() executed while measurement being taken: 228
isr_start_occurred = 1, isr_end_occurred = 1, isr_change_occurred = 2
distance from interrupt service routine 28   cm

Number of times loop() executed while measurement being taken: 1360
isr_start_occurred = 1, isr_end_occurred = 1, isr_change_occurred = 2
distance from interrupt service routine 149   cm


Sample output - 
Measurement obtained by both 
interrupt service routine and "pulseIn()" statement
===================================================
Number of times loop() executed while measurement being taken: 1
isr_start_occurred = 1, isr_end_occurred = 1, isr_change_occurred = 2
distance from pulseIn() statement 27   cm
distance from interrupt service routine 28   cm


Number of times loop() executed while measurement being taken: 1
isr_start_occurred = 1, isr_end_occurred = 1, isr_change_occurred = 2
distance from pulseIn() statement 148   cm
distance from interrupt service routine 149   cm

Click on the button below to copy the sample sketch.


0 comments: