/*
ESP32 HTTPClient Jokes API Example
https://wokwi.com/projects/342032431249883731
Copyright (C) 2022, Uri Shaked
*/
//#include <WiFi.h>
//#include <HTTPClient.h>
//#include <ArduinoJson.h>
//#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
//#define BTN_PIN 5
#define TFT_DC 2
#define TFT_CS 15
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
const char* Whi = "White "; /// The Texts!!
const char* Red = " Red ";
const char* Blu = " Blue ";
const char* Yel = "Yellow";
const char* Grn = "Green ";
const uint16_t ColorArr[] = {WHITE, 1, 1, 2, 2}; //The Colors!!
bool ButtonPushed = false; //set while button is pushed
bool WaitForIt = false; //set to pause a bit to allow player to read hit/miss result
int Button = 5; //the Pin-Number of the button used.
int TextNo = 8; //Number of text displayed - 8 for "Ready" message
int ColorNo = 0; //Number of Color the text is displayed in
int Hits; //counts the number of Hits (correct pushes)
int Miss; //counts the number of Misses (and incorrect pushes)
int Fix = 0; //ensures we don't wait too long for a match!
int FixMax = 5; //Force a match after this many mismatches (prevent boredom!!)
long StartTime = 0; //set to millis() value when we start timing
long DelayTime = 0; // how many millis have elapsed since start
long DispTime = 0; // random delay to next tft - gradually gets faster!
long TimeToBeat = 9999; //remembers fastest reaction time during game
long ReadTime = 2000; //time allowed to read result(longer at end)
long score = 0; //keep the score! (sum of HitSpeeds!)
long speed = 2000; //how fast the colors come(gets faster by 5% every hit)
void setup() {
pinMode(Button, INPUT_PULLUP);
tft.begin();
tft.setRotation(1);
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(2);
}
void showScore(){
tft.clearDisplay(); //Dark Mode!! :-)
tft.setTextColor(ILI9341_WHITE);
tft.setCursor(0,0);
tft.print("Score:");
tft.print(score); //score at top left
tft.setCursor(80,0);
tft.print("Miss:");
tft.print(Miss); //misses (Lives?) at top right
tft.setCursor (10, 20); //near middle of screen??
}
void loop() {
// read the input pin:
ButtonPushed = !digitalRead(Button); //invert so a push is a 1
if (!WaitForIt){ //Display random Text/Color every sec while waiting for push
if (!ButtonPushed){
if((millis()-StartTime)>DispTime){ //time to display a new combo..
showScore();
if (Miss > 4) { //display Game Over until next button push if done
tft.setCursor (10, 20); //a little higher!
tft.print(" Game");
tft.println(" Over!");
tft.println("");
tft.print("Hits=");
tft.print(Hits);
tft.print(" Min="); //display fastest reaction time in ms
tft.print(TimeToBeat);
tft.print("ms");
}
else if (TextNo==8){ //new game started
tft.print("Ready...");
TextNo = 6;
}
else if (TextNo == ColorNo){ //check if we missed the last one!!
Miss++; //we lose a life!
showScore();
tft.print("Miss!!!");
TextNo = 6; //ensure the miss is not counted again the next time round!
Fix = 0; // reset the count to next forced hit
if(Miss>4){ //all lives lost!
ReadTime = 5000; //longer after last miss!!
}
WaitForIt = true; //wait for miss to be read
StartTime = millis();
}
else { //we need to display a new combo..
TextNo = random(0,5); //choose a random text
if(Fix++ > FixMax){ //we haven't had a match for a while
ColorNo = TextNo; //so make this one a match!
}
else {
ColorNo = random(0,5); //choose a random color
if((Fix == 1)&&(ColorNo == TextNo)){ //if we get a match on the first go..
ColorNo = TextNo + 1; //force a different color
if (ColorNo > 4){
ColorNo = 0; //ensure we don't get 2 matches in a row (too difficult!)
}
}
}
//display the random text in the random color
tft.setTextColor(ColorArr[ColorNo]);
if (TextNo==0){
tft.print(Whi);
}
else if(TextNo==1){
tft.print(Red);
}
else if(TextNo==2){
tft.print(Blu);
}
else if(TextNo==3){
tft.print(Yel);
}
else {
tft.print(Grn);
}
tft.print(" ");
if (ColorNo==0){
tft.print(Whi);
}
else if(ColorNo==1){
tft.print(Red);
}
else if(ColorNo==2){
tft.print(Blu);
}
else if(ColorNo==3){
tft.print(Yel);
}
else {
tft.print(Grn);
}
}
tft.display(); // Show initial text
StartTime = millis();
DispTime = random(speed-200,speed); //new display time slightly random (starts around 2 sec and speeds up!)
}
}
else { // button is pushed
showScore();
if (Miss > 4) { //start new game
tft.println("Resetting-");
score = 0;
TimeToBeat = 9999;
speed = 2000;
Hits = 0;
Miss = 0;
TextNo = 8; //print "Ready..." text.
}
else if (TextNo==ColorNo){ //we have a good hit!
DelayTime = millis() - StartTime;
tft.print("You Took.. ");
tft.print(DelayTime);
tft.println("ms");
if (DelayTime<TimeToBeat){
tft.println();
tft.print(" BEST ");
tft.println("..so far!!");
tft.println();
TimeToBeat = DelayTime;
}
else { //not the best!
tft.println();
tft.print("Fastest is ");
tft.print(TimeToBeat);
tft.println("ms");
tft.println();
}
Hits++;
Fix = 0;
score = score + 60000/DelayTime; //score for hit based on speed (in clicks per minute)
speed = speed - (speed/20); //speed up changes by 5% after every hit
FixMax = random(5, 7); //match will happen before 7 cycles
TextNo = 6; //ensure this hit not counted as a miss!
}
else { //Bad Push - not equal!
tft.println(" Too Quick!");
tft.println();
tft.println("Colors did not match!");
tft.println();
Miss++; //lose a life!
if(Miss>4){
ReadTime = 5000; //longer after last miss!!
}
}
tft.print("Hits=");
tft.print(Hits);
tft.print(" Miss=");
tft.println(Miss);
tft.display(); // Show initial text
WaitForIt = true;
StartTime = millis();
}
}
else if (ButtonPushed){ //wait for button released!
StartTime = millis();
}
else { //wait 2s (after button release!) for result to be read...
if ((millis()-StartTime) > ReadTime){
WaitForIt = false;
ReadTime = 2000; //reset to normal
}
}
}