/*
Simple "Hello World" for ILI9341 LCD
https://wokwi.com/arduino/projects/308024602434470466
*/
#include "SPI.h"
#include "Adafruit_GFX.h"
#include "Adafruit_ILI9341.h"
#define GREY 0x8410
#define DGREY 0x52AA
#define TFT_DC 9
#define TFT_CS 10
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
const float deg[24][2] = {
{0,1},
{0.258819045102521,0.965925826289068},
{0.5,0.866025403784439},
{0.707106781186547,0.707106781186548},
{0.866025403784439,0.5},
{0.965925826289068,0.258819045102521},
{1,0},
{0.965925826289068,-0.258819045102521},
{0.866025403784439,-0.5},
{0.707106781186548,-0.707106781186547},
{0.5,-0.866025403784439},
{0.258819045102521,-0.965925826289068},
{0,-1},
{-0.25881904510252,-0.965925826289068},
{-0.5,-0.866025403784439},
{-0.707106781186547,-0.707106781186548},
{-0.866025403784438,-0.5},
{-0.965925826289068,-0.258819045102521},
{-1,0},
{-0.965925826289068,0.258819045102521},
{-0.866025403784439,0.5},
{-0.707106781186548,0.707106781186547},
{-0.5,0.866025403784438},
{-0.258819045102521,0.965925826289068}
};
#include <TouchScreen.h>
#define YP A1 // must be an analog pin, use "An" notation!
#define XM A2 // must be an analog pin, use "An" notation!
#define YM 7 // can be a digital pin
#define XP 6 // can be a digital pin
#define TS_MINX 125
#define TS_MINY 85
#define TS_MAXX 965
#define TS_MAXY 905
TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);
#define LCD_CS A3
#define LCD_CD A2
#define LCD_WR A1
#define LCD_RD A0
// optional
#define LCD_RESET A4
// Assign human-readable names to some common 16-bit color values:
#define BLACK 0x0000
#define BLUE 0x001F
#define RED 0xF800
#define GREEN 0x07E0
#define CYAN 0x07FF
#define MAGENTA 0xF81F
#define YELLOW 0xFFE0
#define WHITE 0xFFFF
#define GREY 0x8410
#define ORANGE 0xfc00
#define MINPRESSURE 10
#define MAXPRESSURE 1000
int pos = 12;
int goal = 0;
int dir = 1;
int speed=275;
long oldMilis;
bool pause=false;
int score =0;
void setup(void) {
//tft.reset();
tft.begin(0x9341); // SDFP5408
tft.setRotation(3); // Need for the Mega, please changed for your choice or rotation initial.
tft.fillScreen(BLUE);
goal = random(0,24);
tft.setCursor(34, 35);
tft.setTextColor(YELLOW);
tft.setTextSize(4);
tft.print("L");
tft.fillCircle(72, 35, 10, YELLOW);
tft.fillCircle(72, 35, 7, BLUE);
tft.fillCircle(72, 50, 15, YELLOW);
tft.fillCircle(72, 47, 5, BLACK);
tft.fillRect(70, 50, 5, 9, BLACK);
tft.setCursor(92, 35);
tft.print("ck Buster");
tft.setCursor(45, 110);
tft.setTextColor(ORANGE);
tft.setTextSize(3);
tft.print("Bacon Studios");
tft.setCursor(37, 175);
tft.setTextColor(WHITE);
tft.setTextSize(3);
tft.print("Press to Begin");
pinMode(13, OUTPUT);
waitOneTouch();
tft.fillScreen(BLUE);
Draw(true);
oldMilis=millis();
}
void drawRoundLine(int x, int y,uint16_t r, uint16_t color) {
tft.fillCircle(200-(uint16_t)x, 110-(uint16_t)y, r, color);
tft.fillCircle(200-(uint16_t)floor(0.9*(x+160)-144), 110-(uint16_t)floor(0.9*(y+120)-108), r, color);
tft.fillCircle(200-(uint16_t)floor(0.85*(x+160)-136), 110-(uint16_t)floor(0.85*(y+120)-102), r, color);
tft.fillCircle(200-(uint16_t)floor(0.95*(x+160)-152), 110-(uint16_t)floor(0.95*(y+120)-114), r, color);
}
void Draw(bool full) {
// tft.fillRoundRect(160, 120, 18, 35, 9, LGREY);
if(full) {
for (int i=0;i<24;i++) {
if (i==goal) {
drawRoundLine((int)floor(deg[i][0]*90), (int)floor(deg[i][1]*90),6, GREEN);
} else if ((i+1)%24==goal||i-1==goal||(i==0&&goal==23)||(i+2)%24==goal||i-2==goal||(i==0&&goal==22)) {
drawRoundLine((int)floor(deg[i][0]*90), (int)floor(deg[i][1]*90),6, ORANGE);
} else if ((i+3)%24==goal||i-3==goal||(i==0&&goal==21)) {
drawRoundLine((int)floor(deg[i][0]*90), (int)floor(deg[i][1]*90),6, RED);
} else drawRoundLine((int)floor(deg[i][0]*90), (int)floor(deg[i][1]*90),6, GREY);
}
tft.setCursor(10, 10);
tft.setTextColor(YELLOW);
tft.setTextSize(3);
tft.print("Score: ");
tft.fillRect(10, 40, 60, 30, BLUE);
tft.setCursor(10, 40);
tft.print(score);
} else {
int i = (pos-dir)%24;
if(pos==0) i = dir==1?23:1;
drawRoundLine((int)floor(deg[pos][0]*90), (int)floor(deg[pos][1]*90),6, YELLOW);
if (i==goal) {
drawRoundLine((int)floor(deg[i][0]*90), (int)floor(deg[i][1]*90),6, GREEN);
return;
} else if ((i+1)%24==goal||i-1==goal||(i==0&&goal==23)||(i+2)%24==goal||i-2==goal||(i==0&&goal==22)) {
drawRoundLine((int)floor(deg[i][0]*90), (int)floor(deg[i][1]*90),6, ORANGE);
return;
} else if ((i+3)%24==goal||i-3==goal||(i==0&&goal==21)) {
drawRoundLine((int)floor(deg[i][0]*90), (int)floor(deg[i][1]*90),6, RED);
return;
}
drawRoundLine((int)floor(deg[i][0]*90), (int)floor(deg[i][1]*90),6, GREY);
}
}
void Reset() {
score = 0;
dir=1;
speed =275;
goal = random(0,24);
tft.fillScreen(BLUE);
Draw(true);
}
void loop()
{
digitalWrite(13, HIGH);
TSPoint p = ts.getPoint();
digitalWrite(13, LOW);
pinMode(XM, OUTPUT);
pinMode(YP, OUTPUT);
tft.print(p.z);
if (p.z > MINPRESSURE && p.z < MAXPRESSURE) {
p.x = map(p.x, TS_MINX, TS_MAXX, 0, tft.width());
p.y = map(p.y, TS_MINY, TS_MAXY, 0, tft.height());
if (pos==goal) {
score+=1;
delay(500);
} else if (!((pos+1)%24==goal||pos-1==goal||(pos==0&&goal==23)||(pos+2)%24==goal||pos-2==goal||(pos==0&&goal==22))) {
tft.fillScreen(RED);
tft.setCursor(55, 35);
tft.setTextColor(BLACK);
tft.setTextSize(4);
tft.print("GAME OVER");
tft.setCursor(27, 145);
tft.setTextColor(WHITE);
tft.setTextSize(3);
tft.print("Press to Restart");
pinMode(13, OUTPUT);
waitOneTouch();;
Reset();
}
speed*=0.9;
if(speed<30) speed = 30;
dir*=-1;
oldMilis=millis();
goal = random(0,24);
Draw(true);
} else {
if (!pause) {
if(millis()-oldMilis>speed) {
oldMilis=millis();
pos+=dir;
pos%=24;
if (pos==-1) pos=23;
Draw(false);
}
}
}
}
// Wait one touch
TSPoint waitOneTouch() {
TSPoint p;
do {
p= ts.getPoint();
pinMode(XM, OUTPUT); //Pins configures again for TFT control
pinMode(YP, OUTPUT);
} while((p.z < MINPRESSURE )|| (p.z > MAXPRESSURE));
return p;
}
Loading
ili9341-cap-touch
ili9341-cap-touch