Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Wire.h>
- #include "MAX30105.h"
- class FilterBuBp2
- {
- public:
- FilterBuBp2()
- {
- for(int i=0; i <= 4; i++)
- v[i]=0.0;
- }
- private:
- float v[5];
- public:
- float step(float x) //class II
- {
- v[0] = v[1];
- v[1] = v[2];
- v[2] = v[3];
- v[3] = v[4];
- v[4] = (5.586996620231363987e-3 * x)
- + (-0.80080264666570755150 * v[0])
- + (3.37021291056280425380 * v[1])
- + (-5.33686791231501622690 * v[2])
- + (3.76742616976629829395 * v[3]);
- return
- (v[0] + v[4])
- - 2 * v[2];
- }
- };
- FilterBuBp2 c_filter;
- long present, seconds, past, green, past2, present2, pastacc, presentacc, full_sum, sum;
- int hr_threshold=5;
- bool beatG;
- int flag;
- long hrv_arr[70];
- int hrv_max=0;
- int hrv_index=0;
- int gyro_count=0;
- MAX30105 particleSensor;
- const int MPU = 0x68;
- float GyroX, GyroY, GyroZ, GyroX2, GyroY2, GyroZ2, voltage;
- int movement=0;
- int move_arr[10];
- bool smaller=false;
- long past_g=0;
- void setup()
- {
- Serial1.begin(9600);
- // Initialize sensor
- if (!particleSensor.begin(Wire, I2C_SPEED_FAST)) //Use default I2C port, 400kHz speed
- {
- Serial1.println("MAX30105 was not found.");
- while (1);
- }
- //Setup to sense a nice looking saw tooth on the plotter
- byte ledBrightness = 0x3F; //Options: 0=Off to 255=50mA
- byte sampleAverage = 4; //Options: 1, 2, 4, 8, 16, 32
- byte ledMode = 3; //Options: 1 = Red only, 2 = Red + IR, 3 = Red + IR + Green
- int sampleRate = 400; //Options: 50, 100, 200, 400, 800, 1000, 1600, 3200
- int pulseWidth = 411; //Options: 69, 118, 215, 411
- int adcRange = 4096; //Options: 2048, 4096, 8192, 16384
- particleSensor.setup(ledBrightness, sampleAverage, ledMode, sampleRate, pulseWidth, adcRange); //Configure sensor with these settings
- particleSensor.setPulseAmplitudeRed(0x00);
- particleSensor.setPulseAmplitudeIR(0x00);
- particleSensor.setPulseAmplitudeGreen(0xFF);
- past = millis();
- past2 = millis();//send data every 0.5s
- pastacc = millis();
- Wire.endTransmission(true);
- Wire.beginTransmission(MPU); // Start communication with MPU6050 // MPU=0x68
- Wire.write(0x6B); // Talk to the register 6B
- Wire.write(0x00); // Make reset - place a 0 into the 6B register
- Wire.endTransmission(true); //end the transmission
- Wire.beginTransmission(MPU);
- Wire.write(0x1B);
- Wire.write(0x18);
- Wire.endTransmission(true);
- }
- void loop()
- {
- green=c_filter.step(particleSensor.getGreen());
- if (past_g<green) {
- flag++;
- }
- else {
- flag=0;
- }
- if (flag>hr_threshold) {
- present=millis();
- if ((present-past) > 450 && (present-past) < 1000) {
- seconds=60000/(present-past);
- hrv_arr[hrv_index]=present-past;
- hrv_index++;
- /*if (hrv_max<10) {
- hrv_max++;
- }*/
- }
- past=millis();
- if (hrv_index==10) {
- hrv_index=0;
- }
- }
- presentacc=millis();
- Wire.beginTransmission(MPU);
- Wire.write(0x43);
- Wire.endTransmission(false);
- Wire.requestFrom(MPU, 6, true); // Read 4 registers total, each axis value is stored in 2 registers
- GyroX = (Wire.read() << 8 | Wire.read()) / 131.0;
- GyroY = (Wire.read() << 8 | Wire.read()) / 131.0;
- GyroZ = (Wire.read() << 8 | Wire.read()) / 131.0;
- GyroX2+=GyroX*(presentacc-pastacc)/1000;
- GyroY2+=GyroY*(presentacc-pastacc)/1000;
- GyroZ2+=GyroZ*(presentacc-pastacc)/1000;
- pastacc=millis();
- voltage = analogRead(PA3);
- voltage = voltage * 3.3 / 4096;
- present2=millis();
- if (present2-past2 > 480) {
- for (int i = 0; i < 9; i++) {
- full_sum+=pow(hrv_arr[i]-hrv_arr[i+1],2);
- }
- sum=full_sum/9;
- sum=sqrt(sum);
- Serial1.print("hr=");
- Serial1.print(seconds);
- Serial1.print(",sv=");
- Serial1.print(voltage);
- move_arr[gyro_count]=(int)(GyroX2+GyroY2);
- movement=0;
- for (int i = 0; i < 10; i++) {
- movement+=move_arr[i];
- }
- gyro_count++;
- if (gyro_count==10) {
- gyro_count=0;
- }
- GyroX2=0;
- GyroY2=0;
- GyroZ2=0;
- // default dry skin is around 0.5-0.6, wet skin around 0.9-1.1, threshold should be 0.7-0.8
- Serial1.print(",mm=");
- Serial1.print(movement);
- Serial1.print(",var=");
- Serial1.print(sum);
- Serial1.print(",");
- full_sum=0;
- sum=0;
- past2=millis();
- }
- past_g=green;
- }
Advertisement
Add Comment
Please, Sign In to add comment