I have problems in the BrainTest() function When I load it the starting text doesn't appear on the LCD, it goes straight to the:
if (button == 0) {
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Let's start");
delay (1400);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("test");
}
I also think there is a problem to select either "Yes" or "No" at the beginning of the function.
Any help would be appreciated...
#include <LiquidCrystal.h>
// Arduino pins number
const int SW_pin = 2; // digital pin connected to switch output
const int X_pin = 0; // analog pin connected to X output
const int Y_pin = 1; // analog pin connected to Y output
const int LCD_RS = 7;
const int LCD_Enable = 8;
const int LCD_D4 = 9;
const int LCD_D5 = 10;
const int LCD_D6 = 11;
const int LCD_D7 = 12;
LiquidCrystal lcd(LCD_RS, LCD_Enable, LCD_D4, LCD_D5, LCD_D6, LCD_D7);
// Basic vars
int none = 0;
String Apps[3] = {"App select","Credits","Test your brain"};
int CurrentApp = 0;
int Yaxis = 1;
int Xaxis = 1;
int HiCh = 0;
int button;
int JXaxis;
int JYaxis;
int MaxHi;
void AppSelect() { // APPSELECT
lcd.setCursor(0,0);
lcd.print("App select menu");
lcd.setCursor(0,1);
lcd.print(Apps[HiCh]);
if (button == 0) {
SelectApp();
}
if (JYaxis <= 2) {
if (HiCh != 0) {
HiCh = HiCh - 1;
lcd.clear();
lcd.setCursor(0,1);
lcd.print(Apps[HiCh]);
delay(300);
}
}
if (JYaxis >= 7) {
if (HiCh != 1) {
HiCh = HiCh + 1;
lcd.clear();
lcd.setCursor(0,1);
lcd.print(Apps[HiCh]);
delay(300);
}
}
}
void Credits() { // CREDITS
MaxHi = 0;
Serial.print("- Credits app loading \n");
lcd.clear();
lcd.setCursor(9,0);
lcd.print("Credits");
lcd.setCursor(0,1);
lcd.print("Made by Alexandre Bergeron");
Serial.print("- Credits app loaded \n");
delay(1300);
lcd.scrollDisplayLeft();
delay(600);
lcd.scrollDisplayLeft();
delay(600);
lcd.scrollDisplayLeft();
delay(600);
lcd.scrollDisplayLeft();
delay(600);
lcd.scrollDisplayLeft();
delay(600);
lcd.scrollDisplayLeft();
delay(600);
lcd.scrollDisplayLeft();
delay(600);
lcd.scrollDisplayLeft();
delay(600);
lcd.scrollDisplayLeft();
delay(600);
lcd.scrollDisplayLeft();
delay(600);
lcd.scrollDisplayLeft();
delay(600);
lcd.scrollDisplayLeft();
delay(1500);
CurrentApp = 0;
lcd.clear();
}
void BrainTest() { // BRAINTEST
MaxHi = 1;
HiCh = 0;
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Are you ready?");
lcd.setCursor(0,1);
lcd.print("Yes");
for (;;) {
button = digitalRead(SW_pin);
if (HiCh == 0) {
lcd.setCursor(0,1);
lcd.print("Yes");
if (button == 0) {
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Let's start");
delay (1400);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("done");
}
}
if (HiCh == 1) {
lcd.setCursor(0,1);
lcd.print("No");
if (button == 0) {
CurrentApp = 0;
}
}
break;
}
}
void setup() { // SETUP
Serial.begin(9600);
Serial.print("[2J");
Serial.print(" Serial Monitor opened \n \n");
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
Serial.print("- App selector menu \n");
pinMode(SW_pin, INPUT);
digitalWrite(SW_pin, HIGH);
}
void SelectApp() { // SELECTAPP
switch (HiCh) {
case (0):
CurrentApp = 0;
AppSelect();
break;
case (1):
CurrentApp = 1;
Credits();
break;
case (2):
CurrentApp = 2;
BrainTest();
break;
default:
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Error");
Serial.print("- App loading error \n");
break;
}
}
void loop() { // LOOP
Serial.print(HiCh);
button = digitalRead(SW_pin);
int JYaxis = analogRead(Y_pin) / 128;
int JXaxis = analogRead(X_pin) / 128;
if (JYaxis >= 7) {
if (HiCh != 0) {
HiCh = HiCh - 1;
delay(300);
}
}
if (JYaxis <= 2) {
if (HiCh != MaxHi) {
HiCh = HiCh + 1;
delay(300);
}
}
if (CurrentApp == 0) {
MaxHi = 2;
lcd.setCursor(0,0);
lcd.print("App select menu");
lcd.setCursor(0,1);
lcd.print(Apps[HiCh]);
if (button == 0) {
SelectApp();
}
if (JYaxis >= 7) {
if (HiCh != 0) {
lcd.clear();
lcd.setCursor(0,0);
lcd.print("App select menu");
lcd.setCursor(0,1);
lcd.print(Apps[HiCh]);
delay(300);
}
}
if (JYaxis <= 2) {
if (HiCh != 2) {
lcd.clear();
lcd.setCursor(0,0);
lcd.print("App select menu");
lcd.setCursor(0,1);
lcd.print(Apps[HiCh]);
delay(300);
}
}
}
}
If you think I should change anything in the question tell me and I'll correct it. Also if you fount other problems or upgrades to my code just tell me out and I'll consider them.
.
Now the SelectApp(); function is working well but in the BrainTest(); function the first question isn't working. I cannot switch between answers...
Here's the new code
#include <LiquidCrystal.h>
// Arduino pins number
const int SW_pin = 2; // digital pin connected to switch output
const int X_pin = 0; // analog pin connected to X output
const int Y_pin = 1; // analog pin connected to Y output
const int LCD_RS = 7;
const int LCD_Enable = 8;
const int LCD_D4 = 9;
const int LCD_D5 = 10;
const int LCD_D6 = 11;
const int LCD_D7 = 12;
LiquidCrystal lcd(LCD_RS, LCD_Enable, LCD_D4, LCD_D5, LCD_D6, LCD_D7);
// Basic vars
int none = 0;
String Apps[3] = {"App select","Credits","Test your brain"};
int CurrentApp = 0;
int Yaxis = 1;
int Xaxis = 1;
int HiCh = 0;
int button;
int JXaxis;
int JYaxis;
int MaxHi;
void AppSelect() { // APPSELECT
lcd.setCursor(0,0);
lcd.print("App select menu");
lcd.setCursor(0,1);
lcd.print(Apps[HiCh]);
if (button == 0) {
SelectApp();
}
if (JYaxis <= 2) {
if (HiCh != 0) {
HiCh = HiCh - 1;
lcd.clear();
lcd.setCursor(0,1);
lcd.print(Apps[HiCh]);
delay(300);
}
}
if (JYaxis >= 7) {
if (HiCh != 1) {
HiCh = HiCh + 1;
lcd.clear();
lcd.setCursor(0,1);
lcd.print(Apps[HiCh]);
delay(300);
}
}
}
void Credits() { // CREDITS
MaxHi = 0;
Serial.print("- Credits app loading \n");
lcd.clear();
lcd.setCursor(9,0);
lcd.print("Credits");
lcd.setCursor(0,1);
lcd.print("Made by Alexandre Bergeron");
Serial.print("- Credits app loaded \n");
delay(1300);
lcd.scrollDisplayLeft();
delay(600);
lcd.scrollDisplayLeft();
delay(600);
lcd.scrollDisplayLeft();
delay(600);
lcd.scrollDisplayLeft();
delay(600);
lcd.scrollDisplayLeft();
delay(600);
lcd.scrollDisplayLeft();
delay(600);
lcd.scrollDisplayLeft();
delay(600);
lcd.scrollDisplayLeft();
delay(600);
lcd.scrollDisplayLeft();
delay(600);
lcd.scrollDisplayLeft();
delay(600);
lcd.scrollDisplayLeft();
delay(600);
lcd.scrollDisplayLeft();
delay(1500);
CurrentApp = 0;
lcd.clear();
}
void BrainTest() { // BRAINTEST
MaxHi = 1;
HiCh = 0;
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Are you ready?");
lcd.setCursor(0,1);
lcd.print("Yes");
for (;;) {
button = digitalRead(SW_pin);
if (HiCh == 0) {
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Are you ready?");
lcd.setCursor(0,1);
lcd.print("Yes");
if (button == 0) {
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Let's start");
delay (1400);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("done");
}
}
if (HiCh == 1) {
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Are you ready?");
lcd.setCursor(0,1);
lcd.print("No");
if (button == 0) {
CurrentApp = 0;
}
}
break;
}
}
void setup() { // SETUP
Serial.begin(9600);
Serial.print("[2J");
Serial.print(" Serial Monitor opened \n \n");
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
Serial.print("- App selector menu \n");
pinMode(SW_pin, INPUT);
digitalWrite(SW_pin, HIGH);
}
void SelectApp() { // SELECTAPP
switch (HiCh) {
case (0):
CurrentApp = 0;
AppSelect();
break;
case (1):
CurrentApp = 1;
Credits();
break;
case (2):
CurrentApp = 2;
BrainTest();
break;
default:
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Error");
Serial.print("- App loading error \n");
break;
}
}
void loop() { // LOOP
Serial.print(HiCh);
button = digitalRead(SW_pin);
int JYaxis = analogRead(Y_pin) / 128;
int JXaxis = analogRead(X_pin) / 128;
if (JYaxis >= 7) {
if (HiCh != 0) {
HiCh = HiCh - 1;
delay(300);
}
}
if (JYaxis <= 2) {
if (HiCh != MaxHi) {
HiCh = HiCh + 1;
delay(300);
}
}
if (CurrentApp == 0) {
MaxHi = 2;
lcd.setCursor(0,0);
lcd.print("App select menu");
lcd.setCursor(0,1);
lcd.print(Apps[HiCh]);
if (button == 0) {
while (button == 0) {
delay (200);
break;
}
SelectApp();
}
if (JYaxis >= 7) {
if (HiCh != 0) {
lcd.clear();
lcd.setCursor(0,0);
lcd.print("App select menu");
lcd.setCursor(0,1);
lcd.print(Apps[HiCh]);
delay(300);
}
}
if (JYaxis <= 2) {
if (HiCh != 2) {
lcd.clear();
lcd.setCursor(0,0);
lcd.print("App select menu");
lcd.setCursor(0,1);
lcd.print(Apps[HiCh]);
delay(300);
}
}
}
}
You are handling the button completely wrong. You have microseconds to release button after you select the app. So it looks like it's pressed, because it is. You never checked if it was ever released.
I'd recommend to start with super simple program: Count how many times the button was pressed and send it to the serial.
You can't create anything like you're trying to, until you can make this simple button press counter working.