From 991381a67d00f2fa8dcf0475172b0151b4310385 Mon Sep 17 00:00:00 2001 From: HampusM Date: Sun, 19 Dec 2021 20:14:11 +0100 Subject: refactor: use the GY521 sensor instead --- src/gyro.cpp | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 src/gyro.cpp (limited to 'src') diff --git a/src/gyro.cpp b/src/gyro.cpp new file mode 100644 index 0000000..bdd68e9 --- /dev/null +++ b/src/gyro.cpp @@ -0,0 +1,43 @@ +#include "Arduino.h" +#include "GY521.h" + +GY521 sensor(0x68); + +void setup() { + Serial.begin(9600); + + // Wait for Serial because the Arduino Leonardo is weird + while (!Serial) {} + + Wire.begin(); + + delay(100); + while (!sensor.wakeup()) { + Serial.print(millis()); + Serial.println("Error: Could not connect to the GY521 sensor. Retrying after 2000 milliseconds..."); + delay(2000); + } + sensor.setAccelSensitivity(2); // 8g + sensor.setGyroSensitivity(1); // 500 degrees/s + + sensor.setThrottle(); + Serial.println("start..."); + + // Calibration values + sensor.axe = 0.198; + sensor.aye = -0.018; + sensor.gxe = 0.780; + sensor.gye = -1.495; +} + +void loop() { + sensor.read(); + float pitch = sensor.getPitch(); + float roll = sensor.getRoll(); + + Serial.print("Pitch: "); + Serial.print(pitch, 3); + Serial.print(" Roll: "); + Serial.print(roll, 3); + Serial.println(); +} \ No newline at end of file -- cgit v1.2.3-18-g5258