Skip to content

Instantly share code, notes, and snippets.

@TheZoq2
Created March 31, 2019 15:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TheZoq2/d2ae78f44f63a55459a5e0a5c0ec7e57 to your computer and use it in GitHub Desktop.
Save TheZoq2/d2ae78f44f63a55459a5e0a5c0ec7e57 to your computer and use it in GitHub Desktop.
#include <Servo.h>
#include <SPI.h>
#include <Wire.h>
#include <SparkFunLSM9DS1.h>
Servo servo;
LSM9DS1 imu;
const int TRIG_PIN = 16;
const int ECHO_PIN = 2;
const int IMU_M = 0x1e;
const int IMU_AG = 0x6b;
void setup() {
// put your setup code here, to run once:
servo.attach(12);
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
digitalWrite(TRIG_PIN, LOW);
imu.settings.device.commInterface = IMU_MODE_I2C; // Set mode to I2C
imu.settings.device.mAddress = IMU_M; // Set mag address to 0x1E
imu.settings.device.agAddress = IMU_AG; // Set ag address to 0x6B
Serial.begin(9600);
while(!imu.begin()) {
Serial.println("Failed to start IMU");
delay(1000);
}
}
void loop() {
// servo.write(45);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
auto duration = pulseIn(ECHO_PIN, HIGH);
auto distance = duration / 29.1 / 2;
// Serial.print(distance);
// Serial.println(" cm");
// servo.write(distance);
imu.readAccel();
Serial.println(imu.az);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment