/* Program made by Daniele Mari on 8.04.2024 */ #include // Variables LiquidCrystal_I2C lcd(0x27, 16, 2); const int PulseWire = 0; // PulseSensor PURPLE WIRE connected to ANALOG PIN 0 const int LED = LED_BUILTIN; // The on-board Arduino LED, close to PIN 13. int Threshold = 520; // Determine which Signal to "count as a beat" and which to ignore. int mean_signal [50] = {0}; // mean of the signal to determine threshold int i = 0; // variable for increment int j = 0; // variable for increment long temp = 0; // temporary variable for the threshold int Signal; // Signal of the sensor unsigned long Time_now = 0; // actual time unsigned long Time_old = 0; // old time double interval_time_s = 0; // interval between 2 pulse unsigned int BPM = 0; // Pulse per minute bool wait_next = false; // will wait to be under the threshold before to look again for pulse void setup() { Serial.begin(9600); // For Serial Monitor lcd.init(); // init lcd lcd.backlight(); // turn on the backlight lcd.clear(); // clear the LCD (avoid to have any old thing to be printed on lcd) } void loop() { Signal = analogRead(PulseWire); // read the sensor signal Serial.println(Signal); // print the signal into plotter // do the mean for new threshold mean_signal [i] = Signal; i++; if(i>49) { for(i = 0; i<=49; i++) { temp += mean_signal [i]; } Threshold = temp/50 + 20; temp = 0; i = 0; } //**************************** // If the signal is above threshold, then "turn-on" Arduino's on-Board LED and calculate BPM if(Signal > Threshold && !wait_next) { digitalWrite(LED,HIGH); Time_now = millis(); interval_time_s = (Time_now - Time_old); // avoid value out of range if (interval_time_s > 300) { BPM = (1/(interval_time_s/1000))*60; Time_old = Time_now; wait_next = true; lcd.setCursor(0, 0); lcd.print("BPM : "); lcd.setCursor(6, 0); lcd.print(BPM); // set up RGB led color } } else { digitalWrite(LED,LOW); // if sensor signal is below threshold, "turn-off" internal LED. wait_next = false; } delay(20); // small idle time to not overkill micro-controller }