/* PulseSensorâ„¢ Starter Project and Signal Tester * The Best Way to Get Started With, or See the Raw Signal of, your PulseSensorâ„¢ & Arduino. * * Here is a link to the tutorial * https://pulsesensor.com/pages/code-and-guide * * WATCH ME (Tutorial Video): * https://www.youtube.com/watch?v=82T_zBZQkOE * * ------------------------------------------------------------- 1) This shows a live human Heartbeat Pulse. 2) Live visualization in Arduino's Cool "Serial Plotter". 3) Blink an LED on each Heartbeat. 4) This is the direct Pulse Sensor's Signal. 5) A great first-step in troubleshooting your circuit and connections. 6) "Human-readable" code that is newbie friendly." */ // Variables int PulseSensorPurplePin = 0; // Pulse Sensor PURPLE WIRE connected to ANALOG PIN 0 int LED13 = 13; // The on-board Arduion LED int Signal; // holds the incoming raw data. Signal value can range from 0-1024 int Threshold = 600; // Determine which Signal to "count as a beat", and which to ingore. int ANIMDELAY = 100; // animation delay, deafault value is 100 int INTENSITYMIN = 0; // minimum brightness, valid range [0,15] int INTENSITYMAX = 8; // maximum brightness, valid range [0,15] int DIN_PIN = 3; // data in pin int CS_PIN = 5; // load (CS) pin int CLK_PIN = 6; // clock pin // MAX7219 registers byte MAXREG_DECODEMODE = 0x09; byte MAXREG_INTENSITY = 0x0a; byte MAXREG_SCANLIMIT = 0x0b; byte MAXREG_SHUTDOWN = 0x0c; byte MAXREG_DISPTEST = 0x0f; const unsigned char heart[] = { B01100110, B11111111, B11111111, B11111111, B01111110, B00111100, B00011000, B00000000 }; // The SetUp Function: void setup() { pinMode(LED13,OUTPUT); // pin that will blink to your heartbeat! Serial.begin(9600); // Set's up Serial Communication at certain speed. pinMode(DIN_PIN, OUTPUT); pinMode(CLK_PIN, OUTPUT); pinMode(CS_PIN, OUTPUT); // initialization of the MAX7219 setRegistry(MAXREG_SCANLIMIT, 0x07); setRegistry(MAXREG_DECODEMODE, 0x00); // using an led matrix (not digits) setRegistry(MAXREG_SHUTDOWN, 0x01); // not in shutdown mode setRegistry(MAXREG_DISPTEST, 0x00); // no display test setRegistry(MAXREG_INTENSITY, 0x0f & INTENSITYMIN); // draw hearth setRegistry(1, heart[0]); setRegistry(2, heart[1]); setRegistry(3, heart[2]); setRegistry(4, heart[3]); setRegistry(5, heart[4]); setRegistry(6, heart[5]); setRegistry(7, heart[6]); setRegistry(8, heart[7]); } // The Main Loop Function void loop() { Signal = analogRead(PulseSensorPurplePin); // Read the PulseSensor's value. // Assign this value to the "Signal" variable. Serial.println(Signal); // Send the Signal value to Serial Plotter. if(Signal > Threshold){ // If the signal is above "550", then "turn-on" Arduino's on-Board LED. setRegistry(MAXREG_INTENSITY, 0x0f & INTENSITYMAX); delay(ANIMDELAY); // switch off setRegistry(MAXREG_INTENSITY, 0x0f & INTENSITYMIN); delay(ANIMDELAY); // second beat // setRegistry(MAXREG_INTENSITY, 0x0f & INTENSITYMAX); // delay(ANIMDELAY); // switch off // setRegistry(MAXREG_INTENSITY, 0x0f & INTENSITYMIN); // delay(ANIMDELAY*6); // Else, the sigal must be below "550", so "turn-off" this LED. } delay(10); } void setRegistry(byte reg, byte value) { digitalWrite(CS_PIN, LOW); putByte(reg); // specify register putByte(value); // send data digitalWrite(CS_PIN, LOW); digitalWrite(CS_PIN, HIGH); } void putByte(byte data) { byte i = 8; byte mask; while (i > 0) { mask = 0x01 << (i - 1); // get bitmask digitalWrite( CLK_PIN, LOW); // tick if (data & mask) // choose bit digitalWrite(DIN_PIN, HIGH); // send 1 else digitalWrite(DIN_PIN, LOW); // send 0 digitalWrite(CLK_PIN, HIGH); // tock --i; // move to lesser bit } }