I have 4 LEDs which represent left, right, open and down. I also have a Joystick. My goal is it to light up one LED and move the joystick to this direction. I am almost finished, but I am now realizing, that my code only checks the Joystick position when the LED is set On (really short timeframe).
How can I let my code check the joystick input during the delay time I have included, to even make this game playable?
int r = 10; //right
int o = 11; //up
int l = 12; //left
int u = 13; //down
int highscore = 0;
boolean checker = true;
boolean start = false;
char wo;
int i = 0;
int points = 0;
//LCD IC2 libaries
#include <Wire.h> // Wire Bibliothek
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x3f, 16, 2);
//Joystick
#define VRX_PIN A0 // Arduino pin connected to VRX pin
#define VRY_PIN A1 // Arduino pin connected to VRY pin
#define LEFT_THRESHOLD 400
#define RIGHT_THRESHOLD 800
#define UP_THRESHOLD 400
#define DOWN_THRESHOLD 800
#define COMMAND_NO 0x00
#define COMMAND_LEFT 0x01
#define COMMAND_RIGHT 0x02
#define COMMAND_UP 0x04
#define COMMAND_DOWN 0x08
int xValue = 0 ; // To store value of the X axis
int yValue = 0 ; // To store value of the Y axis
int command = COMMAND_NO;
void setup() {
Serial.begin(9600);
pinMode(r, OUTPUT);
pinMode(o, OUTPUT);
pinMode(u, OUTPUT);
pinMode(l, OUTPUT);
//initialize lcd screen
lcd.init();
// turn on the backlight
lcd.backlight();
lcd.setCursor(0,0);
lcd.print("Bewege den Joy-");
lcd.setCursor(0,1);
lcd.print("stick nach unten");
}
void loop() {
//Joystick part
xValue = analogRead(VRX_PIN);
yValue = analogRead(VRY_PIN);
// converts the analog value to commands
// reset commands
command = COMMAND_NO;
// check left/right commands
if (xValue < LEFT_THRESHOLD)
command = command | COMMAND_LEFT;
else if (xValue > RIGHT_THRESHOLD)
command = command | COMMAND_RIGHT;
// check up/down commands
if (yValue < UP_THRESHOLD)
command = command | COMMAND_UP;
else if (yValue > DOWN_THRESHOLD)
command = command | COMMAND_DOWN;
int randomChoice = random(4); // Generiert eine Zufallszahl zwischen 0 und 3
if (start == true && i<10){
switch (randomChoice) {
case 0:
digitalWrite(r, HIGH);
digitalWrite(o, LOW);
digitalWrite(u, LOW);
digitalWrite(l, LOW);
wo = r;
i ++;
break;
case 1:
digitalWrite(r, LOW);
digitalWrite(o, HIGH);
digitalWrite(u, LOW);
digitalWrite(l, LOW);
wo = o;
i ++;
break;
case 2:
digitalWrite(r, LOW);
digitalWrite(o, LOW);
digitalWrite(u, HIGH);
digitalWrite(l, LOW);
wo = u;
i ++;
break;
case 3:
digitalWrite(r, LOW);
digitalWrite(o, LOW);
digitalWrite(u, LOW);
digitalWrite(l, HIGH);
wo = l;
i ++;
break;
default:
digitalWrite(r, LOW);
digitalWrite(o, LOW);
digitalWrite(u, LOW);
digitalWrite(l, LOW);
break;
}
delay(200);
digitalWrite(r, LOW);
digitalWrite(o, LOW);
digitalWrite(u, LOW);
digitalWrite(l, LOW);
lcd.clear();
lcd.setCursor(0,0);
lcd.print(i);
lcd.print(" von 10");
lcd.setCursor(0,1);
lcd.print(points);
lcd.print(" Treffer");
delay(random(800, 2500));
// print command to serial and process command
if (command & COMMAND_LEFT && wo == l) {
Serial.println("COMMAND LEFT");
points ++;
}
if (command & COMMAND_RIGHT && wo == r) {
Serial.println("COMMAND RIGHT");
points ++;
}
if (command & COMMAND_UP && wo == o) {
Serial.println("COMMAND UP");
points ++;
}
if (command & COMMAND_DOWN && wo == u) {
Serial.println("COMMAND DOWN");
points ++;
}
}
if (points>highscore){
highscore = points;
}
if(i==10){
start = false;
delay(50);
lcd.setCursor(0,0);
lcd.print("Punkte: ");
lcd.print(points);
lcd.setCursor(0,1);
lcd.print("Highscore: ");
lcd.print(highscore);
if (command & COMMAND_DOWN && checker == false){
start = true;
i = 0;
points = 0;
}
}
}
For this purpose, when you need to track time while doing other actions, there're peripherals called Timers/Counters (they have many purposes but this is the basic one) in the MCU which can generate interrupts after a set time. This looks like a nice project to get started with them!
In very simplified way you'll need to enable one and set its parameters, calculate the desired number of ticks (there're many calculators for it online, for example this one) to achieve your desired time and implement an interrupt routine which will be used as a notification showing that the set time has elapsed. But I believe you can find better information source about them like a datasheet/reference manual of your MCU or many already working examples from arduino.