Devices & Components
Arduino Uno Rev3
Software & Tools
Arduino IDE
Project description
Code
Heart Beat code
c_cpp
A simple way and example of how you are able to add a visible heart beat indicator into your sketches to show they are alive & kicking.
1// 2// Example of a heart_beat code for use in any sketch where a visual indication is 3// required that the sketch is operating. 4// 5// Ron D Bentley, Stafford, UK, July 2021 6// 7// This example and code is in the public domain and 8// may be used without restriction and without warranty. 9// 10// Note that 11// 1. this example uses the LED_BUILTIN digital pin, normally digital pin 13, 12// so no wiring or external LED is needed to implement this code. 13// However, if an external LED is wished, then simply decide which digital pin 14// is to be used, change to definition 'hear_beat_pin' to be this pin mumber 15// and wire in the external LED. 16// 17 18#define heart_beat_pin LED_BUILTIN // digital pin for heart beat LED 19 20long unsigned heart_beat_freq = 1000; // time(milliseconds) of heart beat frequency 21 22long unsigned heart_beat_on_off_time; // the time the LED is on and off - 1/2 frequency 23long unsigned last_heart_beat_time; // time in milliseconds of last heart beat status change 24bool heart_beat_status = HIGH; // current status of heart beat, start high 25 26// 27// Function handles the heart beat cycle. 28// May be called from anywhere, but at least every main loop cycle. 29// 30void heart_beat() { 31 if (millis() - last_heart_beat_time >= heart_beat_on_off_time) { 32 // time to swap status of the heart beat LED and update it 33 last_heart_beat_time = millis(); 34 heart_beat_status = !heart_beat_status; // invert current heart beat status value 35 digitalWrite(heart_beat_pin, heart_beat_status); // update LED with new status 36 } 37} 38 39void setup() { 40 // setup heart beat 41 pinMode(heart_beat_pin, OUTPUT); 42 heart_beat_on_off_time = heart_beat_freq / 2; // LED is on and off at 1/2 frequency time 43 // end of heart beat setup 44} 45 46void loop() { 47 heart_beat(); 48 // add your other code here... 49 50 51} 52
Heart Beat code
c_cpp
A simple way and example of how you are able to add a visible heart beat indicator into your sketches to show they are alive & kicking.
1// 2// Example of a heart_beat code for use in any sketch where a visual 3 indication is 4// required that the sketch is operating. 5// 6// Ron D Bentley, 7 Stafford, UK, July 2021 8// 9// This example and code is in the public domain 10 and 11// may be used without restriction and without warranty. 12// 13// Note 14 that 15// 1. this example uses the LED_BUILTIN digital pin, normally digital pin 16 13, 17// so no wiring or external LED is needed to implement this code. 18// 19 However, if an external LED is wished, then simply decide which digital pin 20// 21 is to be used, change to definition 'hear_beat_pin' to be this pin mumber 22// 23 and wire in the external LED. 24// 25 26#define heart_beat_pin LED_BUILTIN 27 // digital pin for heart beat LED 28 29long unsigned heart_beat_freq = 1000; 30 // time(milliseconds) of heart beat frequency 31 32long unsigned heart_beat_on_off_time; 33 // the time the LED is on and off - 1/2 frequency 34long unsigned last_heart_beat_time; 35 // time in milliseconds of last heart beat status change 36bool heart_beat_status 37 = HIGH; // current status of heart beat, start high 38 39// 40// Function 41 handles the heart beat cycle. 42// May be called from anywhere, but at least every 43 main loop cycle. 44// 45void heart_beat() { 46 if (millis() - last_heart_beat_time 47 >= heart_beat_on_off_time) { 48 // time to swap status of the heart beat LED 49 and update it 50 last_heart_beat_time = millis(); 51 heart_beat_status = 52 !heart_beat_status; // invert current heart beat status value 53 digitalWrite(heart_beat_pin, 54 heart_beat_status); // update LED with new status 55 } 56} 57 58void setup() 59 { 60 // setup heart beat 61 pinMode(heart_beat_pin, OUTPUT); 62 heart_beat_on_off_time 63 = heart_beat_freq / 2; // LED is on and off at 1/2 frequency time 64 // end of 65 heart beat setup 66} 67 68void loop() { 69 heart_beat(); 70 // add your other 71 code here... 72 73 74} 75
Downloadable files
External LED Heart Beat Indicator Circuit
Circuit to Implement an external LED heart beat indicator if the on-board LED (LED_BUILTIN) is not to be used.
External LED Heart Beat Indicator Circuit

Comments
Only logged in users can leave comments