1、数字输入
利用四脚摁键开关的原理控制
摁下即联通引脚2接地,为低电平;不摁即高电平。利用其原理控制连接着LED的引脚13,从而实现led的亮灭。
原理如下图注释
利用Serial.begin()和Serial.println()(Serial.print()也可以打开内容不过不能换行)可以在窗口监视器看到引脚2的数字输入的变化,利用其可以便于观察以后编写代码中各种变量的变化
if...else...控制结构则实现了开关对13引脚高低电平变化的控制,该结构还可以嵌套使用,可以实现多重控制。
if( 表达式1 ) {
语句块1
WeChat_20240421101819
} else {
语句块2
}
(if()括号里面的为逻辑表达式,成立则为1,即执行语句块1;为0则else执行语句块2)
2、根据上诉所学函数和控制结构,可以控制两个开关
Boolean(可简写为bool)为布尔类型,bool有0和1两种类型,0为假,1为真,若有其它数字(出0外)赋值给布尔变量类型,布尔变量都将读取为1。
!为布尔运算符的逻辑非,若pushButton=8;则!pushButton=1;若pushButton=1;则!pushButton = 8;
&&为布尔运算符的逻辑与,两边条件都为真(1)才成立使整条式子都为1
“||”为布尔运算符的逻辑或,两条式子任意一个条件成立,即整条式子成立为真
上述代码需要同时摁下两个开关LED才会亮
上述代码只需随便摁任意一个开关LED都会亮(包括同时摁)
WeChat_20240421102032
WeChat_20240421102017
3、制作MC猜数字是个循序渐进的过程
利用while循环,swtich...case结构,内置random,randomSeed,delay等函数以及自己编写的函数来制作这个随机数字生成。
void setup() {
pinMode(2,INPUT_PULLUP);//利于开关控制摁键
Serial.begin(9600);
int pinNumber = 3;//初始赋值3给变量pinNumber
while(pinNumber <=9){
pinMode(pinNumber,OUTPUT);
pinNumber = pinNumber + 1;
}
randomSeed(analogRead(A0));
//读入A0接口的模拟输入
//利用while循环,提高效率
}
void loop() {
int myNumber=0;
if(!digitalRead(2)){
myNumber = getRandomNumber(0,10);
//随机产生0-9的整数
displayNumber(myNumber);
}
}
int getRandomNumber(int minNumber, int maxNumber){//定义一个函数getRandomNumber;
int randomNumber;
int i = 0;//给变量i赋个初始值0,不然i=i+1无法运行;
while(i<5){
i=i+1;
randomNumber = random(minNumber,maxNumber);
displayNumber(randomNumber);
delay(100);
displayClear();
delay(100);
Serial.print("i=");
Serial.println(i);
Serial.print("randomNumber=");
Serial.println(randomNumber);
Serial.println("");//便于在串口显示器观察变量的变化情况;
}
return randomNumber;//只有return的时候就只返回0
}
void displayClear(){
digitalWrite(3,LOW);
digitalWrite(4,LOW);
digitalWrite(5,LOW);
digitalWrite(6,LOW);
digitalWrite(7,LOW);
digitalWrite(8,LOW);
digitalWrite(9,LOW);
}
//编写一个清除的函数,将显示一次后的引脚设为低电平,使其熄屏,不会与下一次循环的重叠在一起
void displayNumber(int ledNumber){//一定要给参数定类型,参数之间用逗号隔开
switch(ledNumber){//名称要统一
case 0:
digitalWrite(3,HIGH);
digitalWrite(4,HIGH);
digitalWrite(6,HIGH);
digitalWrite(7,HIGH);
digitalWrite(8,HIGH);
digitalWrite(9,HIGH);
break;
case 1:
digitalWrite(4,HIGH);
digitalWrite(7,HIGH);
//1
break;
case 2:
digitalWrite(3,HIGH);
digitalWrite(4,HIGH);
digitalWrite(5,HIGH);
digitalWrite(8,HIGH);
digitalWrite(9,HIGH);
//2
break;
case 3:
digitalWrite(3,HIGH);
digitalWrite(4,HIGH);
digitalWrite(5,HIGH);
digitalWrite(7,HIGH);
digitalWrite(8,HIGH);
//3
break;
case 4:
digitalWrite(4,HIGH);
digitalWrite(5,HIGH);
digitalWrite(6,HIGH);
digitalWrite(7,HIGH);
break;
case 5:
digitalWrite(3,HIGH);
digitalWrite(5,HIGH);
digitalWrite(6,HIGH);
digitalWrite(7,HIGH);
digitalWrite(8,HIGH);
break;
case 6:
digitalWrite(3,HIGH);
digitalWrite(5,HIGH);
digitalWrite(6,HIGH);
digitalWrite(7,HIGH);
digitalWrite(8,HIGH);
digitalWrite(9,HIGH);
break;
case 7:
digitalWrite(3,HIGH);
digitalWrite(4,HIGH);
digitalWrite(7,HIGH);
break;
case 8:
digitalWrite(3,HIGH);
digitalWrite(4,HIGH);
digitalWrite(5,HIGH);
digitalWrite(6,HIGH);
digitalWrite(7,HIGH);
digitalWrite(8,HIGH);
digitalWrite(9,HIGH);
break;
case 9:
digitalWrite(3,HIGH);
digitalWrite(4,HIGH);
digitalWrite(5,HIGH);
digitalWrite(6,HIGH);
digitalWrite(7,HIGH);
digitalWrite(8,HIGH);
break;
default:
digitalWrite(4,HIGH);
digitalWrite(5,HIGH);
digitalWrite(7,HIGH);
digitalWrite(8,HIGH);
digitalWrite(9,HIGH);
break;
}
}
WeChat_20240421111135
我是根据上图来编写displayNumber函数的(此数码管为共阴极数码管),连接电路也根据上图来连接(内部有七个二极管)