Need help with end of code. I hooked up a joystick to an LCD. I want the LCD to display a sequence UP, DOWN, LEFT, RIGHT. If you do anything wrong in the sequence it will display BOOM! on the LCD. I also got a countdown timer hooked up so its hard to use delays or while functions. The problem is when I'm using the down joystick it displays boom even though it should not.
#include <LiquidCrystal.h>
#include "SevSeg.h" //Seven Segment 4 Digit Library
SevSeg sevseg;
int up;
int down;
int left;
int right;
int value;
float displayTimeSecs = 0.1; //how long do you want each number on display to show (in secs)
float displayTime = (displayTimeSecs * 5000);
long buzzerFrequency = 500;
float buzzerDuration = (displayTimeSecs * 100);
long startNumber = 500; //countdown starts with this number
long endNumber = 0; //countdown ends with this number
const int buzzerPin = 52;
const int leftButton = 50;
const int rightButton = 51;
const int rs = 22 , en = 23, d4 = 24, d5 = 25, d6 = 26, d7 = 27;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
int VRxAnalogRead;
int VRyAnalogRead;
void setup() {
pinMode(buzzerPin,OUTPUT);
Serial.begin(9600);
lcd.begin(16,2);
byte numDigits = 4;
byte digitPins[] = {10, 11, 12, 13};
byte segmentPins[] = {9, 2, 3, 5, 6, 8, 7, 4};
bool resistorsOnSegments = false;
byte hardwareConfig = COMMON_CATHODE;
bool updateWithDelays = false;
bool leadingZeros = false;
bool disableDecPoint = true;
sevseg.begin(hardwareConfig, numDigits, digitPins, segmentPins, resistorsOnSegments,
updateWithDelays, leadingZeros, disableDecPoint);
sevseg.setBrightness(90);
}
void millisMessage1(){ //instead of delay 3 sec
if(millis()==3000){
lcd.setCursor(0,0);
lcd.print(" ");
}
}
void millisMessage2(){ //instead of delay 3 sec
if(millis()>=6000){
lcd.setCursor(0,0);
lcd.print(" ");
lcd.setCursor(0,1);
lcd.print(" ");
}
}
void millisMessage3(){ //instead of delay 3 sec
if(millis()>=9000){
lcd.setCursor(0,0);
lcd.print(" ");
lcd.setCursor(0,1);
lcd.print(" ");
}
}
void millisMessage4(){ //instead of delay 1 sec
if(millis()>=10000){
lcd.setCursor(0,0);
lcd.print(" ");
lcd.setCursor(0,1);
lcd.print(" ");
}
}
void millisMessage5(){ //instead of delay 1 sec
if(millis()>=11000){
lcd.setCursor(0,0);
lcd.print(" ");
lcd.setCursor(0,1);
lcd.print(" ");
}
}
void millisMessage6(){ //instead of delay 1 sec
if(millis()>=12000){
lcd.setCursor(0,0);
lcd.print(" ");
lcd.setCursor(0,1);
lcd.print(" ");
}
}
void joystick(){
VRxAnalogRead = analogRead(A0); //VRx
VRyAnalogRead = analogRead(A1); //VRy
Serial.print("VRx Analog Value");//sets VRx as this saying when set to serial monitor
Serial.println(VRxAnalogRead);// reads VRx a new line after the end of print
Serial.print("VRy Analog Value");
Serial.println(VRyAnalogRead);
}
void lcdBoom(){
lcd.setCursor(0,0);
lcd.print("BOOOOOOOOOOOOOM!");
lcd.setCursor(0,1);
lcd.print(" ");
}
void boomUp(){ //
if(VRxAnalogRead >=600){
value = 1;
thirdMessage();
}
else if(VRxAnalogRead<=100){
value = 2;
lcdBoom();
}
else if(VRyAnalogRead<=100){
value = 3;
lcdBoom();
}
else if(VRyAnalogRead>=600){
value = 4;
lcdBoom();
}
}
void boomDown(){
if(VRxAnalogRead<=100){
value = 5;
fourthMessage();
}
else if(VRxAnalogRead >=600){
value = 6;
lcdBoom();
}
else if(VRyAnalogRead<=100){
value = 7;
lcdBoom();
}
else if(VRyAnalogRead>=600){
value = 8;
lcdBoom();
}
}
void boomLeft(){
if(VRyAnalogRead<=100){
value = 9;
fifthMessage();
}
else if(VRxAnalogRead >=600){
value = 10;
lcdBoom();
}
else if(VRxAnalogRead<=100){
value = 11;
lcdBoom();
}
else if(VRyAnalogRead>=600){
value = 12;
lcdBoom();
}
}
void boomRight(){
if(VRyAnalogRead>=600){
value = 13;
sixthMessage();
}
else if(VRxAnalogRead >=600){
value = 14;
lcdBoom();
}
else if(VRxAnalogRead<=100){
value = 15;
lcdBoom();
}
else if(VRyAnalogRead<=100){
value = 16;
lcdBoom();
}
}
void firstMessage(){
lcd.print("Defuse The Bomb!");
millisMessage1();
}
void secondMessage(){
lcd.setCursor(0,0);
lcd.print("Joystick: ");
lcd.setCursor(0,1);
lcd.print("UP Don't mess up");
millisMessage2();
}
void thirdMessage(){
millisMessage3();
lcd.setCursor(0,0);
lcd.print("Joystick: ");
lcd.setCursor(0,1);
lcd.print("Down! ");
}
void fourthMessage(){
millisMessage4();
lcd.setCursor(0,0);
lcd.print("Joystick:");
lcd.setCursor(0,1);
lcd.print("LEFT! ");
millisMessage4();
}
void fifthMessage(){
millisMessage5();
lcd.setCursor(0,0);
lcd.print("Joystick:");
lcd.setCursor(0,1);
lcd.print("RIGHT! ");
}
void sixthMessage(){
millisMessage6();
lcd.setCursor(0,0);
lcd.print("Time for Buttons ");
lcd.setCursor(0,1);
lcd.print(" ");
}
void loop() {
{
joystick();
millis();
}
{
tone(buzzerPin,buzzerFrequency,buzzerDuration);
if (startNumber >= endNumber) {
for (long i = 0; i <= displayTime; i++){
sevseg.setNumber(startNumber,0);
sevseg.refreshDisplay();
if(startNumber==0){
sevseg.setNumber(0000,0);
tone(buzzerPin,buzzerFrequency,buzzerDuration);
}
}
}
startNumber--;
tone(buzzerPin,buzzerFrequency,buzzerDuration);
}
{
sevseg.setNumber(0000,0); //after countdown shows endNumber, show this number.
sevseg.refreshDisplay();
}
if(startNumber ==499){
firstMessage(); // Defuse the bomb
}
if(startNumber ==480){ //up
secondMessage(); // joystick up
}
if(startNumber<=479){
boomUp();
if(value==1){
boomUp();
if(value==2){
if(value==3){
if(value==4){
if(value==5){
boomDown();
if(value==6){
if(value==7){
if(value==8){
if(value==9){
boomLeft();
if(value==10){
if(value==11){
if(value==12){
if(value==13){
boomRight();
if(value==14){
if(value==15){
if(value==16){
}}}}}}}}}}}}}}}}}}