I am a beginner in Arduino programing. I have this code below:
#include<LiquidCrystal.h>
LiquidCrystal lcd(12,11,7,6,5,4);
//Desingation pin
int led = 13;
//Variables
long number1;
long number2;
long calcul;
long result;
int q1;
int q2;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
lcd.begin(16,2);
pinMode(led,OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
lcd.home();
lcd.print("Snake & Ladder");
lcd.setCursor(0,1);
lcd.print("Let's Calculate!");
delay(3500);
lcd.clear();
if(q1 == 0 && q2 == 0){//condition if question 1&2 has not pass
lcd.home();
lcd.print("A="); //print first random nummber 0-6
number1 = random(6);
delay(500);
lcd.print(number1);
delay(500);
lcd.print(",");
delay(500);
lcd.print("B=");//print second random number 0-6
number2 = random(6);
lcd.print(number2);
delay(500);
lcd.print(", A+B=?");
calcul = number1 + number2;//Calculate the equation
while(q1 == 0 && q2 == 0){//loop with condition if question 1&2 has not pass
result = Serial.parseInt();
if(result == calcul){ //if answer correct
lcd.clear();
lcd.home();
lcd.print("A=");
lcd.print(number1);
lcd.print(",");
lcd.print("B=");
lcd.print(number2);
lcd.print(", A+B=?");
lcd.setCursor(0,1);
lcd.print(result);
digitalWrite(led,HIGH);
lcd.print(" GOOD, PROCEED");
delay(2000);
digitalWrite(led,LOW);
q1 = 1;
}
if(result != calcul && result >= 1){ //if answer is incorrect
lcd.clear();
lcd.home();
lcd.print("A=");
lcd.print(number1);
lcd.print(",");
lcd.print("B=");
lcd.print(number2);
lcd.print(", A+B=?");
lcd.setCursor(0,1);
lcd.print(result);
lcd.print(" RETRY, BACK 1");
for(int i = 0; i < 3; i++){
digitalWrite(led,HIGH);
delay(500);
digitalWrite(led,LOW);
delay(500);
}
}
}
lcd.clear();
}
}
The program is intended to be a random number generator where we have to find the correct sum of the two numbers. The program itself works but only runs once, when the answer is correct. How do I make the program so it can repeat itself countless times?
Thanks for replying Steve. Is it looping because of the for int? If so, is there any way I can adjust the program to repeat the question over and over again?
The setup() function runs once at the start. After that,
the loop() function (and anything inside of it) runs repeatedly, typically thousands of times per second unless you are doing things that slow it down.
If things in the loop() function won't happen unless q1 is zero but you set q1 to one, nothing more will happen. It has nothing to do with you flashing an LED in the "for" loop. Nothing.
Thanks for pointing it out for me. I soon realize what was wrong. The code has been updated below:
#include<LiquidCrystal.h>
LiquidCrystal lcd(12,11,7,6,5,4);
//Variables
long number1;
long number2;
long calcul;
long result;
int q1;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
lcd.begin(16,2);
}
void loop() {
// put your main code here, to run repeatedly:
lcd.home();
lcd.print("Snake & Ladder");
lcd.setCursor(0,1);
lcd.print("Let's Calculate!");
delay(500);
lcd.clear();
if(q1 == 0){//condition if question has not pass
lcd.home();
lcd.print("A="); //print first random nummber 0-6
number1 = random(6);
delay(500);
lcd.print(number1);
delay(500);
lcd.print(",");
delay(500);
lcd.print("B=");//print second random number 0-6
number2 = random(6);
lcd.print(number2);
delay(500);
lcd.print(", A+B=?");
calcul = number1 + number2;//Calculate the equation
while(q1 == 0){//loop with condition if question has not pass
result = Serial.parseInt();
if(result == calcul){ //if answer correct
lcd.clear();
lcd.home();
lcd.print("A=");
lcd.print(number1);
lcd.print(",");
lcd.print("B=");
lcd.print(number2);
lcd.print(", A+B=?");
lcd.setCursor(0,1);
lcd.print(result);
lcd.print(" RIGHT");
delay(500);
q1 = 1;
}
if(result != calcul && result >= 1){ //if answer is incorrect
lcd.clear();
lcd.home();
lcd.print("A=");
lcd.print(number1);
lcd.print(",");
lcd.print("B=");
lcd.print(number2);
lcd.print(", A+B=?");
lcd.setCursor(0,1);
lcd.print(result);
lcd.print(" WRONG, BACK 1");
for(int i = 0; i < 3; i++){
}
}
}
lcd.clear();
}
lcd.home();
lcd.print("PLEASE MOVE");
lcd.setCursor(0,1);
lcd.print(result);
delay(3000);
lcd.clear(); //Clear the LCD screen for repetition process
q1 = 0;
}
If there is anyway it can be improve I'm open for any recommendations. I would like to point out that I removed the Led part of the program since I see it as not that important for now and that the program works as I wished. Once again thanks for the info and help.