Mpu6050

 #include <Adafruit_GFX.h>

#include <Adafruit_ST7735.h>

#include <SPI.h>

#include <Wire.h>

#include <MPU6050.h>


#define TFT_CS   10

#define TFT_RST  8

#define TFT_DC   9


Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_RST);

MPU6050 mpu;


// Colors

#define SKY_BLUE   tft.color565(0, 102, 204)

#define GROUND_BR  tft.color565(153, 76, 0)

#define YELLOW     ST77XX_YELLOW

#define WHITE      ST77XX_WHITE

#define BLACK      ST77XX_BLACK


// Filter variables

float pitchFiltered = 0;

float alpha = 0.92;   // smoothing (0.90–0.97 best)


// Timing

unsigned long lastTime = 0;

float dt;


// Display center

const int cx = 80;

const int cy = 64;


void setup() {

  Wire.begin();

  mpu.initialize();


  tft.initR(INITR_BLACKTAB);

  tft.setRotation(1);


  tft.fillScreen(BLACK);

  drawFrame();


  lastTime = millis();

}


void loop() {

  updateIMU();

  drawHorizon();

}


// ================= MPU + SMOOTH =================

void updateIMU() {

  int16_t ax, ay, az, gx, gy, gz;

  mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);


  unsigned long now = millis();

  dt = (now - lastTime) / 1000.0;

  lastTime = now;


  // Pitch from accelerometer

  float pitch = atan2(ay, az) * 180 / PI;


  // Smooth filter (VERY IMPORTANT for stability)

  pitchFiltered = alpha * pitchFiltered + (1 - alpha) * pitch;


  // small dead zone (removes jitter)

  if (abs(pitchFiltered) < 0.4) pitchFiltered = 0;

}


// ================= STATIC FRAME =================

void drawFrame() {

  tft.drawCircle(cx, cy, 60, WHITE);

}


// ================= HORIZON DRAW =================

void drawHorizon() {


  // sensitivity (increase = more movement)

  float scale = 1.4;


  int offset = pitchFiltered * scale;

  offset = constrain(offset, -50, 50);


  int horizonY = cy + offset;


  // --- Sky & Ground ---

  tft.fillRect(0, 0, 160, horizonY, SKY_BLUE);

  tft.fillRect(0, horizonY, 160, 128, GROUND_BR);


  // --- Horizon line ---

  tft.drawFastHLine(0, horizonY, 160, WHITE);


  // --- Pitch lines ---

  for (int i = -3; i <= 3; i++) {

    int y = horizonY - (i * 15);


    if (i != 0) {

      tft.drawFastHLine(cx - 20, y, 40, WHITE);

    }


    int value = i * 10;


    String valStr = (value > 0) ? "+" + String(value) : String(value);


    tft.setTextSize(1);

    tft.setTextColor(WHITE);


    tft.setCursor(2, y - 3);

    tft.print(valStr);


    tft.setCursor(142, y - 3);

    tft.print(valStr);

  }


  // --- Aircraft symbol (fixed center) ---

  tft.fillRect(cx - 30, cy, 60, 2, YELLOW);

  tft.drawLine(cx - 10, cy, cx, cy + 8, YELLOW);

  tft.drawLine(cx, cy + 8, cx + 10, cy, YELLOW);

  tft.fillCircle(cx, cy, 2, YELLOW);


  // redraw frame circle (so it stays visible)

  tft.drawCircle(cx, cy, 60, WHITE);

}

Post a Comment

Previous Post Next Post

Contact Form