You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
80 lines
2.0 KiB
C++
80 lines
2.0 KiB
C++
// Basic demo for accelerometer readings from Adafruit MPU6050
|
|
|
|
#include <Adafruit_MPU6050.h>
|
|
#include <Adafruit_Sensor.h>
|
|
#include <Wire.h>
|
|
#include <RTClib.h>
|
|
RTC_DS1307 rtc;
|
|
char t[32];
|
|
|
|
Adafruit_MPU6050 mpu;
|
|
|
|
void setup(void) {
|
|
pinMode(7, OUTPUT);
|
|
Serial.begin(115200);
|
|
Wire.begin();
|
|
rtc.begin();
|
|
DateTime now = rtc.now();
|
|
Serial.println("--------------------------------------------");
|
|
Serial.println(" Starting development board! ");
|
|
sprintf(t, "%02d:%02d:%02d %02d/%02d/%02d", now.hour(), now.minute(), now.second(), now.day(), now.month(), now.year());
|
|
Serial.print(F(" Date/Time: "));
|
|
Serial.println(t);
|
|
Serial.println("--------------------------------------------");
|
|
delay(1000);
|
|
while (!Serial) {
|
|
delay(10); // will pause Zero, Leonardo, etc until serial console opens
|
|
}
|
|
|
|
// Try to initialize!
|
|
if (!mpu.begin(0x69)) {
|
|
Serial.println("Failed to find MPU6050 chip");
|
|
while (1) {
|
|
delay(10);
|
|
}
|
|
}
|
|
|
|
mpu.setAccelerometerRange(MPU6050_RANGE_16_G);
|
|
mpu.setGyroRange(MPU6050_RANGE_250_DEG);
|
|
mpu.setFilterBandwidth(MPU6050_BAND_21_HZ);
|
|
Serial.println("");
|
|
delay(100);
|
|
}
|
|
|
|
void loop() {
|
|
|
|
/* Get new sensor events with the readings */
|
|
sensors_event_t a, g, temp;
|
|
mpu.getEvent(&a, &g, &temp);
|
|
|
|
/* Print out the values */
|
|
Serial.print("AccelX:");
|
|
Serial.print(a.acceleration.x);
|
|
Serial.print(",");
|
|
Serial.print("AccelY:");
|
|
Serial.print(a.acceleration.y);
|
|
Serial.print(",");
|
|
Serial.print("AccelZ:");
|
|
Serial.print(a.acceleration.z);
|
|
Serial.print(", ");
|
|
Serial.print("GyroX:");
|
|
Serial.print(g.gyro.x);
|
|
Serial.print(",");
|
|
Serial.print("GyroY:");
|
|
Serial.print(g.gyro.y);
|
|
Serial.print(",");
|
|
Serial.print("GyroZ:");
|
|
Serial.print(g.gyro.z);
|
|
Serial.print(",");
|
|
Serial.print("Temp:");
|
|
Serial.print(temp.temperature);
|
|
Serial.println("");
|
|
if(temp.temperature > 28){
|
|
digitalWrite(7, HIGH);
|
|
} else {
|
|
digitalWrite(7, LOW);
|
|
}
|
|
|
|
delay(100);
|
|
}
|