Skip to content

Instantly share code, notes, and snippets.

@chrisfraser
Last active June 10, 2016 08:08
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 chrisfraser/6bb624be9656a205fedb597fdb59fabf to your computer and use it in GitHub Desktop.
Save chrisfraser/6bb624be9656a205fedb597fdb59fabf to your computer and use it in GitHub Desktop.
#include<Wire.h>
#include <Ultrasonic.h>
const int MPU = 0x68; // I2C address of the MPU-6050
const int LDR = A0;
const int BUTTON = 4;
const int RED = 15;
const int GREEN = 12;
const int BLUE = 13;
int16_t AcX, AcY, AcZ, Tmp, GyX, GyY, GyZ;
Ultrasonic ultrasonic(2, 14); // (Trig PIN,Echo PIN)
void setup()
{
Serial.begin(9600);
delay(500);
Wire.begin(4, 5); // sda, scl
Wire.beginTransmission(MPU);
Wire.write(0x6B); // PWR_MGMT_1 register
Wire.write(0); // set to zero (wakes up the MPU-6050)
Wire.endTransmission(true);
pinMode(LDR, INPUT);
pinMode(BUTTON, INPUT);
pinMode(RED, OUTPUT);
pinMode(GREEN, OUTPUT);
pinMode(BLUE, OUTPUT);
}
void loop()
{
Wire.beginTransmission(MPU);
Wire.write(0x3B); // starting with register 0x3B (ACCEL_XOUT_H)
Wire.endTransmission(false);
Wire.requestFrom(MPU, 14, true); // request a total of 14 registers
AcX = Wire.read() << 8 | Wire.read(); // 0x3B (ACCEL_XOUT_H) & 0x3C (ACCEL_XOUT_L)
AcY = Wire.read() << 8 | Wire.read(); // 0x3D (ACCEL_YOUT_H) & 0x3E (ACCEL_YOUT_L)
AcZ = Wire.read() << 8 | Wire.read(); // 0x3F (ACCEL_ZOUT_H) & 0x40 (ACCEL_ZOUT_L)
Tmp = Wire.read() << 8 | Wire.read(); // 0x41 (TEMP_OUT_H) & 0x42 (TEMP_OUT_L)
Serial.print("AcX = "); Serial.print(AcX); //AcX
Serial.print(" | ");
Serial.print("AcY = "); Serial.print(AcY); //AcY
Serial.print(" | ");
Serial.print("AcZ = "); Serial.print(AcX); //AcZ
Serial.print(" | ");
Serial.print("Tmp = "); Serial.print(Tmp / 340.00 + 36.53); //equation for temperature in degrees C from datasheet
Serial.print(" | ");
Serial.print(ultrasonic.Ranging(CM)); // CM or INC
Serial.print(" cm" );
Serial.print(" | ");
Serial.print("LDR: ");
Serial.print(analogRead(LDR));
Serial.print(" | ");
Serial.print("BUTTON: ");
Serial.println(digitalRead(BUTTON));
analogWrite(RED, random(0, 1023));
analogWrite(GREEN, random(0, 1023));
analogWrite(BLUE, random(0, 1023));
delay(1000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment