布尔类型的变量的小运算实验
bool pushButton1;
bool pushButton2;
void setup()
{
pinMode(2,INPUT_PULLUP);
pinMode(8,INPUT_PULLUP);
pinMode(13,OUTPUT);
}
void loop()
{
pushButton1 = digitalRead(2);
pushButton2 = digitalRead(8);
if(!pushButton1 && !pushButton2)
digitalWrite(13,HIGH);
else
digitalWrite(13,LOW);
}
数码管实验
因用杜邦线代替面包板跳线,故上图的连线十分杂乱。
总结一下该实验:1、单位的数码管连结还算是简单的,需要细心;
2、注意上拉模式下的按键开关、2号引脚与GND的连接方式;
3、一开始电阻连接错误,后面根据实物连线画出电路图很容易就能找出错误的地方;
4、看错面包板上的红蓝线2次;
void setup() {
pinMode(2, INPUT_PULLUP);
for (int i = 2; i <= 9; i++) {
pinMode(i, OUTPUT);
}
}
// the loop function runs over and over again forever
void loop() {
int myNumber = 0;
while(myNumber <= 9){
displayNumber(myNumber);
delay(500);
displayClear();
myNumber++;
while(!digitalRead(2)){
digitalWrite(5, HIGH);
digitalWrite(6, HIGH);
digitalWrite(7, HIGH);
digitalWrite(8, HIGH);
digitalWrite(9, HIGH);
}
delay(500);
}
}
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 number){
switch(number){
case 1:
digitalWrite(4, HIGH);
digitalWrite(7, HIGH);
break;
case 2:
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
digitalWrite(5, HIGH);
digitalWrite(8, HIGH);
digitalWrite(9, HIGH);
break;
case 3:
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
digitalWrite(5, HIGH);
digitalWrite(7, HIGH);
digitalWrite(8, HIGH);
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;
case 0:
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
digitalWrite(6, HIGH);
digitalWrite(7, HIGH);
digitalWrite(8, HIGH);
digitalWrite(9, HIGH);
break;
default:
digitalWrite(5, HIGH);
}
}
模拟输入实验
analogWrite是Arduino编程语言中一个重要的函数。通过控制数字引脚的输出电压,从而实现对模拟电路的控制。
bool pushButton1;
bool pushButton2;
int ledPin = 9;
int brightness = 128;
void setup()
{
pinMode(2,INPUT_PULLUP);
pinMode(8,INPUT_PULLUP);
pinMode(ledPin,OUTPUT);
Serial.begin(9600);
}
void loop()
{
pushButton1 = digitalRead(2);
pushButton2 = digitalRead(8);
if(!pushButton1 && brightness > 0)
brightness--;
else if(!pushButton2 && brightness < 255)
brightness++;
analogWrite(ledPin,brightness);
Serial.println(brightness);
delay(10);
}
analogWrite函数的基本原理是PWM技术。
PWM的中文名称是脉冲宽度调制技术,其基本思路是将模拟信号转换为数字信号,具体是通过改变数字的占空比来模拟连续变化的信号。
在Arduino中,PWM是通过定时器来实现的。