#include <Adafruit_GFX.h> // https://github.com/adafruit/Adafruit-GFX-Library
#include <Adafruit_ILI9341.h> // https://github.com/adafruit/Adafruit_ILI9341
#define TFT_MISO 15
#define TFT_CLK 18
#define TFT_MOSI 23
#define TFT_DC 2 // incorrect DC to screen pin gives white screen
#define TFT_RST 4
#define TFT_CS 5
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST);
// custom colors - https://doc-tft-espi.readthedocs.io/tft_espi/colors/
#define wht 0xFFFF
#define blk 0x0000
#define red 0xF800
#define amb 0xFEA0
#define yel 0xFFE0
#define grn 0x07E0
#define cyn 0x07FF
#define blu 0x001F
#define redoff 0x7800
#define yeloff 0x9A60
#define grnoff 0x03E0
#define KEY1 14 // button
bool toggle = 0;
unsigned long blinktimer, blinktimeout = 500;
void setup() {
Serial.begin(115200);
delay(2000);
pinMode(KEY1, INPUT_PULLUP); // PRESSED = 0, RELEASED = 1
tft.begin();
tft.setRotation(1);
tft.setTextSize(1);
tft.setTextColor(wht, blk);
drawTrafficLight();
for (byte i = 0; i < 8; i++) // scroll Serial Monitor window to clear
Serial.println();
delay(3000);
}
void loop() {
// green light ON
circlefill(50, 30, 13, redoff);
circlefill(50, 65, 13, yeloff);
circlefill(50, 100, 13, grn);
// wait for button
while (digitalRead(KEY1) == 1) // normally RELEASED
walkdontwalk(1); // flashing "DONT WALK"
// and we're WALKING...
walkdontwalk(0); // steady "WALK"
// yellow light ON
circlefill(50, 30, 13, redoff);
circlefill(50, 65, 13, yel);
circlefill(50, 100, 13, grnoff);
delay(3000);
// red light ON
circlefill(50, 30, 13, red);
circlefill(50, 65, 13, yeloff);
circlefill(50, 100, 13, grnoff);
delay(5000);
}
void walkdontwalk(bool state) {
if (state == 1) { // DONT WALK blink
if (millis() - blinktimer >= blinktimeout) {
blinktimer = millis();
toggle = !toggle;
if (toggle) {
text(100, 25, 5, redoff, "DONT"); // x, y, point, text
text(100, 80, 5, redoff, "WALK");
} else {
text(100, 25, 5, red, "DONT");
text(100, 80, 5, red, "WALK");
}
}
} else { // WALK steady
text(100, 25, 5, redoff, "DONT");
text(100, 80, 5, red, "WALK");
}
}
void drawTrafficLight() {
for (byte i = 0; i < 4; i++) // border thickness
roundrectdraw(90 + i, 10 + i, 135 - i * 2, 115 - i * 2, 7, red, 5);
text(100, 25, 5, redoff, "DONT");
text(100, 80, 5, red, "WALK");
roundrectfill(20, 10, 60, 115, 7, yel); // x, y, w, h, color
circlefill(50, 30, 15, blk);
circlefill(50, 30, 13, red); // x, y, radius, color
circlefill(50, 65, 15, blk);
circlefill(50, 65, 13, amb); // x, y, radius, color (gold)
circlefill(50, 100, 15, blk);
circlefill(50, 100, 13, grn); // x, y, radius, color
}
void text(int x, int y, byte point, int color, char text[]) {
x += 41;
y += 53;
tft.setTextSize(point);
tft.setTextColor(color);
tft.setCursor(x, y);
tft.print(text);
tft.setTextSize(1);
}
void roundrectdraw(int x, int y, int width, int height, int innerradius, int color, int outterradius) {
x += 41;
y += 53;
tft.drawRoundRect(x, y, width, height, outterradius, color);
}
void roundrectfill(int x, int y, int width, int height, int radius, int color) {
x += 41;
y += 53;
tft.fillRoundRect(x, y, width, height, radius, color);
// https://doc-tft-espi.readthedocs.io/tft_espi/methods/fillrect/
}
void circle(int x, int y, int radius, int color) {
x += 41;
y += 53;
tft.drawCircle(x, y, radius, color);
}
void circlefill(int x, int y, int radius, int color) {
x += 41;
y += 53;
tft.fillCircle(x, y, radius, color);
}Loading
esp32-devkit-c-v4
esp32-devkit-c-v4