#define LIGHT_SENSOR_PIN 36 // ESP32 pin GIOP36 (ADC0)
boolean countStatus;
int beat, bpm;
unsigned long millisBefore;
float mockupTime;
float mockupInc = 1;
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
randomSeed(analogRead(0));
}
void loop() {
// reads the input on analog pin (value between 0 and 4095)
// read the input on analog pin 36:
// int sensorValue = analogRead(LIGHT_SENSOR_PIN);
// Temporary use of mockup function
int sensorValue = mockupPulse();
if (millis() % 100 == 1) {
// Serial.print("Analog Value = ");
// Serial.println(sensorValue); // the raw analog reading
}
if (countStatus == 0) {
//800 - 3200 = Light - Bright
if (sensorValue > 3200) {
countStatus = 1;
beat++;
Serial.println("Beat detected!");
Serial.print("Beat : ");
Serial.println(beat);
}
} else {
// <800 = Dim
if (sensorValue < 800) {
countStatus = 0;
}
}
//15000 ms = 1/4 min => beat * 4 for bpm
if (millis() - millisBefore > 15000) {
bpm = beat * 4;
beat = 0;
Serial.print("BPM : ");
Serial.println(bpm);
millisBefore = millis();
}
delay(1);
}
// Mockup function for sensor values
int mockupPulse() {
// Every ten seconds we change the tempo in a random way
if (millis() % 10000 == 1) {
mockupInc = (mockupInc + random(20, 180) / 100.0) / 2;
}
mockupTime += mockupInc;
int iTime = (int) mockupTime;
return (iTime % 1000) * 4;
}