A "traffic light" exercise in push button, TFT shapes and colors.
Not for use in working, traffic management, systems.
In the manner of this topic:
// https://forum.arduino.cc/t/semaforo-inteligente/1462631/
// https://wokwi.com/projects/477335770048260097
#include <Arduino_Nesso_N1.h>
NessoBattery battery;
NessoDisplay display;
#define wht 0xFFFF
#define blk 0x0000
#define red 0xF800
#define amb 0xFEA0
#define yel 0xFFE0
#define grn 0x07E0
#define blu 0x001F
#define redoff 0x7800
#define yeloff 0x9A60
#define grnoff 0x03E0
bool toggle = 0;
unsigned long blinktimer, blinktimeout = 500;
void setup() {
battery.begin(); // Initialize battery library
battery.enableCharge(); // Enable charging
display.begin();
display.setRotation(3); // KEY1 left
display.setTextSize(1);
display.setTextColor(wht, blk);
pinMode(KEY1, INPUT_PULLUP); // PRESSED = 0, RELEASED = 1
Serial.begin(115200);
delay(2000);
drawTrafficLight();
delay(3000);
}
void loop() {
// green light OH
circlefill(50, 30, 13, redoff);
circlefill(50, 65, 13, yeloff);
circlefill(50, 100, 13, grn);
while (digitalRead(KEY1)==1) // wait for button
walkdontwalk(1); // flashing "DONT WALK"
walkdontwalk(0); // steady "WALK"
circlefill(50, 30, 13, redoff);
circlefill(50, 65, 13, yel); // yellow light ON
circlefill(50, 100, 13, grnoff);
delay(3000);
circlefill(50, 30, 13, red); // red light ON
circlefill(50, 65, 13, yeloff);
circlefill(50, 100, 13, grnoff);
delay(5000);
}
void walkdontwalk(bool state) {
if (state == 1) { // DONT WALK
if (millis() - blinktimer >= blinktimeout) {
blinktimer = millis();
toggle = !toggle;
if (toggle) {
text(100, 25, 5, redoff, "DONT"); // x, y, pitch, text
text(100, 80, 5, redoff, "WALK");
} else {
text(100, 25, 5, red, "DONT");
text(100, 80, 5, red, "WALK");
}
}
} else {
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 pitch, int color, char text[]) {
display.setTextSize(pitch);
display.setTextColor(color);
display.setCursor(x, y);
display.print(text);
display.setTextSize(1);
}
void roundrectdraw(int x, int y, int width, int height, int innerradius, int color, int outterradius) {
display.drawRoundRect(x, y, width, height, outterradius, color);
}
void roundrectfill(int x, int y, int width, int height, int radius, int color) {
display.fillRoundRect(x, y, width, height, radius, color);
}
void circlefill(int x, int y, int radius, int color) {
display.fillCircle(x, y, radius, color);
}
