In: Physics
Hi,
I'm trying to rewrite the code below (code #1) by changing delay()
to millis().
void loop() {
// Print the value inside of myBPM.
Serial.begin(9600);
int myBPM = pulseSensor.getBeatsPerMinute(); // Calls function on
our pulseSensor object that returns BPM as an "int".
// "myBPM" hold this BPM value now.
if (pulseSensor.sawStartOfBeat()) { // Constantly test to see if
"a beat happened".
Serial.println("♥ A HeartBeat Happened ! "); // If test is "true",
print a message "a heartbeat happened".
Serial.print("BPM: "); // Print phrase "BPM: "
Serial.println(myBPM); // Print the value inside of myBPM.
lcd.setCursor(0, 0);
lcd.print("HeartBeat Found!");
lcd.setCursor(4, 1);
lcd.print("BPM: "); lcd.print(myBPM);
delay(1600);
lcd.clear();
}
delay(20); // considered best practice in a simple sketch.
}//end loop
I have written it here (shown in code #2)
const int PulseWire = 0; // PulseSensor PURPLE WIRE connected to ANALOG PIN 0
int Threshold = 550; // Determine which Signal to "count
as a beat" and which to ignore.
// Use the "Gettting Started Project" to fine-tune Threshold Value
beyond default setting.
// Otherwise leave the default "550" value.
int prev_delay = 0;
int delayInterval = 20;
int prev_HRmillis = 0;
int intervalHR = 1200;
PulseSensorPlayground pulseSensor; // Creates an instance of the
PulseSensorPlayground object called "pulseSensor"
void loop(){
unsigned long current_delayInterval = millis();
if (current_delayInterval - prev_delay >= delayInterval)
prev_delay = current_delayInterval;
{
unsigned long current_HRmillis = millis();
int myBPM = pulseSensor.getBeatsPerMinute(); // Calls function
on our pulseSensor object that returns BPM as an "int".
// "myBPM" hold this BPM value now.
if (pulseSensor.sawStartOfBeat() == true) {
if (current_HRmillis - prev_HRmillis >= intervalHR) {
//saved the last time temp read value
prev_HRmillis = current_HRmillis;
// Constantly test to see if "a beat happened".
Serial.println("♥ A HeartBeat Happened ! "); // If test is "true",
print a message "a heartbeat happened".
Serial.print("BPM: "); // Print phrase "BPM: "
Serial.println(myBPM); // Print the value inside of myBPM
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Your heartbeat!");
lcd.setCursor(4, 1);
lcd.print("BPM: "); lcd.print(myBPM);
}
}
}
}
}
On the code #1, when the sensor senses a beat, it shows the BPM
on the LCD. When the sensor is not worn, the LCD goes clear.
Rewriting the code does to millis() still does a good job reading
the BPM but the LCD does not clear when the sensor is not worn. I
think I have trouble rewriting the delay(20) to a millis()
function. Can anyone help me with this? I still have trouble
grasping the concept of the millis() function.I got used to using
the delay() function as to me it is more straightforward.
Thank you very much!
In the first line, after the if command you have to keep "prev_delay = current_delayInterval;" inside the if statement brackets. millis() function just return the amount time in milliseconds that has passed since the microcontrolled has been turned on. Delay function also uses millis in its defination with some other check cases, which lowers the performance of the delay() function.
For converting delay to millis, you have to use the if statement and you have to measure time using millis() at two different points in a code (This if statement should be inside loop()). We have to compare the delay in time between two points to a predefined interval, and take a decision accordingly to have a delay like behaviour.
For clearing the lcd, You have to run at in the end of the loop as you have done in code #1, in code #2 you have changed to order which is resulting in lcd not getting cleared after the loop, it still have contents. Make the order similar to code #1.