# 超級電子元件參考程式碼 ## 超級電子元件 ### ex1 :::spoiler **提示** ```cpp #include <Servo.h> Servo myservo; // 建立一個 servo #define photocellPin A2 //定義光敏電阻 (photocell) 腳位 int photocellVal = 0; // photocell variable int minLight = 100; // 最小光線門檻值 int pos = 0; // 設定 Servo 角度的變數 int reset = 0; int flag = 0 ; // 判斷順時轉(0)、逆時轉(1)的旗幟 void setup() { Serial.begin(9600); /*stu code*/ // 將 servo 物件連接到 pin 9 /*stu code*/ // reset 至 0 度 } void loop() { Serial.println(/*photocellVal*/pos); // 可監控亮度值or角度 photocellVal = /*stu code*/ // 讀取亮度值 if(/*stu code*/){ if(flag == 0){ pos++; /*stu code*/ // 告訴 servo 走到 'pos' 的位置 /*stu code*/ // 等待 5ms 讓 servo 走到指定位置 if(/*stu code*/) // 角度++到180度時,改逆時針轉 /*stu code*/ } if(flag == 1){ pos--; /*stu code*/ // 告訴 servo 走到 'pos' 的位置 /*stu code*/ // 等待 5ms 讓 servo 走到指定位置 if(/*stu code*/) // 角度--到0度時,改順時針轉 /*stu code*/ } } } ``` ::: <!-- :::spoiler **解答** ```cpp #include <Servo.h> Servo myservo; // 建立一個 servo #define photocellPin A2 //定義光敏電阻 (photocell) 腳位 int photocellVal = 0; // photocell variable int minLight = 100; // 最小光線門檻值 int pos = 0; // 設定 Servo 角度的變數 int reset = 0; int flag = 0 ; // 判斷順時轉(0)、逆時轉(1)的旗幟 void setup() { Serial.begin(9600); myservo.attach(9); // 將 servo 物件連接到 pin 9 myservo.write(reset); // reset 至 0 度 } void loop() { Serial.println(/*photocellVal*/pos); // 可監控亮度值or角度 photocellVal = analogRead(photocellPin); // 讀取亮度值 if(photocellVal < minLight){ if(flag == 0){ pos++; myservo.write(pos); // 告訴 servo 走到 'pos' 的位置 delay(5); // 等待 5ms 讓 servo 走到指定位置 if(pos == 180) // 角度++到180度時,改逆時針轉 flag = 1 ; } if(flag == 1){ pos--; myservo.write(pos); // 告訴 servo 走到 'pos' 的位置 delay(5); // 等待 5ms 讓 servo 走到指定位置 if(pos == 0) // 角度--到0度時,改順時針轉 flag = 0 ; } } } ``` ::: --> ### ex2 :::spoiler **提示** ```cpp #define LED 6 #define buzzer 5 #define threshold 10// 容許誤差值(error) int r = random(0, 255); // 0-255間取一數字 void setup() { /*stu code*/ // 定義LED腳位 /*stu code*/ // 定義buzzer 腳位 } void loop() { int pwm = /*stu code*/ // 讀取A0腳位的值(範圍0-1023),pwm(範圍0-255) if (abs(/*stu code*/) /*stu code*/ /*stu code*/){ // abs取絕對值 /*stu code*/ // buzzer發聲 /*stu code*/ // 持續2秒 /*stu code*/ // 重取隨機數 } else{ /*stu code*/; // buzzer不發聲 } /*stu code*/; // 越靠近隨機數Led越亮 } ``` ::: <!-- :::spoiler **解答** ```cpp #define LED 6 #define buzzer 5 #define threshold 10// 容許誤差值(error) int r = random(0, 255); // 0-255間取一數字 void setup() { pinMode(LED,OUTPUT); // 定義LED腳位 pinMode(buzzer,OUTPUT); // 定義buzzer 腳位 } void loop() { int pwm = analogRead(A0)/4; // 讀取A0腳位的值(範圍0-1023),pwm(範圍0-255) if (abs(pwm-r) < threshold){ // abs取絕對值 tone(buzzer,523); // buzzer發聲 delay(2000); // 持續2秒 r = random(0, 255); // 重取隨機數 } else{ noTone(buzzer); // buzzer不發聲 } analogWrite(LED,255-abs(r-pwm)); // 越靠近隨機數Led越亮 } ``` ::: --> ### ex3 :::spoiler **提示** ```cpp #define BUTTON1_PIN 7 // 按鈕1的接腳 #define BUTTON2_PIN 8 // 按鈕2的接腳 #define buzzer 3 // buzzer的接腳 int buttonState1 = 0; // 按鈕1的狀態 int buttonState2 = 0; // 按鈕2的狀態 #define Do 523 // Do頻率 #define Re 587 // Re頻率 /*#define Mi 659 #define Fa 698 #define So 784 #define La 880 #define Si 988*/ void setup() { /*stu code*/ //設定buzzer的PIN腳 /*stu code*/ //設定按鈕1的接腳 /*stu code*/ //設定按鈕2的接腳 } void loop() { buttonState1 = /*stu code*/ //讀取按鍵1的狀態 buttonState2 = /*stu code*/ //讀取按鍵2的狀態 if(/*stu code*/){ //如果按鍵1按了 /*stu code*/ //發出Do音 } else if(/*stu code*/){ //如果按鍵2按了 /*stu code*/ //發出Re音 } else{ //如果按鍵是未按下 /*stu code*/ //不發聲 } } ``` ::: <!-- :::spoiler **解答** ```cpp #define BUTTON1_PIN 7 // 按鈕1的接腳 #define BUTTON2_PIN 8 // 按鈕2的接腳 #define buzzer 3 // buzzer的接腳 #define Do 523 // Do頻率 #define Re 587 // Re頻率 /*#define Mi 659 #define Fa 698 #define So 784 #define La 880 #define Si 988*/ int buttonState1 = 0; // 按鈕1的狀態 int buttonState2 = 0; // 按鈕2的狀態 //////////////////////////////////////// #include <LedControl.h> #define DIN 12 // din的接腳 #define CLK 11 // clk的接腳 #define CS 10 // cs的接腳 LedControl LC = LedControl(DIN,CLK,CS,1); void Update(int Map[8][8]) { for(int i=0;i<8;i++) { for(int j=0;j<8;j++) { LC.setLed(0,i,j,Map[i][j]); //更新亮暗 } } } int Doled[8][8]={{0,0,0,0,0,0,0,0}, // 顯示圖形 {1,1,0,0,0,0,0,0}, {1,0,1,0,0,0,0,0}, {1,0,0,1,0,0,0,0}, {1,0,0,1,0,1,1,1}, {1,0,0,1,0,1,0,1}, {1,0,0,1,0,1,0,1}, {1,1,1,0,0,1,1,1}}; int Reled[8][8]={{0,0,0,0,0,0,0,0}, // 顯示圖形 {1,1,1,0,0,0,0,0}, {1,0,0,1,0,0,0,0}, {1,0,0,1,0,1,1,1}, {1,0,1,0,0,1,0,1}, {1,1,0,0,0,1,1,1}, {1,0,1,0,0,1,0,0}, {1,0,0,1,0,1,1,1}}; //////////////////////////////////////////////////// void setup() { pinMode(buzzer, OUTPUT); //設定buzzer的PIN腳 pinMode(BUTTON1_PIN, INPUT); //設定按鈕1的接腳 pinMode(BUTTON2_PIN, INPUT); //設定按鈕2的接腳 pinMode(DIN,OUTPUT); //設定CIN的PIN腳 pinMode(CS,OUTPUT); //設定CS的PIN腳 pinMode(CLK,OUTPUT); //設定CLK的PIN腳 LC.shutdown(0,false); //供不供電 LC.setIntensity(0,8); //設定亮暗 (0~15) LC.clearDisplay(0); //清除顯示 } //////////////////////////////////////////////////////// void loop() { buttonState1 = digitalRead(BUTTON1_PIN); //讀取按鍵1的狀態 buttonState2 = digitalRead(BUTTON2_PIN); //讀取按鍵2的狀態 if(buttonState1 == HIGH){ //如果按鍵1按了 tone(buzzer,Do); //發出Do音 Update(Doled); //顯示Do } else if(buttonState2 == HIGH){ //如果按鍵2按了 tone(buzzer,Re); //發出Re音 Update(Reled); //顯示Re } else{ //如果按鍵是未按下 noTone(buzzer); //不發聲 LC.clearDisplay(0); //清除顯示 } } ``` ::: --> ### ex4 :::spoiler **提示** ```cpp #include<LiquidCrystal_I2C.h> #include <Wire.h> #include <DHT.h> #define pin 2 //定義DHT data pin角 #define DHTTYPE DHT11 LiquidCrystal_I2C lcd(0x27,16,2); //宣告LCD物件 DHT dht2 (pin,DHTTYPE); //宣告DHT物件 void setup() { /*stu code*/ //初始化LCD /*stu code*/ //開啟背光 /*stu code*/ //顯示I AM DETECTOR /*stu code*/ //延遲一秒 dht2.begin(); } void loop() { /*stu code*/ //清除顯示的文字 lcd.setCursor(/*stu code*/);// (column,row) /*stu code*/("Tem:"); //顯示Tem lcd.setCursor(/*stu code*/);// (column,row) /*stu code*/ //顯示溫度值 lcd.setCursor(/*stu code*/);// (column,row) /*stu code*/("Hum:"); //顯示Hum lcd.setCursor(/*stu code*/);// (column,row) /*stu code*/ //顯示溫度值 /*stu code*/ //延遲一秒 } ``` ::: <!-- :::spoiler **解答** ```cpp #include<LiquidCrystal_I2C.h> #include <Wire.h> #include <DHT.h> #define pin 2 //定義DHT data pin角 #define DHTTYPE DHT11 LiquidCrystal_I2C lcd(0x27,16,2); //宣告LCD物件 DHT dht2 (pin,DHTTYPE); //宣告DHT物件 void setup() { lcd.init(); //初始化LCD lcd.backlight(); //開啟背光 lcd.print("I AM DETECTOR"); //顯示I AM DETECTOR delay(1000); //延遲一秒 dht2.begin(); } void loop() { lcd.clear(); //清除顯示的文字 lcd.setCursor(0, 0);// (column,row) lcd.print("Tem:"); //顯示Tem lcd.setCursor(4, 0);// (column,row) lcd.print(dht2.readTemperature()); //顯示溫度值 lcd.setCursor(0, 1);// (column,row) lcd.print("Hum:"); //顯示Hum lcd.setCursor(4, 1);// (column,row) lcd.print(dht2.readHumidity()); //顯示溫度值 delay(1000); //延遲一秒 } ``` ::: -->