本帖最后由 阿斯顿发 于 2020-12-22 20:56 编辑
大概描述一下:
做一个摩斯电码的学习机,有三个模式:study,learn,exam。
现在问题是怎么通过按动按键实现三个模式之间的切换,是不管程序运行到哪都能切换模式,不是运行完才能切换模式
程序如下:int dianbao = 3;
int mode = 2;
int fengmingqi = 4;
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
//char array1[]=" Arduino123456789 "; //the string to print on the LCD
//char array2[]="lv zhen "; //the string to print on the LCD
// initialize the library with the numbers of the interface pins使用接口引脚的数字对库进行初始化
LiquidCrystal_I2C lcd(0x27,16,2); // set the LCD address to 0x270x3F for a 16 chars and 2 line display
//将 LCD 地址设置为 0x27 0x3F,用于 16 个字符和 2 行显示
void setup() {
lcd.init(); //initialize the lcd
lcd.backlight(); //open the backlight
pinMode(dianbao,INPUT_PULLUP);
pinMode(mode,INPUT);
pinMode(fengmingqi,OUTPUT);
attachInterrupt(0,bianhuan,CHANGE);
}
void loop() {
lcd.setCursor(0,0); // 将光标设置为第 0 列,第 0 行
lcd.print("MODE:STUDY ");
delay(1500);
lcd.clear();
delay(500);
//int a = analogRead(0);
//a = map(a,0,1023,1,26);
for(int q = -10000;q>10000;q++);
{
int a = random(1,26);
switch(a){
case 1 :
lcd.print("A ._");
s();l();
break;
case 2 :
lcd.print("B _...");
l();s();s();s();
break;
case 3 :
lcd.print("C _._.");
l();s();l();s();
break;
case 4:
lcd.print("D _..");
l();s();s();
break;
case 5:{
lcd.print("E .");
s();
break;}
/*case 6 :{
lcd.print("F .._. ");
break;}*/
case 7 :
lcd.print("G __. ");
l();l();s();
break;
case 8 :
lcd.print("H .... ");
s();s();s();s();
break;
case 9 :
lcd.print("I .. ");
s();s();
break;
case 10 :
lcd.print("J .___");
s();l();l();l();
break;
case 11:
lcd.print("K _._ ");
l();s();l();
break;
case 12:
lcd.print("L ._.. ");
s();l();s();s();
break;
case 13:
lcd.print("M __ ");
l();l();
break;
case 14:
lcd.print("N _. ");
l();s();
break;
case 15:
lcd.print("O ___ ");
l();l();l();
break;
case 16:
lcd.print(" .__. ");
s();l();l();s();
break;
case 17:
lcd.print("Q __._ ");
l();l();s();l();
break;
case 18:
lcd.print("R ._. ");
s();l();s();
break;
case 19:
lcd.print("S ... ");
s();s();s();
break;
case 20:
lcd.print("T _ ");
l();
break;
case 21:
lcd.print("U .._ ");
s();s();l();
break;
case 22:
lcd.print("V ..._ ");
s();s();s();l();
break;
case 23:
lcd.print("W .__ ");
s();l();l();
break;
case 24:
lcd.print("X _.._");
l();s();s();l();
break;
case 25:
lcd.print("Y _.__ ");
l();s();l();l();
break;
case 26:
lcd.print("Z __.. ");
l();l();s();s();
break;
}
if(a==6){
lcd.print("F .._. ");
s();s();l();s();
}
}
delay(2000);
}
void bianhuan(){
lcd.clear();
delay(1000);
attachInterrupt(0,bianhuan1,RISING);
lcd.setCursor(0,0); // set the cursor to column 15, line 0将光标设置为第 15 列,第 0 行
lcd.print("MODEEARN");
delay(1500);
lcd.clear();
delay(500);
int a = analogRead(0);
a = map(a,0,1023,1,26);
}
void bianhuan1(){
lcd.clear();
delay(1000);
attachInterrupt(0,bianhuan2,RISING);
char array1[]="MODE:EXAM";
lcd.setCursor(0,0); // set the cursor to column 15, line 0将光标设置为第 15 列,第 0 行
lcd.print(array1);
delay(1500);
lcd.clear();
}
void bianhuan2(){
detachInterrupt(0);
}
void l(){
digitalWrite(fengmingqi,HIGH);
delay(270);
digitalWrite(fengmingqi,LOW);
delay(100);
}
void s(){
digitalWrite(fengmingqi,HIGH);
delay(100);
digitalWrite(fengmingqi,LOW);
delay(100);
}
|