im making an arduino based CPR.
after counting the compression until 30 in 20 sec. i need to detect the angle of the head tilt, so i need tu run the gyroscope sensor until the angle is incresed 30 degrees.
in my code the gyroscope codes isnt looped, its just like executed once and it stop.
#include <MPU6050_tockn.h>
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE );
MPU6050 mpu6050(Wire);
// this constant won't change:
const int buttonPin = 3; // the pin that the pushbutton is attached to
const int ledPin = 13; // the pin that the LED is attached to
const int pressGood = 10;
const int pressBad = 9;
int fsrPin = A0;
int hidungpin = 4;
int mpxpin = A1;
// Variables will change:
int buttonPushCounter = 0; // counter for the number of button presses
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button
int cprcount;
int fsrReading;
int gyroReading;
int fsrVoltage;
int idle, goals;
int minVal = 265;
int maxVal = 402;
int hidungstate;
int sensorValue = 0;
float sensorVoltage, kPa, PSI;
unsigned long timeBegin, timeEnd, time, totalTime;
unsigned long fsrResistance;
unsigned long fsrConductance;
long fsrForce;
long timer = 0;
void setup() {
pinMode(buttonPin, INPUT);
Serial.begin(9600);
Wire.begin();
mpu6050.begin();
mpu6050.calcGyroOffsets(true);
lcd.begin(16, 2);
lcd.print("Mulai Kompresi");
delay(5000);
lcd.clear();
lcd.print("30x dlm 20 detik");
delay(1000);
}
void gyro(){
mpu6050.update();
if(millis() - timer > 1000){
Serial.println("=======================================================");
Serial.print("angleX : ");Serial.print(mpu6050.getAngleX());
Serial.print(" angleY : ");Serial.print(mpu6050.getAngleY());
lcd.clear();
lcd.print("kemiringan: ");
lcd.setCursor(0,1);
lcd.print(mpu6050.getAngleY());
Serial.println("=======================================================");
timer = millis();
idle = (mpu6050.getAngleY() == 80);
goals = (idle + 30);
}
}
void loop() {
time = millis();
buttonState = digitalRead(buttonPin);
fsrReading = analogRead(fsrPin);
if (buttonState != lastButtonState) {
if (buttonState == HIGH) {
cprcount++;
Serial.print("total comp: ");
Serial.println(cprcount);
lcd.clear();
lcd.print(cprcount);
delay(100);
}
lastButtonState = buttonState;
if (cprcount == 1) {
timeBegin = millis();
}
if (cprcount == 30) {
cprcount = 0;
timeEnd = millis();
totalTime = (timeEnd - timeBegin) / 1000;
Serial.println("-----------------");
Serial.println("Total Time: ");
Serial.println(totalTime);
Serial.println("-----------------");
lcd.clear();
lcd.print("Total Time : ");
lcd.print(totalTime);
delay(1000);
if ( 20 <= totalTime && totalTime< 25 ) { // if the compression is correct
lcd.clear();
Serial.print("Segera miringkan kepala korban!!");
lcd.print("Segera miringkan kepala korban!!");
for (int positionCounter = 0; positionCounter < 17; positionCounter++) {
lcd.scrollDisplayLeft();
delay(300);
}
gyro(); // gyroscope start
if (mpu6050.getAngleY() > goals){
lcd.clear();
lcd.print("head tilted!"); //head tilted
}
} else { // if compression incorrect
lcd.clear();
lcd.print("Kompresi belum dilakukan dengan benar!");
for (int positionCounter = 0; positionCounter < 23; positionCounter++) {
lcd.scrollDisplayLeft();
delay(300);
}
Serial.print("Kompresi belum dilakukan dengan benar!");
}
}
}
}
thanks for the help