Advertisement
Guest User

Untitled

a guest
Dec 5th, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <LiquidCrystal.h>  
  2. #include <Wire.h>  // Wire library - used for I2C communication
  3. int Contrast=133;
  4. int bright = 50;
  5.  
  6. /////////////////////////
  7. const int MPU_ADDR = 0x68; // I2C address of the MPU-6050. If AD0 pin is set to HIGH, the I2C address will be 0x69.
  8.  
  9. int16_t accelerometer_x, accelerometer_y, accelerometer_z; // variables for accelerometer raw data
  10. int16_t gyro_x, gyro_y, gyro_z; // variables for gyro raw data
  11. int16_t temperature; // variables for temperature data
  12.  
  13. char tmp_str[7]; // temporary variable used in convert function
  14.  
  15. char* convert_int16_to_str(int16_t i) { // converts int16 to string. Moreover, resulting strings will have the same length in the debug monitor.
  16.   sprintf(tmp_str, "%6d", i);
  17.   return tmp_str;
  18. }
  19.  
  20. LiquidCrystal lcd(12, 11, 5, 4, 3, 2);  
  21.  
  22.  void setup()
  23.  {
  24.     analogWrite(6,Contrast);
  25.      lcd.begin(16, 2);
  26.      analogWrite(10, bright);
  27. /////////////////////////
  28.   Serial.begin(4800);
  29.   Wire.begin();
  30.   Wire.beginTransmission(MPU_ADDR); // Begins a transmission to the I2C slave (GY-521 board)
  31.   Wire.write(0x6B); // PWR_MGMT_1 register
  32.   Wire.write(0); // set to zero (wakes up the MPU-6050)
  33.   Wire.endTransmission(true);
  34.  
  35.  lcd.begin(2,16);              
  36.  lcd.clear();                
  37.  lcd.setCursor(0,0);  
  38.  lcd.print("TorqueTracker");
  39.  lcd.setCursor(5,1);  
  40.  lcd.print("v0.1");
  41.  delay(5000);
  42.   }  
  43.  
  44. void loop()
  45.  {  
  46.  
  47.  lcd.begin(2,16);              
  48.  lcd.clear();                
  49.  lcd.setCursor(0,0);  
  50.  lcd.print("x");
  51.  lcd.print(accelerometer_x);  
  52.  lcd.setCursor(9,0);          
  53.  lcd.print("y");
  54.  lcd.print(accelerometer_y);
  55.  lcd.setCursor(5,1);          
  56.  lcd.print("z");
  57.  lcd.print(accelerometer_z);
  58.  delay(100);
  59.  
  60. /////////////////////////
  61.   Wire.beginTransmission(MPU_ADDR);
  62.   Wire.write(0x3B); // starting with register 0x3B (ACCEL_XOUT_H) [MPU-6000 and MPU-6050 Register Map and Descriptions Revision 4.2, p.40]
  63.   Wire.endTransmission(false); // the parameter indicates that the Arduino will send a restart. As a result, the connection is kept active.
  64.   Wire.requestFrom(MPU_ADDR, 7*2, true); // request a total of 7*2=14 registers
  65.  
  66.   // "Wire.read()<<8 | Wire.read();" means two registers are read and stored in the same variable
  67.   accelerometer_x = Wire.read()<<8 | Wire.read(); // reading registers: 0x3B (ACCEL_XOUT_H) and 0x3C (ACCEL_XOUT_L)
  68.   accelerometer_y = Wire.read()<<8 | Wire.read(); // reading registers: 0x3D (ACCEL_YOUT_H) and 0x3E (ACCEL_YOUT_L)
  69.   accelerometer_z = Wire.read()<<8 | Wire.read(); // reading registers: 0x3F (ACCEL_ZOUT_H) and 0x40 (ACCEL_ZOUT_L)
  70.   temperature = Wire.read()<<8 | Wire.read(); // reading registers: 0x41 (TEMP_OUT_H) and 0x42 (TEMP_OUT_L)
  71.   gyro_x = Wire.read()<<8 | Wire.read(); // reading registers: 0x43 (GYRO_XOUT_H) and 0x44 (GYRO_XOUT_L)
  72.   gyro_y = Wire.read()<<8 | Wire.read(); // reading registers: 0x45 (GYRO_YOUT_H) and 0x46 (GYRO_YOUT_L)
  73.   gyro_z = Wire.read()<<8 | Wire.read(); // reading registers: 0x47 (GYRO_ZOUT_H) and 0x48 (GYRO_ZOUT_L)
  74.  
  75.   // print out data
  76.   Serial.print("aX = "); Serial.print(convert_int16_to_str(accelerometer_x));
  77.   Serial.print(" | aY = "); Serial.print(convert_int16_to_str(accelerometer_y));
  78.   Serial.print(" | aZ = "); Serial.print(convert_int16_to_str(accelerometer_z));
  79.   // the following equation was taken from the documentation [MPU-6000/MPU-6050 Register Map and Description, p.30]
  80.   Serial.print(" | tmp = "); Serial.print(temperature/340.00+36.53);
  81.   Serial.print(" | gX = "); Serial.print(convert_int16_to_str(gyro_x));
  82.   Serial.print(" | gY = "); Serial.print(convert_int16_to_str(gyro_y));
  83.   Serial.print(" | gZ = "); Serial.print(convert_int16_to_str(gyro_z));
  84.   Serial.println();
  85.  
  86.   // delay
  87.   delay(300);
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement