Advertisement
Agno

Untitled

Oct 31st, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.28 KB | None | 0 0
  1. #include <Wire.h>
  2. #include <SPI.h>
  3. #include <RF24.h>
  4. const int MPU = 0x68; // MPU6050 I2C address
  5. float accAngleX, accAngleY;
  6. float accAngleErrorX, accAngleErrorY;
  7. float accX, accY, accZ;
  8.  
  9. RF24 radio(9, 10);
  10.  
  11. int angles[2];
  12.  
  13. const uint64_t pipe = 0xE8E8F0F0E1LL;
  14.  
  15. void setup() {
  16. Serial.begin(57600);
  17. Wire.begin();
  18. Wire.beginTransmission(MPU);
  19. Wire.write(0x6B); // resetuje sensor
  20. Wire.write(0x00);
  21. Wire.endTransmission(true);
  22. radio.begin();
  23. radio.openWritingPipe(pipe);
  24.  
  25. delay(20);
  26. }
  27. void loop() {
  28. Wire.beginTransmission(MPU);
  29. Wire.write(0x3B); // (ACCEL_XOUT_H)
  30. Wire.endTransmission(false);
  31. Wire.requestFrom(MPU, 6, true); // pobieram 6 rejestrów akcelerometru
  32. accX = (Wire.read() << 8 | Wire.read()) / 16384.0; // na poczatku przeczytaj MSB potem LSB i podziel
  33. accY = (Wire.read() << 8 | Wire.read()) / 16384.0;
  34. accZ = (Wire.read() << 8 | Wire.read()) / 16384.0;
  35.  
  36. accAngleX = atan(accX / sqrt(pow(accY, 2) + pow(accZ, 2 ))) / (PI / 180);
  37. accAngleY = atan(accY / sqrt(pow(accX, 2) + pow(accZ, 2 ))) / (PI / 180);
  38.  
  39.  
  40.  
  41. Serial.print("X: ");
  42. Serial.print(accAngleX);
  43. Serial.print(" | Y: ");
  44. Serial.println(accAngleY);
  45. angles[0] = (int) accAngleX;
  46. angles[1] = (int) accAngleY;
  47. radio.write(angles, sizeof(angles));
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement