Question

In: Physics

Hi, I'm trying to rewrite the code below (code #1) by changing delay() to millis(). void...

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!

Solutions

Expert Solution

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.


Related Solutions

***The code is provided below*** When trying to compile the code below, I'm receiving three errors....
***The code is provided below*** When trying to compile the code below, I'm receiving three errors. Can I get some assistance on correcting the issues? I removed the code because I thought I corrected my problem. I used #define to get rid of the CRT errors, and included an int at main(). The code compiles but still does not run properly. When entering the insertion prompt for the call details, after entering the phone number, the program just continuously runs,...
Invalid entry code in python my code is pasted below. The last elif statement, I'm trying...
Invalid entry code in python my code is pasted below. The last elif statement, I'm trying to get the program to print "invalid entry" if the entry for user_input is invalid. The first user prompt should only allow for numbers 1-10 and "exit" and "quit" import math user_prompt = """Enter number of operation that you want to execute <type exit or quit to end program>: 1 sin(x) 2 cos(x) 3 tan(x) 4 asin(x) 5 acos(x) 6 atan(x) 7 ln(x) 8...
Hi! I have a homework question I'm not sure if I'm doing it correctly. Below is...
Hi! I have a homework question I'm not sure if I'm doing it correctly. Below is the information from the question and my answer in bold. Glass Company makes glass orders based on the customer specifications, so the company uses job costing to track costs. The company uses direct labor hours as the cost driver for manufacturing overhead application. Manufacturing overhead costs for the year:        $787,500 Usage of direct labor hours for the year:               225,000 Beginning Work-in-process, March 1 (Job 57)      $80,000 Beginning...
Question: How to delay this code from 1 second delay to 0.5 second delay? org 0000h;...
Question: How to delay this code from 1 second delay to 0.5 second delay? org 0000h; ljmp main; org 0050h; main:      mov dptr,#SMG_DUAN ;     mov r0,#00h;     mov r1,#0ah; lin1:mov a,r0;     movc a,@a+dptr; get first indexed data in rom to accumolator a     mov p1,a; move data in a to port 1     lcall delay; subroutine call for the delay     inc r0; increase r0 by one to get to the next index     djnz r1,lin1; repeat...
Hi: I'm trying to do a lab called " Making solutions with Alkali Halides (And Breaking...
Hi: I'm trying to do a lab called " Making solutions with Alkali Halides (And Breaking Them). The Lab is by John B. Brady. The lab is about X-ray diffraction and I am suppose to answer a few questions and plot the molar composition vs d-values for KCL and NaCl mixture, as well as the d111 for each sample. I have the data but I honestly am comfused about how to stay this lab. Any ideas would be appreciated. Thank...
The source code I have is what i'm trying to fix for the assignment at the...
The source code I have is what i'm trying to fix for the assignment at the bottom. Source Code: #include <iostream> #include <cstdlib> #include <ctime> #include <iomanip> using namespace std; const int NUM_ROWS = 10; const int NUM_COLS = 10; // Setting values in a 10 by 10 array of random integers (1 - 100) // Pre: twoDArray has been declared with row and column size of NUM_COLS // Must have constant integer NUM_COLS declared // rowSize must be less...
Question -Please rephrase the passage below . i'm trying to elimate plagrism I'm part of an...
Question -Please rephrase the passage below . i'm trying to elimate plagrism I'm part of an energetic entrepreneurial team aspiring to create a difference in the field of Tech platforms. We are currently working on a software platform that aims to integrate range of media dvices like laptops, mobiles, video recorders and other devices. With growing number of devices in our daily life, it's becoming increasingly complex to manage all devices separately. With this advent, we aim to eliminate the...
USING C++ ONLY. Please study the code posted below. the goal is to rewrite the code...
USING C++ ONLY. Please study the code posted below. the goal is to rewrite the code implementing a template class using a linked list instead of an array. Note: The functionality should remain the same. /** * Queue implementation using linked list C style implementation ( no OOP). */ #include <cstdio> #include <cstdlib> #include <climits> #include <iostream> #define CAPACITY 100 // Queue max capacity using namespace std; /** Queue structure definition */ struct QueueType { int data; struct QueueType *...
Hi! Below is the case study and I want to make sure I'm on the right...
Hi! Below is the case study and I want to make sure I'm on the right track. I bolded the questions I'm interested in (1-5). Looking for people who are familiar with the DSM-5/abnormal psychology and able to answer all questions completely. Thank you! Detailed answers extremely appreciated! Questions: 1. Diagnosis; what is the evidence for it? 2. Treatment; typical treatment used for this diagnosis AND most effective treatment. IF the person is in treatment, what should we target first...
Hi! Below is the case study and I want to make sure I'm on the right...
Hi! Below is the case study and I want to make sure I'm on the right track. I bolded the questions I'm interested in (1-5). Looking for people who are familiar with the DSM-5/abnormal psychology and able to answer all questions completely. Thank you! - no ICD 10 please! Questions: 1. Diagnosis; what is the evidence for it? For this one, assume the patient has borderline disorder and answer the rest accordingly. 2. Treatment; typical treatment used for this diagnosis...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT