代码改变世界

Arduino光敏+LED+数码管+蜂鸣器综合实验

2022-01-12 00:03  jym蒟蒻  阅读(436)  评论(0编辑  收藏  举报

内容


用光敏控制LED的明暗程度,光敏值在3位数码管上显示,串口输出光敏值,蜂鸣器到一定的值会didi

话不多说,直接上代码,那几个部分拼到一起,多了个数的拆分,让k1k2k3分别等于百位十位个位

 

代码

void setup(){
Serial.begin(9600);
int ledpin=9;
for(int i=0;i<7;++i){
    pinMode(i+2,OUTPUT);
  }
  pinMode(12,OUTPUT);
  pinMode(13,OUTPUT);
  pinMode(11,OUTPUT);
}
int a2g[10][9]={
 {0,0,0,0,0,0,1},  //0
    {1,0,0,1,1,1,1},  //1
    {0,0,1,0,0,1,0},  //2
    {0,0,0,0,1,1,0},  //3
    {1,0,0,1,1,0,0},  //4
    {0,1,0,0,1,0,0},  //5
    {0,1,0,0,0,0,0},  //6
    {0,0,0,1,1,1,1},  //7
    {0,0,0,0,0,0,0},  //8
    {0,0,0,0,1,0,0},  //9
};
void loop(){
  
  int sv=analogRead(A0);
  Serial.println(sv);
  analogWrite(9,255*sv/300);
  
  if(sv<60){
  tone(10,40);
  }
  else {
  noTone(10);
  } 

  int a=sv;
  int i=0;
  while(a>0){

  i=i+1;  
  a=a/10;
  }   
  int k1,k2,k3;
  if(i==2) 
  {
    k3=0;
    for(int j=0;j<i;++j){
      if(j==0) {
        k1=sv%10;
        }
        else if(j==1){
        k2=sv%10;
       }
       sv=sv/10;
    }
  }
  else if(i==3){
    for(int j=0;j<i;++j){
      if(j==0) {
      k1=sv%10;
      }
      else if(j==1){
      k2=sv%10;
      }
      else {
      k3=sv%10;
      }
      sv=sv/10;
    }
  }
  for(int j=0;j<30;++j){
  
  digitalWrite(13,1);
  digitalWrite(12,0);
  digitalWrite(11,0);
  for(int i=0;i<7;++i){
       digitalWrite(i+2,a2g[k1][i]);
   }
   delay(10);
  digitalWrite(13,0);
  digitalWrite(12,1);
  digitalWrite(11,0);
  for(int i=0;i<7;++i){
       digitalWrite(i+2,a2g[k2][i]);
   }
   delay(10);
   digitalWrite(13,0);
  digitalWrite(12,0);
  digitalWrite(11,1);
  for(int i=0;i<7;++i){
       digitalWrite(i+2,a2g[k3][i]);
   }
   delay(10);
  }
  //delay(100);
}

 

 

实物图