/**
Arduino Electronic Safe
Copyright (C) 2020, Uri Shaked.
Released under the MIT License.
*/
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
const int greenLEDPin =2; //绿LED亮,表示结果正确
const int redLEDPin =3; //红LED亮,表示结果错误
int firstNum ; //加法或减法操作的第一个数
int secondNum ; //加法或减法操作的第二个数
int result ; //两个数的运算结果
int myResult; //我计算的运算结果
char operatr = ' '; //存放‘+’或‘-’的字符
int start_flag = 0 ; //开始运算标志位
int rightNum= 0; //记录正确数
int errorNum= 0; //记录错误数
int counts = 0; //记录答题次数
long startTime; //开始记录答题时间
long endTime; //结束记录答题时间
/*
* 下面的配置是对4*4键盘的配置
*/
const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
char keys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte rowPins[ROWS] = {5, 6, 7, 8}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {9, 10, 11, 12}; //connect to the column pinouts of the keypad
Keypad myKeypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS);
//液晶屏的配置
LiquidCrystal_I2C lcd(0x27,16,2); //创建对象,并配置LCD地址及行列
void startDisplay(){
lcd.clear(); //清屏
lcd.setCursor(0, 0) ; //设置光标位置为第1行第1个位置
lcd.print("Welcome!"); //使屏幕显示Welcome!
lcd.setCursor(0, 1) ; //设置光标位置为第2行第1个位置
lcd.print("Press A to start");
delay(500);
}
void setup() {
pinMode(greenLEDPin,OUTPUT);
pinMode(redLEDPin,OUTPUT);
lcd.init(); //初始化LCD
lcd.backlight(); //打开背光
delay(500); //延时500ms
startDisplay();
randomSeed(analogRead(A0));
}
void loop() {
char newKey = myKeypad.getKey();
if(newKey != NO_KEY && newKey == 'A'){
start_flag=1;
startTime=millis();
}
if(start_flag==1 && counts <= 10){ //开始运算
counts++;
gennerator(); //生成数据并赋值给相关变量
lcd.clear(); //清屏
lcd.setCursor(0, 0); //设置光标位置为第1行第1个位置
lcd.print(firstNum); //显示第一个数字
lcd.print(operatr); //显示运算符
lcd.print(secondNum); //显示第二个数字
lcd.print("="); //显示 =
while(1){
char newKey = myKeypad.getKey();
//把输入的数值显示出来,并把最后的输入结果赋给myResult
for(int i=0;i<=9;i++){
if(newKey != NO_KEY && newKey ==('0'+i)){
int num=newKey-48; //把字符数字变为数学中的数字
lcd.print(num); //使屏幕显示文字
delay(100);
myResult=myResult*10+num;
}
}
if(newKey != NO_KEY && newKey == 'D'){
break;
}
}
lcd.setCursor(0, 1);
if(result==myResult){
rightNum++;
digitalWrite(greenLEDPin,HIGH);
lcd.print(" great^-^");
}else{
errorNum++;
digitalWrite(redLEDPin,HIGH);
lcd.print(" losing-_-");
}
delay(2000);
digitalWrite(greenLEDPin,LOW);
digitalWrite(redLEDPin,LOW);
}
//结束本轮答题
endToHandle();
}
void endToHandle(){
if(start_flag==1&& counts == 10){
lcd.clear(); //清屏
lcd.setCursor(0, 0);
lcd.print("right:");
lcd.print(rightNum);
lcd.setCursor(0, 1);
lcd.print("error:");
lcd.print(errorNum);
delay(3000);
lcd.clear(); //清屏
lcd.setCursor(0, 0);
if(rightNum==10){
lcd.print(" best!!");
}else if(rightNum>=8){
lcd.print(" great!!");
}else if(rightNum>=6){
lcd.print(" passed!!");
}else{
lcd.print("need work hard!");
}
delay(3000);
endTime=millis();
int fen=(endTime-startTime)/1000/60;
int miao=(endTime-startTime)/1000%60;
lcd.clear(); //清屏
lcd.setCursor(0, 0);
lcd.print("Time:");
lcd.print(fen);
lcd.print("\'");
lcd.print(miao);
lcd.print("\"");
delay(5000);
start_flag=0;
counts=0;
lcd.clear(); //清屏
lcd.setCursor(0, 0);
lcd.print("Press A to start");
}
}
/*
* 通过生成的随机数给firstNum、secondNum、result、operatr赋值,myResult重新赋值为0
*/
void gennerator(){
int x=random(99)+1; //生成一个1-99的数字
int y=random(99)+1;
int z=random(2); //生成一个0或1的数字,0代表加法,1代表减法
if(z==0){
operatr = '+';
if(x>y){
result=x; //最大的数存放结果
firstNum=y ;
secondNum=x-y ;
}else{
result=y;
firstNum=x;
secondNum=y-x ;
}
}else {
operatr = '-';
if(x>y){
result=x-y;
firstNum=x;
secondNum=y;
}else{
result=y-x;
firstNum=y;
secondNum=x ;
}
}
myResult=0;
}