#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h>
#include <Wire.h>
#include <CuteBuzzerSounds.h>
#include <NTPClient.h>
int BUZZER_PIN_sx = 5;
int BUZZER_PIN_dx = 18;
Adafruit_MPU6050 mpu;
void checksensor(){
if (!mpu.begin()) {
Serial.println("Sensor init failed");
while (1)
yield();
}
Serial.println("Found a MPU-6050 sensor");
//set del range dell'accelerometro come quello di wokwi
mpu.setAccelerometerRange(MPU6050_RANGE_2_G);
switch (mpu.getAccelerometerRange()) {
case MPU6050_RANGE_2_G:
Serial.print("+-2G");
break;
}
//set del range del giroscopio come quello di wokwi
mpu.setGyroRange(MPU6050_RANGE_250_DEG);
switch (mpu.getGyroRange()) {
case MPU6050_RANGE_250_DEG:
Serial.print("+- 250 deg/s");
break;
}
}
void setup() {
//setup wifi
Serial.begin(115200);
pinMode(BUZZER_PIN_sx, OUTPUT);
pinMode(BUZZER_PIN_dx, OUTPUT);
digitalWrite(BUZZER_PIN_sx, LOW);
digitalWrite(BUZZER_PIN_dx, LOW);
checksensor();
}
void loop() {
sensors_event_t a, g, t;
if (!mpu.getEvent(&a, &g, &t)){
Serial.print("Errore in mpu.getEvent");
}
//Serial.print("Accelerometer ");
//Serial.print("X: ");
//Serial.print(a.acceleration.x, 1);
//Serial.print(" m/s^2, ");
//Serial.print("Y: ");
//Serial.print(a.acceleration.y, 1);
//Serial.print(" m/s^2, ");
//Serial.print("Z: ");
//Serial.print(a.acceleration.z, 1);
//Serial.println(" m/s^2");
//Serial.print("Gyroscope ");
Serial.print("\n X: ");
Serial.print(g.gyro.x, 1);
//Serial.print(" rps, ");
Serial.print(" Y: ");
Serial.print(g.gyro.y, 1);
//Serial.print(" rps, ");
Serial.print(" Z: ");
Serial.print(g.gyro.z, 1);
//Serial.println(" rps");
//avviso sonoro se movimento eccessivo avanti/indietro o sx/dx
//abbiamo moltiplicato per 4.4/250
if(g.gyro.y>0.352)
{ //avanti indietro
// aspetto 5 secondi
Serial.print(" Errore avanti/indietro: ");
Serial.print(g.gyro.y);
digitalWrite(BUZZER_PIN_sx, HIGH); // turn on
digitalWrite(BUZZER_PIN_dx, HIGH); // turn on
}
/*else if(g.gyro.z>0.352){ //sx
// aspetto 5 secondi
if(g.gyro.z>0.352){
digitalWrite(BUZZER_PIN_sx, HIGH); // turn on
}
}
else if(g.gyro.z<-0.352){ //dx
// aspetto 5 secondi
if(g.gyro.z<-0.352){
digitalWrite(BUZZER_PIN_dx, HIGH); // turn on
}
} */
else{
digitalWrite(BUZZER_PIN_sx, LOW); // turn off
digitalWrite(BUZZER_PIN_dx, LOW); // turn off
}
//delay(2000);
}