#include"TFT_eSPI.h"
//Define LCD pins
#define TFT_CS_PIN 4 // Chip select control pin
#define TFT_SDA_PIN 3 // Serial Data pin
#define TFT_SCL_PIN 5 // Serial Clock pin
#define TFT_DC_PIN 2 // Data Command control pin
#define TFT_RES_PIN 1 // LCD Reset pin
#define TFT_LEDA_PIN 38 // LCD Backlight - set 0 for on and 1 for off
unsigned long nowMillis;
unsigned long startMillis;
uint8_t stardelay = 35; //higher the number the slower the stars
//Define LCD Screen Size
#define DISPLAY_WIDTH 160 //set tft screen width
#define DISPLAY_HEIGHT 80 //set tft screen height
#define STARS 80 //amount of stars to show on screen
float star_x[STARS], star_y[STARS], star_z[STARS];
void initStar(int i) {
star_x[i] = random(-100, 100);
star_y[i] = random(-100, 100);
star_z[i] = random(100, 500);
}
TFT_eSPI tft = TFT_eSPI();
void showStarfield() {
int x,y;
int centrex,centrey;
centrex = DISPLAY_WIDTH / 2;
centrey = DISPLAY_HEIGHT / 2;
for (int i = 0; i < STARS; i++) {
star_z[i] = star_z[i] - 7;
x = star_x[i] / star_z[i] * 100 + centrex;
y = star_y[i] / star_z[i] * 100 + centrey;
if(
(x < 0) || (x > DISPLAY_WIDTH) ||
(y < 0) || (y > DISPLAY_HEIGHT) ||
(star_z[i] < 1)
)
initStar(i);
tft.drawPixel(x, y, TFT_WHITE);
}
}
void setup(void) {
tft.init();
tft.fillScreen(TFT_BLACK);
tft.setRotation(1);
startMillis = millis();
for (int i = 0; i < STARS; i++)
initStar(i);
}
void loop() {
nowMillis = millis(); //get the current "time" (actually the number of milliseconds since the program started)
if (nowMillis - startMillis >= stardelay)
{
tft.fillScreen(TFT_BLACK);
showStarfield();
startMillis = nowMillis;
}
}