码迷,mamicode.com
首页 > 其他好文 > 详细

Arduino UNO 学习 (二) 抢答器、PWM调光、蜂鸣器、感光灯、三色灯、热敏传感器、倾斜传感器、火焰传感器

时间:2020-04-23 23:13:30      阅读:99      评论:0      收藏:0      [点我收藏+]

标签:print   width   can   符号   idt   run   make   delay   pre   

(一)、抢答器:

代码

 1 int redled = 8;
 2 int yellowled = 7;
 3 int greenled = 6;
 4 
 5 int redpin = 5;
 6 int yellowpin = 4;
 7 int greenpin = 3;
 8 
 9 int restpin = 2;
10 int red,yellow,green;
11 
12 void setup() {
13   // put your setup code here, to run once:
14   pinMode(redled, OUTPUT);
15   pinMode(yellowled, OUTPUT);
16   pinMode(greenled, OUTPUT);
17   pinMode(redpin, INPUT);
18   pinMode(yellowpin, INPUT);
19   pinMode(greenpin, INPUT);
20 }
21 
22 void loop() {
23   // put your main code here, to run repeatedly:
24   red = digitalRead(redpin);
25   yellow = digitalRead(yellowpin);
26   green = digitalRead(greenpin);
27   
28   if (red == LOW) { RED_YES(); }
29   if (yellow == LOW) { YELLOW_YES(); }
30   if (green == LOW) { GREEN_YES(); }
31 }
32 
33 void RED_YES(){
34   while(digitalRead(restpin) == 1){
35     digitalWrite(redled, HIGH);
36     digitalWrite(greenled, LOW);
37     digitalWrite(yellowled, LOW);
38   }
39   CLEAR_LED();
40 }
41 
42 void YELLOW_YES(){
43   while(digitalRead(restpin) == 1){
44     digitalWrite(redled, LOW);
45     digitalWrite(greenled, LOW);
46     digitalWrite(yellowled, HIGH);
47   }
48   CLEAR_LED();
49 }
50 
51 void GREEN_YES(){
52   while(digitalRead(restpin) == 1){
53     digitalWrite(redled, LOW);
54     digitalWrite(greenled, HIGH);
55     digitalWrite(yellowled, LOW);
56   }
57   CLEAR_LED();
58 }
59 
60 void CLEAR_LED(){
61   digitalWrite(redled, LOW);
62   digitalWrite(greenled, LOW);
63   digitalWrite(yellowled, LOW);
64   }

技术图片

 

(二)、PWM调光

代码

int potpin = 0;
int ledpin = 11;
int val = 0 ;

void setup() {
  // put your setup code here, to run once:
  pinMode(ledpin, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
  val = analogRead(potpin);
  Serial.println(val);
  analogWrite(ledpin, val/4);
  delay(10);
}

技术图片

 

(三)、蜂鸣器

代码

int buzzer = 8;
// buzzer break, can not make noise
void setup() {
  // put your setup code here, to run once:
  pinMode(buzzer, OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
  long frequency = 300;
  tone(buzzer, frequency);
  delay(100);

  noTone(buzzer);
  delay(2000);
//  while(1){
//    for(int i; i < 80; i++){
//      digitalWrite(buzzer, HIGH);
//      delay(1);
//      digitalWrite(buzzer, LOW);
//      delay(1);
//      }
//    for(int i; i < 100; i++){
//      digitalWrite(buzzer, HIGH);
//      delay(2);
//      digitalWrite(buzzer, LOW);
//      delay(2);
//      }
//    }
}

技术图片

 

(四)、感光灯

代码

int potpin = 0;
int ledpin = 11;
int val;

void setup() {
  // put your setup code here, to run once:
  pinMode(potpin, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
  val = analogRead(potpin);
  Serial.println(val);
  analogWrite(ledpin, val);
  delay(10);
}

技术图片

 

(五)、三色灯

代码

int redpin = 11;
int bluepin = 10;
int greenpin = 9;

int redval, blueval, greenval;
int i = 0;

void setup() {
  // put your setup code here, to run once:
  pinMode(redpin, OUTPUT);
  pinMode(bluepin, OUTPUT);
  pinMode(greenpin, OUTPUT);
  pinMode(redval, INPUT);
  pinMode(blueval, INPUT);
  pinMode(greenval, INPUT);
  Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
  i++;
  if(i < 200){
    redval += 1;
    greenval -= 1;
    blueval = 1;
    }
  else if(i < 400){
    redval -= 1;
    greenval = 1;
    blueval += 1;
    }
  else if(i < 600){
      redval = 1;
      greenval += 1;
      blueval -= 1;
      }
  else { i = 0; }

  analogWrite(redpin, redval);
  analogWrite(greenpin, greenval);
  analogWrite(bluepin, blueval);

  Serial.print(i, DEC);
  Serial.print( "   R:");
  Serial.print(redval, DEC);

  Serial.print( "   B:");
  Serial.print(blueval, DEC);

  Serial.print( "   G:");
  Serial.print(greenval, DEC);
}

技术图片

 

(六)、热敏传感器

代码

int sensorInPin = 0;
int ledpin = 8;
int sensorVal = 0;

void setup() {
  // put your setup code here, to run once:
  pinMode(ledpin, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
  sensorVal = analogRead(sensorInPin);
  Serial.print(sensorVal);
  analogWrite(ledpin, sensorVal);
  delay(5000);
}

技术图片

 

(七)、倾斜传感器

代码

int sensorInPin = A5;
int ledpin = 8;

void setup() {
  // put your setup code here, to run once:
  pinMode(ledpin, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
  int i;
  while(1){
    i = analogRead(sensorInPin);
    Serial.print(i);
    if(i > 1000){
      digitalWrite(ledpin, HIGH);
      }
    else { digitalWrite(ledpin, LOW);}
    }
}

技术图片

 

(八)、火焰传感器

代码

int fireInPin = A5;
int beep = 8;
int val = 0;

void setup() {
  // put your setup code here, to run once:
  pinMode(beep, OUTPUT);
  //pinMode(fireInPin, INPUT);
  Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
  //while(1){
    val = analogRead(fireInPin);
    Serial.print(val);
    Serial.print("\n");
    if(val > 200){
      digitalWrite(beep, HIGH);
      }
    else { digitalWrite(beep, LOW); }
   // }
}

忘记拍照了!

 

(九)、声音传感器

代码

int soundInPin = 2;
int ledpin = 13;
int val;

void setup() {
  // put your setup code here, to run once:
  pinMode(ledpin, OUTPUT);
  pinMode(soundInPin, INPUT);
  Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
  val = analogRead(soundInPin);
  Serial.println(val);
  delay(2000);

  if(val == HIGH){
    digitalWrite(ledpin, HIGH);
    delay(5000);
    digitalWrite(ledpin, LOW);
    }
}

忘记拍照了!

 

总结:

(1)虽说面包板+加跳线更加灵活,但是也容易接触不牢的问题。

(2)蜂鸣器中要使用Tone()函数,在无源蜂鸣器的应用中。

(3)Serial.begin(9600),未开启串口函数,9600为波特率(bps, byte per second),波特率表示每秒钟传送的码元符号的个数,

是衡量数据传送速率的指标,它用单位时间内载波调制状态改变的次数来表示;9600的波特率的信道,

理论上每秒可以传输9600个二进制位,也就是9600/8个英文字母的数据量,也就是1200个字节,大约1.2KB。

而19200则是每秒可传输2400字节,大约2.4KB。

(4)Serial.print(a, DEC),为输出十进制数b。Serial函数的用法参考https://blog.csdn.net/hhaowang/article/details/88529842

(5)火焰传感器并不是通过火焰的温度来实现的,而是通过亮度来实现的。

(6)声音传感器中需要50k欧的电阻,这里没有就没有实现。

(7)是否是长时间使用UNO,会出现设备不稳定的情况?,再没有外接输入源的时候,模拟(或数字)信号端,有信号的变化,

最大到1023!不知道怎么回事!

(8)关于硬件方面的使用还需要加强!

Arduino UNO 学习 (二) 抢答器、PWM调光、蜂鸣器、感光灯、三色灯、热敏传感器、倾斜传感器、火焰传感器

标签:print   width   can   符号   idt   run   make   delay   pre   

原文地址:https://www.cnblogs.com/heze/p/12763971.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!