A library made in CPP for Arduino for simulating heart beat lub dub. You can connect your LEDs or Motors to flash to a sound of LUB and DUB of a heartbeat.
- Add
#include <ArduinoHeartLubDub.h>
at the top of your file - Define 4 function
lub
,afterLub
,dub
,afterDub
- Define
bpm
value for controlling the speed of the blink. - Add
cardiacCycle
function execution in theloop
void loop() { cardiacCycle( bpm, lub, afterLub, dub, afterDub ); }
#include <ArduinoHeartLubDub.h>
/**
* LED OUTPUT PIN
* */
int ledPin = 10;
/**
* Set the bpm of the led
*/
int bpm = 60;
/**
* Cardiac cycle - LUB
*/
void lub()
{
analogWrite(ledPin, 255);
}
/**
* Cardiac cycle - After LUB is done
*/
void afterLub()
{
analogWrite(ledPin, 0);
}
/**
* Cardiac cycle - DUB
*/
void dub()
{
analogWrite(ledPin, 60); // <-- make it a bit dimmer on second flash
}
/**
* Cardiac cycle - After DUB is done
*/
void afterDub()
{
analogWrite(ledPin, 0);
}
void setup()
{
pinMode(ledPin, OUTPUT);
}
void loop()
{
// This needs to be executed every loop
// It's not thread blocking
// Unless you block the thread in the lub, afterLub, dub, afterDub functions
cardiacCycle(
bpm,
lub,
afterLub,
dub,
afterDub);
}
Checkout the demo on Wokwi
I have a website where I documented the project, if you want to get some insights or try Simulated DEMO you can check it out in my cookbook.