#include <Wire.
h> //initialize button1,button2 and {
button3 as input, combined
#include <LiquidCrystal_I2C.h>
with pull up
LiquidCrystal_I2C lcd.setCursor(1,0);
pinMode(button1,
lcd(0x27,16,2);
INPUT_PULLUP); lcd.print("1. 1 + 1 = ?");
#define button1 2 //the number
pinMode(button2, lcd.setCursor(1,1);
of the button 1
INPUT_PULLUP);
lcd.print("A.1 B.11 C.2");
#define button2 3 //button 2
pinMode(button3,
attach to //turn all the led off
INPUT_PULLUP);
#define button3 4 //button3 digitalWrite(LED1,LOW);
pinMode(button4,
attach to
INPUT_PULLUP);
digitalWrite(LED2,LOW);
#define button4 9 //button4
//turn all the led off
attach to digitalWrite(LED3,LOW);
digitalWrite(LED1,LOW);
#define buzzerPin 5 //the digitalWrite(LED4,LOW);
buzzer attach to digitalWrite(LED2,LOW);
//read the state of the button4
#define LED1 6 //LED 1 attach to digitalWrite(LED3,LOW);
#define LED2 7 //LED 2 attach to digitalWrite(LED4,LOW);
b4State = digitalRead(button4);
#define LED3 8 //LED 3 attach to }
//when button4 (reset) pressed
#define LED4 10 //LED4 attach
if(b4State == 0)
to
void loop() {
{
#define uint8 unsigned char
if(b4State == 0)//confirm that
int x;
if (y == 0) the button4 is pressed
int y=0;
{{lcd.init(); {
uint8 flag = 0; //used to indicate
lcd.backlight(); flag = 1; //if so, flag is 1
the state of button4 key
lcd.setCursor(5,0); digitalWrite(LED4,
uint8
HIGH);//turn the host LED on
b1State,b2State,b3State,b4Stat lcd.print("WELCOME!");
e = 0; delay(200);
lcd.setCursor(1,1);
void setup() { }
lcd.print("ARITHMETIC GAME");
//initialize buzzer, LED1, LED2, }
LED3, and LED4 as output delay(3000);
if(flag == 1)
pinMode(buzzerPin, OUTPUT); lcd.clear();
{
pinMode(LED1, OUTPUT); y+=1;}
//read the state of the button
pinMode(LED2, OUTPUT); }// question 1! of buttons
pinMode(LED3, OUTPUT); b1State = digitalRead(button1);
pinMode(LED4, OUTPUT); else if (y == 1) b2State = digitalRead(button2);
b3State = digitalRead(button3); digitalWrite(LED1,LOW); flag = 1; //if so, flag is 1
//if the button1 press the first digitalWrite(LED2,LOW); digitalWrite(LED4,
HIGH);//turn the host LED on
if(b1State == 0) digitalWrite(LED3,HIGH);
delay(200);
{ y++;
}
flag = 0; while(digitalRead(button4));
}
digitalWrite(LED4, LOW); x++;
if(flag == 1)
digitalWrite(LED1, }
HIGH);//turn the LED1 on only {
}
digitalWrite(LED2,LOW); //read the state of the button
}
of buttons
digitalWrite(LED3,LOW);
// question2
b1State = digitalRead(button1);
y+=1;
else if (y == 2)
b2State = digitalRead(button2);
{
while(digitalRead(button4));//d b3State = digitalRead(button3);
etect the button4, if pressed, lcd.clear();
//if the button1 press the first
out of the while loop
lcd.setCursor(1,0);
if(b1State == 0)
}
lcd.print("2. 2 * 2 = ?");
{
//if the button2 press the first
lcd.setCursor(1,1);
flag = 0;
if(b2State == 0)
lcd.print("A.1 B.4 C.6");
digitalWrite(LED4, LOW);
{
//turn all the led off
digitalWrite(LED1,
flag = 0;
digitalWrite(LED1,LOW); HIGH);//turn the LED1 on only
digitalWrite(LED4,LOW);
digitalWrite(LED2,LOW); digitalWrite(LED2,LOW);
digitalWrite(LED1,LOW);
digitalWrite(LED3,LOW); digitalWrite(LED3,LOW);
digitalWrite(LED2,HIGH);
digitalWrite(LED4,LOW); y+=1;
digitalWrite(LED3,LOW);
//read the state of the button4
y++; while(digitalRead(button4));//d
etect the button4, if pressed,
while(digitalRead(button4));
out of the while loop
b4State = digitalRead(button4);
}
}
//when button4 (reset) pressed
//if the button3 press the first
//if the button2 press the first
if(b4State == 0)
if(b3State == 0)
if(b2State == 0)
{
{
{
if(b4State == 0)//confirm that
flag = 0; the button4 is pressed flag = 0;
digitalWrite(LED4,LOW); { digitalWrite(LED4,LOW);
digitalWrite(LED1,LOW); digitalWrite(LED1,LOW); digitalWrite(LED1,
HIGH);//turn the LED1 on only
digitalWrite(LED2,HIGH); digitalWrite(LED2,LOW);
digitalWrite(LED2,LOW);
digitalWrite(LED3,LOW); digitalWrite(LED3,LOW);
digitalWrite(LED3,LOW);
y++; digitalWrite(LED4,LOW);
y+=1;
while(digitalRead(button4)); //read the state of the button4
x++;
while(digitalRead(button4));//d
} b4State = digitalRead(button4); etect the button4, if pressed,
out of the while loop
//if the button3 press the first //when button4 (reset) pressed
}
if(b3State == 0) if(b4State == 0)
//if the button2 press the first
{ {
if(b2State == 0)
flag = 0; if(b4State == 0)//confirm that
the button4 is pressed {
digitalWrite(LED4,LOW);
{ flag = 0;
digitalWrite(LED1,LOW);
flag = 1; //if so, flag is 1 digitalWrite(LED4,LOW);
digitalWrite(LED2,LOW);
digitalWrite(LED4, digitalWrite(LED1,LOW);
digitalWrite(LED3,HIGH);
HIGH);//turn the host LED on
digitalWrite(LED2,HIGH);
y++;
delay(200);
digitalWrite(LED3,LOW);
while(digitalRead(button4));
}
y++;
x++;
}
while(digitalRead(button4));
}
if(flag == 1)
}
}
{
//if the button3 press the first
}
//read the state of the button
if(b3State == 0)
of buttons
{
// question3 b1State = digitalRead(button1);
flag = 0;
else if (y == 3) b2State = digitalRead(button2);
digitalWrite(LED4,LOW);
{ b3State = digitalRead(button3);
digitalWrite(LED1,LOW);
lcd.clear(); //if the button1 press the first
digitalWrite(LED2,LOW);
lcd.setCursor(1,0); if(b1State == 0)
digitalWrite(LED3,HIGH);
lcd.print("3. 3 * 2 = ?"); {
y+=1;
lcd.setCursor(1,1); flag = 0;
while(digitalRead(button4));
lcd.print("A.1 B.4 C.6"); digitalWrite(LED4, LOW);
x++;
//turn all the led off
} } y++;
} if(flag == 1) while(digitalRead(button4));
} { }
//read the state of the button //if the button3 press the first
of buttons
// question4 if(b3State == 0)
b1State = digitalRead(button1);
else if (y == 4) {
b2State = digitalRead(button2);
{ flag = 0;
b3State = digitalRead(button3);
lcd.clear(); digitalWrite(LED4,LOW);
//if the button1 press the first
lcd.setCursor(1,0); digitalWrite(LED1,LOW);
if(b1State == 0)
lcd.print("4. 3 + 5 = ?"); digitalWrite(LED2,LOW);
{
lcd.setCursor(1,1); digitalWrite(LED3,HIGH);
flag = 0;
lcd.print("A. 8 B.4 C.2"); y++;
digitalWrite(LED4, LOW);
//turn all the led off while(digitalRead(button4));
digitalWrite(LED1,
digitalWrite(LED1,LOW); }
HIGH);//turn the LED1 on only
digitalWrite(LED2,LOW); }
digitalWrite(LED2,LOW);
digitalWrite(LED3,LOW); }
digitalWrite(LED3,LOW);
digitalWrite(LED4,LOW);
y+=1;
//read the state of the button4 // question5
while(digitalRead(button4));//d else if (y == 5)
etect the button4, if pressed,
b4State = digitalRead(button4); out of the while loop {
//when button4 (reset) pressed x++; lcd.clear();
if(b4State == 0) } lcd.setCursor(1,0);
{ //if the button2 press the first lcd.print("5. 6 + 9 = ?");
if(b4State == 0)//confirm that if(b2State == 0) lcd.setCursor(1,1);
the button4 is pressed
{ lcd.print("A. 1 B.15 C.2");
{
flag = 0; //turn all the led off
flag = 1; //if so, flag is 1
digitalWrite(LED4,LOW); digitalWrite(LED1,LOW);
digitalWrite(LED4,
digitalWrite(LED1,LOW); digitalWrite(LED2,LOW);
HIGH);//turn the host LED on
digitalWrite(LED2,HIGH); digitalWrite(LED3,LOW);
delay(200);
digitalWrite(LED3,LOW); digitalWrite(LED4,LOW);
}
//read the state of the button4
lcd.setCursor(1,0);
while(digitalRead(button4));//d
b4State = digitalRead(button4); lcd.print("Your score is: ");
etect the button4, if pressed,
//when button4 (reset) pressed out of the while loop lcd.setCursor(1,1);
if(b4State == 0) } lcd.print(x);}
{ //if the button2 press the first b4State = digitalRead(button4);
if(b4State == 0)//confirm that if(b2State == 0) //when button4 (reset) pressed
the button4 is pressed
{ if(b4State == 0)
{
flag = 0; {
flag = 1; //if so, flag is 1
digitalWrite(LED4,LOW); if(b4State == 0)//confirm that
digitalWrite(LED4, the button4 is pressed
digitalWrite(LED1,LOW);
HIGH);//turn the host LED on
{
digitalWrite(LED2,HIGH);
delay(200);
flag = 1; //if so, flag is 1
digitalWrite(LED3,LOW);
}
digitalWrite(LED4,
y++;
} HIGH);//turn the host LED on
while(digitalRead(button4));
if(flag == 1) delay(200);
x++;
{ }
}
//read the state of the button }
of buttons //if the button3 press the first
if(flag == 1)
b1State = digitalRead(button1); if(b3State == 0)
{
b2State = digitalRead(button2); {
//read the state of the button
b3State = digitalRead(button3); flag = 0; of buttons
//if the button1 press the first digitalWrite(LED4,LOW); b1State = digitalRead(button1);
if(b1State == 0) digitalWrite(LED1,LOW); b2State = digitalRead(button2);
{ digitalWrite(LED2,LOW); b3State = digitalRead(button3);
flag = 0; digitalWrite(LED3,HIGH); //if the button1 press the first
digitalWrite(LED4, LOW); y++; if(b1State == 0)
digitalWrite(LED1, while(digitalRead(button4)); {
HIGH);//turn the LED1 on only
} flag = 0;
digitalWrite(LED2,LOW);
} digitalWrite(LED4, LOW);
digitalWrite(LED3,LOW);
} digitalWrite(LED1,
y+=1; HIGH);//turn the LED1 on only
else if (y == 6){
digitalWrite(LED2,LOW);
lcd.clear();
digitalWrite(LED3,LOW);
y+=1;
while(digitalRead(button4));//d
etect the button4, if pressed,
out of the while loop
x++;
else if (y == 7){
x=0;
y=0;}}