我很难在同一草图上显示听觉波形和心跳值。我可以完整显示波形或显示心跳,但不能同时显示两者。这就是我的
#include <TFT.h> // Hardware-specific library
#include <SPI.h>
#define TFT_CS 10
#define TFT_DC 9
#define TFT_RESET 8
int xPos = 0;
//Adafruit_ST7735 lcd = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_RESET);
TFT lcd = TFT(TFT_CS, TFT_DC, TFT_RESET);
void setup() {
pinMode(A0, INPUT); //pin reading PPG signals/converting them into voltage values
pinMode(3, OUTPUT); // pwm pin
Serial.begin(9600); //initializing serial port
lcd.begin();
lcd.setCursor(0, 0);
lcd.print("Place finger on sensor"); //instructing user to put finger in sensor
lcd.setCursor(0, 1);
delay(2000); //wait 2 seconds to allow user to do so
lcd.print("begin calibration");
}
void loop() {
// define all variables
int i = 0; //used for indexing purposes throughout code (esp. when taking voltage readings)
int j = 0;
int graphHeight;
float Rinit; //analog read values during calibration stage
float Vinit; //voltage values calculated during calibration stage
float sumoffset; //summing voltage values over the duration of calibration
float avgoffset; //sumoffset/10 –> average voltage value recorded during calibration
float reading; //analog read values read by A0 by
float voltage; //corresponding voltage values ranging from 0-5 V
float NewVolt; //variable storing voltage values exceeding set threshold
unsigned long currentTime[10] = {0}; //array storing last 10 time points where voltage values exceed set threshold
unsigned long initTime; //recording the time at which each potential “peak” occurs
float timepeak[] = {0, 0}; //most recent and 10th most recent time point at which a potential “peak” occurs
float bpm; //variable storing beats per minute calculations
float diffpeak; //difference in seconds between most recent and 10th most recent time point at which a potential “peak” occurs
float TIME; //instantaneous time value in ms at which
reading = analogRead(A0);
graphHeight = map(reading, 0, 1023, 0, lcd.height() / 3);
lcd.stroke(255, 255, 255);
lcd.line(xPos, lcd.height() - graphHeight, xPos, lcd.height());
if (xPos >= 160) {
xPos = 0;
lcd.background(0, 0, 0);
} else {
xPos++;
}
//xPos++;
//delay(50);
for (i = 5; i > 0; i--) {
lcd.background(0, 0, 0);
lcd.setCursor(0, 0);
lcd.print("Calibrating!!");
lcd.setCursor(0, 5);
lcd.print("");
lcd.setCursor(0, 9);
lcd.print(i); //for loop counts down from 5 to 1 so that user knows that calibration is occuring
delay(1000); //delay of 1 s per iteration (5 s calibration time total)
lcd.print(""); //space set to account for change from 2 character number (10) to 1 character number(9 - 0)
Rinit = analogRead(A0); //analog pin 0 directly reads signal coming from output of gain stages in circuit— record them as values ranging 0 - 1023
Vinit = Rinit * 0.00488; //convert
sumoffset = sumoffset + Vinit;
}
avgoffset = sumoffset / 10; //average value of signal so PWM pin intensity
modulation can be set accordingly
Serial.println(avgoffset);
//reading stage
lcd.background(0, 0, 0);
lcd.setCursor(0, 10);
lcd.print("PPG Readings");
while (1) {
reading = analogRead(A0);
voltage = (reading * 0.00488);
if (voltage >= (0.5 * avgoffset)) { //if a particular voltage value exceeds half of the avergae voltage value recorded during calibration
NewVolt = voltage; //store these potential peaks as “new voltage” points
Serial.println(NewVolt); //recording the time of each proposed “peak”
initTime = millis(); //record time at which each potential peak occurs
//saving the last 10 time points through a “sliding” updating array
currentTime[9] = currentTime[8];
currentTime[8] = currentTime[7];
currentTime[7] = currentTime[6];
currentTime[6] = currentTime[5];
currentTime[5] = currentTime[4];
currentTime[4] = currentTime[3];
currentTime[3] = currentTime[2];
currentTime[2] = currentTime[1];
currentTime[1] = currentTime[0];
currentTime[0] = initTime;
//
for (j = 0; j <= 9; j++) {
Serial.println(currentTime[j]); //serial printing currentTime values for testing purposes— to make sure that the initTime value is constantly updating
Serial.println(j); // save time where NewVolt j occurs
if (currentTime[9] - currentTime[0] >= 100) { //if the difference between most recent and 10 th most recent time point is >= 100 ms(to eliminate fluctuations within signal based on noise)
timepeak[1] = (float) currentTime[9]; //converting unsigned long currentTime values to floating point numbers in order to perform mathematical operations on them
timepeak[0] = (float) currentTime[0];
diffpeak = (timepeak[0] - timepeak[1]) / 10000; //recording the difference between the two points(and dividing by 10 since difference between 10 points are taken) as time duration between“ peaks”
bpm = 60 / diffpeak; //calculate the number of “peaks” per minute from duration( of 1 peak) in seconds
//myStats.add(bpm); //add the bpm quantity to perform statistical operations on
//delay(1000);
//there is an inital delay in displaying accurate bpm values — about 6 - 7 inaccurate ones before consistent ones start
//due to the “sliding” updating currentTime vector —
currentTime[10] = 0
}
}
}
lcd.setCursor(0, 20);
lcd.print(bpm);
lcd.print(" beats/min"); //print beats/min value
delay(5000);
lcd.background(0, 0, 0);
}
}