// Importing library
#include <LiquidCrystal.h>
// Intializing variables
int penalty = 1;
float starttime = 0;
float endtime = 0;
int randArrow1 = 0;
int randArrow2 = 0;
int randArrow3 = 0;
int randArrow4 = 0;
int i;
int arrowDigit = 0;
int direction = 0;
int level = 0;
int score = 0;
int dynamicDelay = 2;
int answer = 0;
int VRx = A1;
int VRy = A0;
int xPosition = 0;
int yPosition = 0;
int mapX = 0;
int mapY = 0;
// Initializing arduino pins
LiquidCrystal lcd(12, 11, 6, 5, 4, 3); // RS, E, D4, D5, D6, D7
// Makes a custom character - an arrow pointing upwards
byte upArrow[8] = {
B00000,
B00100,
B01110,
B10101,
B00100,
B00100,
B00000,
B00000,
};
// Makes a custom character - an arrow pointing downwards
byte downArrow[8] = {
B00000,
B00100,
B00100,
B10101,
B01110,
B00100,
B00000,
B00000,
};
// Makes a custom character - an arrow pointing right
byte rightArrow[8] = {
B00000,
B00100,
B00010,
B11111,
B00010,
B00100,
B00000,
B00000,
};
// Makes a custom character - an arrow pointing left
byte leftArrow[8] = {
B00000,
B00100,
B01000,
B11111,
B01000,
B00100,
B00000,
B00000,
};
void setup() {
Serial.begin(9600);
Serial.print("Rules\nFour arrows of random orientations will appear on the LCD screen.\nUse the joy stick to mirror the direction the arrows are pointing in. \nThe program has NOT entered the joystick input until it is displayed on the screen!\n\nBeware! It gets harder at every level!");
pinMode(VRx, INPUT);
pinMode(VRy, INPUT);
// To get make the random function more random
randomSeed(analogRead(4));
// Assigns values to be associated with the custom characters
lcd.createChar(1, upArrow);
lcd.createChar(2, downArrow);
lcd.createChar(3, rightArrow);
lcd.createChar(4, leftArrow);
lcd.begin(16, 2);
}
void check() {
// Assigns the value for x and y potentiamoters
xPosition = analogRead(VRx);
yPosition = analogRead(VRy);
// Graphs the values of x and y potentiamoters
mapX = map(xPosition, 0, 1023, -512, 512);
mapY = map(yPosition, 0, 1023, -512, 512);
// Checks where the joystick is pointing and gives it a value asscoiated with the direction (1 for up, 2 for down, 3 for right, 4 for left)
if ((mapY > 300) and (-300 < mapX < 300)) {
answer = 1;
} else if ((mapY < -300) and (-300 < mapX < 300)) {
answer = 2;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
} else if ((mapX > 300) and (-300 < mapY < 300)) {////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
answer = 3; //34343434 ///change 3 n 4 !@#$%^&*()!@#$%^&*()!@#$%^&*I(!@#$%^&!@#$%^&@#$%^&@#$%^&@#$%^&)//////////////////////////////////////////////////
} else if ((mapX < -300) and (-300 < mapY < 300)) {//////////////////////////////////////////////////////////////////////////////////////////////////////////////
answer = 4;
}
}
void loop() {
// Displays 4 arrows of randomized orientations
lcd.clear();
delay(500);
randArrow1 = random(1, 5);
lcd.write(randArrow1);
delay(500);
lcd.clear();
delay(500);
randArrow2 = random(1, 5);
lcd.write(randArrow2);
delay(500);
lcd.clear();
delay(500);
randArrow3 = random(1, 5);
lcd.write(randArrow3);
delay(500);
lcd.clear();
delay(500);
randArrow4 = random(1, 5);
lcd.write(randArrow4);
delay(500);
lcd.clear();
starttime = micros();
endtime = starttime;
arrowDigit = 0;
for (int i = 0; i < 4; i = i + 1) {
delay(500);
answer = 0;
// Runs a loop (for a specifed amount of time). Loop checks weather the user has entered any input using the joystick or not.
while ((starttime - endtime) < (2 * 1000000)) {
check();
if (answer > 0) {
break;
}
starttime = (micros());
}
lcd.write(answer);
// Assigns values to 'arrowDigit' such that it repersents which arrow (1, 2, 3 or 4) is being tested for
// Assigns values to 'direction' such that it serves as the correct answer the user SHOULD be inputing
arrowDigit = arrowDigit + 1;
if (arrowDigit == 1) {
direction = randArrow1;
} else if (arrowDigit == 2) {
direction = randArrow2;
} else if (arrowDigit == 3) {
direction = randArrow3;
} else if (arrowDigit == 4) {
direction = randArrow4;
}
// Compares the correct answer to user's answer, for the correct arrow (1,2,3 or 4). Inflicts 'penalty' (loss) if answer is incorrect
if ((arrowDigit == 1) and (answer != direction)) {
penalty = 2;
break;
}else if ((arrowDigit == 2) and (answer != direction)) {
penalty = 2;
break;
}else if ((arrowDigit == 3) and (answer != direction)) {
penalty = 2;
break;
}else if ((arrowDigit == 4) and (answer != direction)) {
penalty = 2;
break;
}
delay(500);
}
// Counts what round the player is on. Calculates the player's score. Reduces the value of 'dynamicDelay' by 10% of its current value
level = level+1;
score = ((level-1)*4) + arrowDigit - 1;
dynamicDelay = dynamicDelay*.9;
// Displays lose screen and ends program.
if (penalty == 2) {
lcd.clear();
lcd.setCursor(0,0);
lcd.print("You Lost!");
lcd.setCursor(0,1);
lcd.print("lvl:");
lcd.print(level);
lcd.setCursor(7,1);
lcd.print("Score:");
lcd.print(score);
exit(0);
}
}