/*
Simple "Hello World" for ILI9341 LCD
https://wokwi.com/arduino/projects/308024602434470466
*/
#include "SPI.h"
#include "TFT.h"
// pin definition for monitor
#define cs 10
#define dc 9
#define rst 8 //reset
// create an instance of the library
TFT tft = TFT(cs, dc, rst);
// joy stick
#define VRX_PIN A0 // Arduino pin connected to VRX pin
#define VRY_PIN A1 // Arduino pin connected to VRY pin
#define JOY_BUT_PIN 2 // Arduino pin connected to SW pin
int xValue, yValue;
// part of circle for lenses
float xcoords[8] = {0.127380,0.222033,0.280319,0.300000,0.280319,0.222033,0.127380,0.0};
float ycoords[8] = {0.107153,0.229402,0.362050,0.500000,0.637950,0.770598,0.892847,1.0};
void setup() {
tft.begin();
tft.stroke(255, 255, 255);
Serial.begin(9600);
}
void readAnalogValues(int *box_index, int *lense_index) {
// read analog X and Y analog values
xValue = analogRead(VRX_PIN);
yValue = analogRead(VRY_PIN);
// Read the button value
if (xValue > 1000){
*box_index = (*box_index - 1);
if (*box_index < 0){
*box_index = 3;
}
}
if (xValue < 100){
*box_index = (*box_index + 1) % 4;
}
if (yValue > 1000){
*lense_index = (*lense_index + 1) % 4;
}
if (yValue < 100) {
*lense_index = (*lense_index - 1);
if (*lense_index < 0){
*lense_index = 3;
}
}
}
void drawPartCircle(int x, int y, int w, int h, bool concave){
int current_x = x;
int current_y = y;
int next_x, next_y;
if (concave){
w = -w;
}
for (int i = 0; i < 8; i++){
next_x = x + xcoords[i] * w;
next_y = y + ycoords[i] * h;
tft.line(current_x, current_y, next_x, next_y);
current_x = next_x;
current_y = next_y;
}
}
void draw_biconcave(int x, int y, int w, int h){
tft.line(x, y, x+w, y);
drawPartCircle(x+w, y, w, h, true);
tft.line(x+w, y+h, x, y+h);
drawPartCircle(x, y, w, h, false);
}
void draw_biconvex(int x, int y, int w, int h){
tft.line(x, y, x+w, y);
drawPartCircle(x+w, y, w, h, false);
tft.line(x+w, y+h, x, y+h);
drawPartCircle(x, y, w, h, true);
}
void draw_planoconvex(int x, int y, int w, int h){
tft.line(x, y, x+w, y);
drawPartCircle(x+w, y, w, h, false);
tft.line(x+w, y+h, x, y+h);
tft.line(x, y+h, x, y);
}
void draw_planoconcave(int x, int y, int w, int h){
tft.line(x, y, x+w, y);
drawPartCircle(x+w, y, w, h, true);
tft.line(x+w, y+h, x, y+h);
tft.line(x, y+h, x, y);
}
void draw_triangle(int x, int y, int w, int h){
tft.line(x, y, x+w, y);
tft.line(x+w, y, x+w/2, y+h);
tft.line(x+w/2, y+h, x, y);
}
typedef void (*lense_func_ptr)( int, int, int, int );
lense_func_ptr lense_functions[4] = {&draw_planoconcave, &draw_planoconvex, &draw_biconvex, &draw_biconcave};
void orderLenses(){
// vars for the game
bool finished = false;
int solution[4] = {3, 2, 0, 1};
int state[4] = {0, 0, 0, 0};
int cli = 0; // current lense index
int lli = 1; // last lense index
int cbi = 0; // current box index
int lbi = 1; // last box index
int lense_offset = 10;
int toff = 5; // triangle offset
int box_width = 30;
int box_sep = box_width + 10;
int cx = 1;
int cy = 50;
int th = 5; // triangle height
tft.text("Order the lenses!", 30, 10);
// draw the background and description
for (int i = 0; i < 4; i++){
tft.rect(cx+i*box_sep, cy, box_width, box_width);
}
// loop for changing lenses
while (!finished){
// read stick values in
readAnalogValues(&cbi, &cli);
// check if box changed
if (cbi != lbi){
// clear old triangles
tft.stroke(0, 0, 0);
draw_triangle(cx, cy-toff, box_width, -th);
draw_triangle(cx, cy+box_width+toff, box_width, th);
// draw new triangles
cx = cbi * box_sep;
tft.stroke(255, 255, 255);
draw_triangle(cx, cy-toff, box_width, -th);
draw_triangle(cx, cy+box_width+toff, box_width, th);
}
// check if lense changed
if (cli != lli){
state[cbi] = cli;
// clear old lense
tft.stroke(0, 0, 0);
lense_functions[lli](cx+lense_offset, cy+5, 10, box_width-10);
// draw new lense
tft.stroke(255, 255, 255);
lense_functions[cli](cx+lense_offset, cy+5, 10, box_width-10);
}
lbi = cbi;
lli = cli;
// check if lenses are in right order
finished = true;
for (int i = 0; i < 4; i++){
finished = solution[i] == state[i] && finished;
}
delay(150);
}
Serial.print("You won the game!!!");
delay(1000);
}
void loop() {
orderLenses();
}