5.14菜鸟日志

该文介绍了两个Arduino实验,一是使用布尔变量控制LED灯,基于digitalRead检测按键状态,二是数码管显示和模拟输入实验,利用analogWrite实现PWM控制LED亮度。实验过程中强调了数码管连接的注意事项,以及模拟输入时PWM技术的应用。
摘要由CSDN通过智能技术生成

 布尔类型的变量的小运算实验

​
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是通过定时器来实现的。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值