Advertisement
Hyperion777

Arduino BNO055 +Adafruit_SSD1306

May 22nd, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.08 KB | None | 0 0
  1. #include <Wire.h>
  2. #include <Adafruit_Sensor.h>
  3. #include <Adafruit_BNO055.h>
  4. #include <utility/imumaths.h>
  5. #include <SPI.h>
  6. #include <Adafruit_GFX.h>
  7. #include <Adafruit_SSD1306.h>
  8. #include <Fonts/FreeMono9pt7b.h>
  9.  
  10. /*
  11. TIP: in Adafruit_BNO055.h ho dovuto invertire i valori di:
  12. BNO055_ADDRESS_B e BNO055_ADDRESS_A (0x29)
  13. Vedi: https://forum.arduino.cc/index.php?topic=338503.0
  14. */
  15.  
  16. // Classi per gestire il display e il sensore BNO055
  17. Adafruit_SSD1306 display = Adafruit_SSD1306(128, 64, & Wire);#
  18. define BNO055_SAMPLERATE_DELAY_MS(100)
  19. Adafruit_BNO055 bno = Adafruit_BNO055();
  20.  
  21. // Variabili globali
  22. #
  23. define ROW_HEIGHT 16# define COL_WIDTH 64
  24. int x = 0;
  25.  
  26. void setup() {
  27.  
  28.   Serial.begin(9600);
  29.   Serial.print("Hello world");
  30.  
  31.   // Inizializzo il display
  32.   display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // Address 0x3C for 128x32
  33.   display.display();
  34.   Serial.print("Display inizializzato");
  35.  
  36.   // Inizializzo il sensore BNO055
  37.   if (!bno.begin()) {
  38.     /* There was a problem detecting the BNO055 ... check your connections */
  39.     Serial.print("Non ho rilevato il sensore BNO055. Controlla il collegamento o l'indirizzo I2C.");
  40.     while (1);
  41.   } else {
  42.     Serial.print("Sensore BNO055 inizializzato");
  43.   }
  44.  
  45.   // Impostazioni del testo
  46.   //display.setFont(&FreeMono9pt7b);
  47.   display.setTextColor(WHITE);
  48.  
  49.   // Test: rilevo la temperatura
  50.   int8_t temp = bno.getTemp();
  51.   char temperatureString[32];
  52.   sprintf(temperatureString, "Temperatura: %d", temp);
  53.   Serial.println(temperatureString);
  54.  
  55.   // Mostro la temperatura.
  56.   display.clearDisplay();
  57.   display.display();
  58.   display.setTextSize(2);
  59.   display.println("\nScarso!");
  60.   //display.println(temperatureString);
  61.   display.setCursor(0, 0);
  62.   display.display();
  63.  
  64.   delay(2000);
  65. }
  66.  
  67. void loop() {
  68.   // put your main code here, to run repeatedly:
  69.  
  70.   //delay(1000);
  71.   char string[16];
  72.  
  73.   //display init
  74.   display.clearDisplay();
  75.  
  76.   x++;
  77.  
  78.   if (x < 100) {
  79.  
  80.     display.setTextSize(1);
  81.     // Possible vector values can be:
  82.     // - VECTOR_ACCELEROMETER - m/s^2
  83.     // - VECTOR_MAGNETOMETER  - uT
  84.     // - VECTOR_GYROSCOPE     - rad/s
  85.     // - VECTOR_EULER         - degrees
  86.     // - VECTOR_LINEARACCEL   - m/s^2
  87.     // - VECTOR_GRAVITY       - m/s^2
  88.  
  89.     imu::Vector < 3 > euler = bno.getVector(Adafruit_BNO055::VECTOR_EULER);
  90.     display.setCursor(0 * COL_WIDTH, 0 * ROW_HEIGHT);
  91.     sprintf(string, "X: %.2f", euler.x());
  92.     display.print(string);
  93.     display.setCursor(0 * COL_WIDTH, 1 * ROW_HEIGHT);
  94.     sprintf(string, "Y: %.2f", euler.y());
  95.     display.print(string);
  96.     display.setCursor(0 * COL_WIDTH, 2 * ROW_HEIGHT);
  97.     sprintf(string, "Z: %.2f", euler.z());
  98.     display.print(string);
  99.  
  100.     imu::Vector < 3 > gravity = bno.getVector(Adafruit_BNO055::VECTOR_GRAVITY);
  101.     display.setCursor(1 * COL_WIDTH, 0 * ROW_HEIGHT);
  102.     sprintf(string, "A: %.2f", gravity.x());
  103.     display.print(string);
  104.     display.setCursor(1 * COL_WIDTH, 1 * ROW_HEIGHT);
  105.     sprintf(string, "B: %.2f", gravity.y());
  106.     display.print(string);
  107.     display.setCursor(1 * COL_WIDTH, 2 * ROW_HEIGHT);
  108.     sprintf(string, "C: %.2f", gravity.z());
  109.     display.print(string);
  110.  
  111.     // counter
  112.     sprintf(string, "ticks: %d", x);
  113.     display.setCursor(0 * COL_WIDTH, 3 * ROW_HEIGHT);
  114.     display.print(string);
  115.     display.display(); // actually display all of the above
  116.  
  117.   } else if (x < 120) {
  118.  
  119.     display.setTextSize(2);
  120.     display.setCursor(0 * COL_WIDTH, 1 * ROW_HEIGHT);
  121.     display.print("Guarda\nquesto");
  122.     display.display(); // actually display all of the above
  123.  
  124.   } else {
  125.  
  126.     // drawCircle(int16_t x0, int16_t y0, int16_t r, uint16_t color),
  127.     display.setCursor(0, 0);
  128.     float G = 9.81;
  129.     float RAD = 31;
  130.     float X0 = 64, Y0 = 32;
  131.     display.drawCircle(X0, Y0, RAD, WHITE);
  132.  
  133.     imu::Vector < 3 > gravity = bno.getVector(Adafruit_BNO055::VECTOR_GRAVITY);
  134.     float x = gravity.y() * (RAD / G);
  135.     float y = gravity.z() * (RAD / G);
  136.     display.fillCircle(X0 + x, Y0 + y, 8, WHITE);
  137.  
  138.     display.display(); // actually display all of the above
  139.   }
  140.  
  141.   delay(BNO055_SAMPLERATE_DELAY_MS);
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement