my project is about displaying heart rate per min and temp on a tft screen and sending a warning to an other arduino if it pushes the limit by RF
its all working but there is something wrong
as the pulses some times drops to 30 and some times to 200
isn't that weird ?
i made the sensor my self
here is the code
in ZIP file
the lm35 cant read body temp in a good way and its not accurate at all
so i added 4 to the total i have no idea why just to make it reasonable
It's a totally weird dealing with interrupt handling, volatile variables and normal code interacting with each other. Not synced in any way. I believe you, that the calculated results have more of a random number generator than of a heart rate monitor.
aymanbreak:
that was my firs code to right i am no pro
But the file's name is "final work"
Im sorry, but I'm just not sure this project is worth picking apart every little wrong detail. You have 2 projects in that zip folder which in no way interact with each other, you are trying to analogRead from a digital pin which is not even setup with pinMode() and even if you did it would not work. In the Interrupt file I am seeing undeclared variables and missing libraries, pins not set up.
You would be better off trashing the entire project or do a SERIOUS overhaul of it...
the project is fully working but the problems are with the delays and so its not in sync
its made of 3 projects
one is for the pulse sensor alone which has the Interrupt file with it
and for the tft i used an example from a library
and for the temp i used the regular code http://www.spelecshop.com/1402480/563382/Touch-Panel-Display-for-Arduino.html
here is where i got the library
If it is your opinion that the "project is fully working" most of the time, we need no further discussion.
If it is good enough for you, that you are reading raw data every 2 milliseconds during an interrupt and then send every 20 milliseconds the last read raw data to somewhere else from the loop, do weird calculations in a interrupt service routine which runs for much too long time, and that the weird data will be displayed correctly on a nice graphics display: Well done!
Fix what you believe to be minor sync problems and ready you are!
But in my opinion your code will need a
in most aspects different data and program structure
completely different algorithm for beat detection
much shorter running interrupt handling routine
so that what you name the "final work" would need a nearly complete rewrite to make it "fully working" if this was my project.
But it's up to you what you want: Try to "fix a little bit" what you think is mostly "fully working". Or throw away a lot of the existing code and rewrite most of it.
P.S.: The temperature is NOT the complicated and time critical part in your project.
aymanbreak:
i didn't write the interrupt
here is where i got it from
is it that bad ?
Yes, Sir!
The interrupt handling routine looks like it was programmed by someone who is not a programmer. At least no programmer for microcontrollers. The execution time of that interrupt handling is much too long. Too many calculations done in the interrupt handling routine. Too many 'volatile' variables used.
Perhaps the programming was done by a 'heart researcher'.
Heart researchers are specialists for the human heart, but not for programming microcontrollers, I think.
If you were interested in a mostly different approach for
retrieving raw data by using timer interrupts
pulse/beat rate detection calculations
I would create some new routines for you to do so.
First thing I woul do is that:
Increase the timer rate to 1000 per second
Speed up the interrupt handling for high-speed interrupt handling
put all the calculations into the "normal" code outside the interrupts
Please let me know if you need some code to play with and I'll write some for you.
If you like we could handle it like that:
Step #1: My turn
At first I would create the "raw data reading" for 'analogRead(pulsePin)' with high-speed interrupt handling, so that the raw data (all of them!) can be sent to Serial in realtime.
Step #2: Your turn
2. As a second step, you aquire some raw data (one or two minutes) with your sensor in your Serial monitor, copy the raw data to a .txt or .ino file and post the file as a file attachment into this forum
Step #3: My turn
3. As a third step I would then create the pulse/beat rate calculations for your sensor
Step #4: Your turn
4. And at last you put it all together with your temperature reading and LCD output, perhaps with a little help.
Just let me know if I shall start with step #1 and I'll do so.
aymanbreak:
that would be great
can you give me a code to start with
So OK, let's set the state of your program from 'finished' back to 'start' and let's begin.
As I will have to think a little bit about step-#1 (and write code for it), at first I will send you a completely different code for data reading from your pulse sensor that I have ready.
The code will do that:
initialize Serial at a baudrate of 115200
print a starting message
read 1000 data samples per second and print data to Serial
after 120 seconds the program stops and sends a final message
Don't worry as you only can see data in the range 0...255 and not in the range 0...1023, as I have reduced the ADC resolution to 8 bit, while increasing the data rate to 1000 per second. So let's see what data your sensor can provide.
Would you please run this piece of code, open the Serial monitor at 115200 baud, and after 120 seconds when the final message appears:
copy the contents of the Serial monitor into a text file
save the file and attach the data file as a file attachment to a forums posting?
Here is the first test code to read raw data:
#define PULSEPIN A0 // Pulse Sensor purple wire connected to analog pin A0
void setup() {
Serial.begin(115200);
Serial.println();
Serial.println("Start reading data");
}
unsigned long lastADCsample;
unsigned long numSamples;
void loop()
{
long now=micros();
if (now> 30000000L) // 30000000L µs = 30 seconds
{ // stop program after 30 seconds
Serial.print("Time: ");Serial.println(now/1000000.0,6);
Serial.print("Samples: ");Serial.println(numSamples);
Serial.println("Finished - END");
while(1);
}
else if (now-lastADCsample>=1000)
{
lastADCsample+=1000;
Serial.println(analogRead(PULSEPIN)>>2); // reduce ADC resolution to 8 bit
numSamples++;
}
}
We then can decide if the raw data from your sensor can be used for calculations or if your measuring circuit has to be optimized first.
Edit-1: I think 30 seconds should be enough, so I edited the code for a data interval of 30 seconds instead of 120 seconds.
Edit-2: You know how to copy data from the Serial monitor to a text file? If your operating system is Windows and you don't know, I could explain.
i did it 3 times
the first time had no seance in it
and the second one as well but on the third try i un plugged the laptop adapter and it was reasonable
edit 1 : i added an other try called try 4 and it might be the best so far
aymanbreak:
i did it 3 times
the first time had no seance in it
and the second one as well but on the third try i un plugged the laptop adapter and it was reasonable
edit 1 : i added an other try called try 4 and it might be the best so far
So let's see what data you have sent.
Your try1.txt shows a fantastic pulse pattern, very well done:
Unfortunately: That pulses cannot be human heartbeats.
So what is these fine pulses: When I count the difference (1ms from sample to sample) I find out, that ca. every 17 ms is one period. This fits wonderfully to the "heart beat" of a 60 Hz AC mains power.
1/60s = 16,67 ms
And that's the "heart beat" we can easily detect from your try1.txt: Your mains power is at a frequency of 60 Hz.
So you must live in a different region of the world. I live in Germany, Europe. Mains power here is 230V/50 Hz. With 50 Hz the period would be at 20 ms between "heart beats". But as your "heart beats" are only around 16.67 ms from one beat to the other, you should have a mains power supply at 60 Hz AC.
Am I right?
Edit: try2.txt shows much less pulse amplitude from the mains power, but the 60 Hz pulse is still there. While at the beginning the samples pulse between ca. 83/93, the noise is reduced after a few seconds to a pulse between 40/50 and at the end of the 30 second sampling period the pulses fall to around 33/46 samples.
Now let's have a look on the data in try3.txt and try4.txt.
These two tries show also a pulse pattern.
We can see longer times of lower values as well as longer times of higher values.
That might be a heart beat signal!
Although the signal seems not to be very well: Within the 0...255 range, the signal hardly falls below 60 or rises above 185, so at the lower end 25% of the range are not used as well as the upper 25% of the measuring range.
But I think it should be possible to detect a pulse/beat signal within that data pattern.
thank you very much you've been so helpful to me i can never find a way to repay you
yes i live in the middle east where here the power source il 60hz
and the other two were on the lap battery
aymanbreak:
yes i live in the middle east where here the power source il 60hz
I suppose, it's for the first time that you can see the actual realtime data with a 1ms timing resolution that your sensor is providing?
Anyway.
I'm now working out something for step#1 and then will go on to step#3.
As I'm not having your sensor, it would be helpful to have data in a format that can easily be included in an Arduino sketch, so that I can have a "data simulation".
For providing simulation data I will now post a slightly modified code, that creates a different output on Serial:
#define PULSEPIN A0 // Pulse Sensor purple wire connected to analog pin A0
void setup() {
Serial.begin(115200);
Serial.println();
Serial.println("Start reading data");
}
unsigned long lastADCsample;
unsigned long numSamples;
byte samplesPerLine;
void loop()
{
long now=micros();
if (now> 30000000L) // 30000000L µs = 30 seconds
{ // stop program after 30 seconds
Serial.println();
Serial.print("Time: ");Serial.println(now/1000000.0,6);
Serial.print("Samples: ");Serial.println(numSamples);
Serial.println("Finished - END");
while(1);
}
else if (now-lastADCsample>=1000)
{
lastADCsample+=1000;
Serial.print(analogRead(PULSEPIN)>>2); // reduce ADC resolution to 8 bit
Serial.print(", ");
numSamples++;
samplesPerLine++;
if (samplesPerLine>=30)
{
Serial.println();
samplesPerLine=0;
}
}
}
If possible, please send another set of 'good' data samples as a file attachment, so I can use them for data input simulation without having the sensor ready.
I don't know on what principle and circuit your sensor works, is it something like that.
Yes it's more like the second link
here is the first try and i guess it was a success.
Is measuring oxygen in blood or pressure or even glucose possible ?
I want to make something that i feel it's special.
And thank you again
That looks like its working and has some real usable data!
If you just want to be simple about it, look at your values. I am noticing that you have 2 kinds of heart beats, near the beginning they are kinda of weak peaking at around 120-130 and strong beats peaking at around 160-180, however, your non-beats seems to be less than 100.
SO! You can set 2 thresholds, one threshold to count as a beat (try >= 115 as a threshold) and one threshold to count as the beat is finished (try <= 105). Just remember to keep still otherwise you may get slightly skewed results.