How to make ST7735 TFT display based Digital Clock & Temperature Pressure Measurement using BMP280







           CODE 

 #include <Wire.h>

#include <SPI.h>

#include <Adafruit_GFX.h>

#include <Adafruit_ST7735.h>

#include <Adafruit_BMP280.h>


// ===== TFT =====

#define TFT_CS   10

#define TFT_DC   8

#define TFT_RST  9


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


// ===== BMP280 =====

Adafruit_BMP280 bmp;


// ===== Colors =====

#define CYBER_BG      ST77XX_BLACK

#define CYBER_GREEN   0x07E0

#define CYBER_LIGHT   0xFD20

#define CYBER_BLUE    0x07FF

#define CYBER_PINK    0xF81F

#define CYBER_ACCENT  0x05FF


// ===== Radar =====

int sweepAngle = 0;


// ===== Time =====

unsigned long previousMillis = 0;

int seconds = 0;

int minutes = 0;

int hours = 12;



// ===== Radar Base =====

void drawRadarBase() {


  int cx = 130;

  int cy = 40;


  for (int r = 35; r > 0; r -= 4) {

    tft.drawCircle(cx, cy, r, CYBER_LIGHT);

  }

}



// ===== Radar Sweep =====

void drawRadarSweep() {


  int cx = 130;

  int cy = 40;


  float rad = sweepAngle * 3.14159 / 180.0;


  int x2 = cx + cos(rad) * 35;

  int y2 = cy + sin(rad) * 35;


  tft.drawLine(cx, cy, x2, y2, CYBER_BLUE);


  delay(80);


  tft.drawLine(cx, cy, x2, y2, CYBER_BG);


  sweepAngle += 20;


  if (sweepAngle >= 360) sweepAngle = 0;

}



// ===== Clock =====

void drawClock() {


  tft.fillRect(0, 10, 100, 25, CYBER_BG);


  tft.setTextSize(2);

  tft.setTextColor(CYBER_LIGHT);

  tft.setCursor(5, 10);


  if (hours < 10) tft.print("0");

  tft.print(hours);

  tft.print(":");


  if (minutes < 10) tft.print("0");

  tft.print(minutes);

}



// ===== Seconds bar =====

void drawSeconds() {


  int barWidth = map(seconds, 0, 59, 0, 60);


  tft.fillRect(5, 35, 70, 8, CYBER_BG);


  tft.drawRect(5, 35, 60, 6, CYBER_ACCENT);


  tft.fillRect(5, 35, barWidth, 6, CYBER_GREEN);

}



// ===== Environment =====

void drawEnv() {


  float temp = bmp.readTemperature();

  float pressure = bmp.readPressure() / 100.0;


  // Clear area first

  tft.fillRect(0, 48, 100, 30, CYBER_BG);


  tft.setTextSize(1);


  tft.setTextColor(CYBER_GREEN);

  tft.setCursor(5, 50);

  tft.print("TEMP:");

  tft.setCursor(45, 50);

  tft.print(temp,1);

  tft.print("C");


  tft.setTextColor(CYBER_PINK);

  tft.setCursor(5, 62);

  tft.print("PRES:");

  tft.setCursor(45, 62);

  tft.print(pressure,0);

  tft.print("hPa");

}



// ===== Neon Border =====

void neonBorder() {


  static bool pulse = false;


  uint16_t c = pulse ? CYBER_LIGHT : CYBER_ACCENT;


  tft.drawRect(0, 0, 160, 80, c);


  pulse = !pulse;

}



// ===== Setup =====

void setup() {


  Serial.begin(9600);


  Wire.begin();  // UNO I2C


  bmp.begin(0x76);


  tft.initR(INITR_MINI160x80);

  tft.setRotation(1);

  tft.fillScreen(CYBER_BG);


  drawRadarBase();

  drawClock();

  drawSeconds();

  drawEnv();

}



// ===== Loop =====

void loop() {


  drawRadarSweep();


  neonBorder();


  unsigned long currentMillis = millis();


  if (currentMillis - previousMillis >= 1000) {


    previousMillis = currentMillis;


    seconds++;


    if (seconds >= 60) {


      seconds = 0;

      minutes++;


      if (minutes >= 60) {


        minutes = 0;

        hours++;


        if (hours >= 24) hours = 0;

      }


      drawClock();

    }


    drawSeconds();


    if (seconds % 5 == 0) {

      drawEnv();

    }

  }

}

Post a Comment

Previous Post Next Post

Contact Form