Potentiometer

 #include <Adafruit_GFX.h>

#include <Adafruit_ST7735.h>

#include <SPI.h>


#define TFT_CS   10

#define TFT_RST  8

#define TFT_DC   9


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


// 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


const int potPin = A0;


const int cx = 80;

const int cy = 64;


void setup() {

  tft.initR(INITR_BLACKTAB);

  tft.setRotation(1);


  tft.fillScreen(BLACK);

  drawFrame();

}


void loop() {


  int potValue = analogRead(potPin);


  // Convert 0–1023 → -45 to +45 degrees

  float angle = map(potValue, 0, 1023, -45, 45);


  drawHorizon(angle);


  delay(20); // smooth update

}


// ================= FRAME =================

void drawFrame() {

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

}


// ================= HORIZON =================

void drawHorizon(float angle) {


  int scale = 1.2;


  int offset = angle * 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 txt = (value > 0) ? "+" + String(value) : String(value);


    tft.setTextSize(1);

    tft.setTextColor(WHITE);


    tft.setCursor(2, y - 3);

    tft.print(txt);


    tft.setCursor(142, y - 3);

    tft.print(txt);

  }


  // Aircraft symbol fixed

  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);


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

}

Post a Comment

Previous Post Next Post

Contact Form