Arduino UNO 學習 (二) 搶答器、PWM調光、蜂鳴器、感光燈、三色燈、熱敏傳感器、傾斜傳感器、火焰傳感器


(一)、搶答器:

代碼

 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)關於硬件方面的使用還需要加強!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM