Hello, I have to write a code for lilygo esp (ttgo t-display) which shows on the screen the stress level calculated with the data from a simple pulse sensor and a gsr sensor. Under stress level also to show the beat per minute (bpm) and the data from bpm. Could anyone help me?
Thank you.
Yes, there are many people in this forum who can help you. But don't forget we are not in the room with you, we can't see what you have in front of you. So please read the forum guide in the sticky post so you know all the things you need to post and how to post them.
Is this a school/college project? How long do you have to complete it?
Yes, of course. Here are your next steps:
- read the pulse sensor
- read the gsr sensor
- calculate the stress level
- show the stress level on the screen
- get the bpm
- show the bpm
Now you can start coding one step after another.
Start by researching what is out in the Ether...
Arduino "example" + "pulse-sensor " + "gsr-sensor" at DuckDuckGo
Find a project that looks similar for your need per sensor
Test each code independently with your sensor.
Combine the codes into one sketch specific to your hardware.
Debug.
Come back to forum with your code and ask specific question.
Note:
When you are working with other's code consider if/how code can be improved. Try to edit the code to match your specific needs. Do this before attempting to combine (forum has many post on combining code.)
#include <Wire.h>
#include <TFT_eSPI.h>
TFT_eSPI tft = TFT_eSPI(); // Initialize TFT object
int sensorPin = 36; // Pulse sensor pin
unsigned long lastBeatTime = 0;
unsigned long thisBeatTime = 0;
bool beatInProgress = false;
int beatsPerMinute = 0;
#define GSR_PIN 32
#define GSR_SAMPLE_COUNT 100
float stressLevel = 0; // Variable to store stress level
void setup() {
Serial.begin(115200);
tft.init();
tft.setRotation(1);
tft.fillScreen(TFT_BLACK);
tft.setTextSize(2);
}
void loop() {
tft.fillScreen(TFT_BLACK);
int sensorValue = analogRead(sensorPin);
if (sensorValue > 400 && !beatInProgress) {
thisBeatTime = millis();
beatInProgress = true;
}
else if (sensorValue < 400 && beatInProgress) {
lastBeatTime = thisBeatTime;
thisBeatTime = millis();
beatsPerMinute = 60000 / (thisBeatTime - lastBeatTime);
beatInProgress = false;
}
uint32_t gsrValue = 0;
for (int i = 0; i < GSR_SAMPLE_COUNT; i++) {
gsrValue += analogRead(GSR_PIN);
}
gsrValue /= GSR_SAMPLE_COUNT;
float voltage = map(gsrValue, 0, 4095, 0, 3300) / 1000.0;
if (beatsPerMinute > 0 && voltage > 0) {
stressLevel = ((beatsPerMinute - 60) / 140) * 100 + (voltage * 10); // Calculate stress level
}
// Display BPM, GSR, and stress level on TFT screen
tft.setCursor(0, 0);
tft.setTextColor(TFT_WHITE);
tft.printf("BPM: %d ", beatsPerMinute);
tft.printf("GSR: %.3f V", voltage);
tft.setCursor(0, 30);
tft.setTextColor(TFT_RED);
tft.printf("Stress Level: %.2f", stressLevel);
delay(100);
}
This is my code, the problem is that on the screen even thought I used that formula for the stress monitor, it doesn't show the right answer, for example I have the bmp 122 and GSR 1.9 and the stress level should be around 60, but on the display is 31
Where did "that formula" come from? How were the numbers chosen?
Division result is truncated to int 0. Type cast float(beatsPerMinute) or make any of the constants float, e.g. 60.0
thank you
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.