int buttonState = 0;
void setup()
{
pinMode(6, OUTPUT);
pinMode(7, INPUT_PULLUP); // 設定7pin是讀取訊號的位置
}
void loop()
{
buttonState = digitalRead(7);
if (buttonState == LOW)
{
digitalWrite(6, HIGH);
}
else
{
digitalWrite(6, LOW);
}
}
接線圖
改成input_pullup後運作的畫面
心得:
這次課程學到如何利用按鈕的通路斷路讓腳位判斷是否發出訊號,了解這個邏輯後,就可以很快速地完成接線,但途中有一個問題是燈會忽明忽暗,利用序列埠監控後發現,訊號不穩定會一直回傳1或0,所以在程式加入input_pullup,但又產生另外一個問題,input_pullup的預設是high所以在接線和程式上要改成如果判斷為LOW,會給出訊號,再把按鈕另一端的5V改成GND就能完成,我這次理解蠻快,也很快地做完了,所以在幫助同學完成的過程中也讓自己更理解這次的課程。
計時器程式碼
#include <ThreeWire.h>
#include <RtcDS1302.h>
#include <TM1637Display.h>
#define countof(a) (sizeof(a) / sizeof(a[0]))
#define CLK 11
#define DIO 12
TM1637Display display(CLK, DIO);
ThreeWire myWire(9, 10, 8); //接腳: DAT/IO, SCLK/CLK, RST/CE (DS1302的接線自行更改)
RtcDS1302<ThreeWire> Rtc(myWire);
void setup()
{
Serial.begin(9600);//鮑率57600
display.setBrightness(7);
Serial.print("compiled: ");
Serial.print(__DATE__);
Serial.println(__TIME__);
Rtc.Begin();
RtcDateTime compiled = RtcDateTime(__DATE__, __TIME__);
printDateTime(compiled);
Serial.println();
if (!Rtc.IsDateTimeValid())
{
// Common Causes:
// 1) 第一次使用但設備還沒跑好
// 2) 電池電量低
Serial.println("RTC lost confidence in the DateTime!");
Rtc.SetDateTime(compiled);
}
if (Rtc.GetIsWriteProtected())
{
Serial.println("RTC was write protected, enabling writing now");
Rtc.SetIsWriteProtected(false);
}
if (!Rtc.GetIsRunning())
{
Serial.println("RTC was not actively running, starting now");
Rtc.SetIsRunning(true);
}
//判斷DS1302上紀綠的時間和編譯時的時間,哪個比較新
//如果編譯時間比較新,就進行設定,把DS1302上的時間改成新的時間
//now:DS1302上紀綠的時間,compiled:編譯時的時間
RtcDateTime now = Rtc.GetDateTime();
if (now < compiled)
{
Serial.println("RTC is older than compile time! (Updating DateTime)");
Rtc.SetDateTime(compiled);
}
else if (now > compiled)
{
Serial.println("RTC is newer than compile time. (this is expected)");
}
else if (now == compiled)
{
Serial.println("RTC is the same as compile time! (not expected but all is fine)");
}
}
void loop()
{
RtcDateTime now = Rtc.GetDateTime();
printDateTime(now);//呼叫副函數
Serial.println();
if (!now.IsValid())
{
// Common Causes:
// 1) the battery on the device is low or even missing and the power line was disconnected
Serial.println("RTC lost confidence in the DateTime!");
}
}
void printDateTime(const RtcDateTime &dt)
{
char datestring[20];
snprintf_P(datestring,
countof(datestring),
PSTR("%02u/%02u/%04u %02u:%02u:%02u"),
dt.Month(),//月
dt.Day(),//日
dt.Year(),//年
dt.Hour(),//小時
dt.Minute(),//分鐘
dt.Second());//秒
Serial.println(dt.Minute());
display.showNumberDecEx(dt.Hour(), 0b11100000, 2, 2); //前兩位數設定為小時
display.showNumberDecEx(dt.Minute(), 0b11100000, true, 2, 2); //後兩位數設定為分鐘
delay(1000);
}
計時器加上顯示器程式碼
#include <Wire.h>
#include <ThreeWire.h>
#include <RtcDS1302.h>
ThreeWire myWire(9, 10, 8); //接腳: DAT/IO, SCLK/CLK, RST/CE (DS1302的接線自行更改)
RtcDS1302<ThreeWire> Rtc(myWire);
void setup ()
{
Serial.begin(9600);
Serial.print("compiled: ");
Serial.print(__DATE__);
Serial.println(__TIME__);
Rtc.Begin();
//__DATE__,__TIME__,是程式碼編譯時的日期和時間
RtcDateTime compiled = RtcDateTime(__DATE__, __TIME__);
printDateTime(compiled);
Serial.println();
//判斷DS1302是否接好
if (!Rtc.IsDateTimeValid())
{
// Common Causes:
// 1) first time you ran and the device wasn't running yet
// 2) the battery on the device is low or even missing
Serial.println("RTC lost confidence in the DateTime!");
Rtc.SetDateTime(compiled);
}
if (Rtc.GetIsWriteProtected())
{
Serial.println("RTC was write protected, enabling writing now");
Rtc.SetIsWriteProtected(false);
}
if (!Rtc.GetIsRunning())
{
Serial.println("RTC was not actively running, starting now");
Rtc.SetIsRunning(true);
}
//判斷DS1302上紀綠的時間和編譯時的時間,哪個比較新
//如果編譯時間比較新,就進行設定,把DS1302上的時間改成新的時間
//now:DS1302上紀綠的時間,compiled:編譯時的時間
RtcDateTime now = Rtc.GetDateTime();
if (now < compiled)
{
Serial.println("RTC is older than compile time! (Updating DateTime)");
//編譯時間比較新,把DS1302上的時間改成編譯的時間
Rtc.SetDateTime(compiled);
}
else if (now > compiled)
{
Serial.println("RTC is newer than compile time. (this is expected)");
}
else if (now == compiled)
{
Serial.println("RTC is the same as compile time! (not expected but all is fine)");
}
}
void loop ()
{
RtcDateTime now = Rtc.GetDateTime();
printDateTime(now);
Serial.println();
//判斷DS1302是否正常,如果不正常,一般是線沒接好,或是電池沒電了
if (!now.IsValid())
{
Serial.println("RTC lost confidence in the DateTime!");
}
delay(10000); // 10秒更新一次
}
#define countof(a) (sizeof(a) / sizeof(a[0]))
//顯示完整年月日時間的副程式
void printDateTime(const RtcDateTime& dt)
{
char datestring[20];
snprintf_P(datestring,
countof(datestring),
PSTR("%02u/%02u/%04u %02u:%02u:%02u"),
dt.Month(),
dt.Day(),
dt.Year(),
dt.Hour(),
dt.Minute(),
dt.Second() );
Serial.print(datestring);
}
學到了如何更改程式以改變腳位。這不僅讓我更深入地了解程式碼的結構和邏輯,也幫助我建立了對程式碼的掌控感。能夠修改程式碼來實現特定功能,讓我對程式設計的應用有了更多的信心與期待。
#include <Servo.h> //載入函式庫,這是內建的,不用安裝
Servo myservo; // 建立SERVO物件
int r = A0 ; //假設一個字母R代替A0的孔洞
int sensorValue;
void setup() {
Serial.begin(9600);
myservo.attach(9); // 設定要將伺服馬達接到哪一個PIN腳
// myservo.write(0); //歸零
// put your setup code here, to run once:
}
void loop() {
sensorValue = analogRead(r);
Serial.println(sensorValue);
if(sensorValue <= 50){ //如果光敏電阻的數值小於或大於某一數值時,則伺服馬達轉動。
myservo.write(150); //旋轉到30度
delay(100); //延遲0.5 秒
myservo.write(30); //旋轉到0度
delay(100); //延遲0.5秒 可自行更改
}
else{
myservo.write(90); //旋轉到0度
delay(15);
}
}
在這次課程中我領到的馬達好像和別人的不太一樣,其他人的是只要設定角度就行,但我的是要設定速度,在這個前提下我的程式碼就要有所更改,還要在速度和延遲中找到平衡
影片中我用電火布固定感測器
DHT11修改後程式
//使用DHT11函式庫
#include <SimpleDHT.h>
//定義腳位自行更改
int pinDHT11 = 2;
//建立DHT11函式庫物件
SimpleDHT11 dht11;
void setup() {
Serial.begin(9600);
}
void loop() {
byte temperature = 0;
byte humidity = 0;
int err = SimpleDHTErrSuccess;
// start working...
Serial.println("=================================");
if ((err = dht11.read(pinDHT11, &temperature, &humidity, NULL)) != SimpleDHTErrSuccess) {
Serial.print("Read DHT11 failed, err="); Serial.println(err);delay(1000);
return;
}
Serial.print("Humidity = ");
Serial.print((int)humidity);
Serial.print("% , ");
Serial.print("Temperature = ");
Serial.print((int)temperature);
Serial.println("C ");
delay(1000); //每1秒顯示一次
}
這次只要稍微修改程式碼上的腳位或是依照程式碼接線可以運作了
#define m1 294 //頻率1
#define m5 440 //頻率2
#define h5 880 //頻率3
int red = 2;
int yellow = 3;
int green = 4;
int v=5; //蜂鳴器輸出
int trigPin = 12; //定義 觸發輸出腳位 Trig Pin
int echoPin = 11; //定義 回音接收腳位 Echo Pin
float duration, distance; //定義 時間長度duration 與 距離distance 格式為 浮點數
void setup(){
Serial.begin(9600); //設定 序列埠 鮑率
while (!Serial){ ;} //等待序列埠接通
pinMode(red, OUTPUT);
pinMode(yellow, OUTPUT);
pinMode(green, OUTPUT);
pinMode(v, OUTPUT);
pinMode(trigPin, OUTPUT); //設定 觸發輸出腳位(trigPin)的功能為 輸出(OUTPUT)
pinMode(echoPin, INPUT); //設定 回音接收腳位(echoPin)的功能為 輸入(INPUT)
}
void loop(){
//於 觸發輸出腳位 Trig Pin 產生觸發脈衝(應大於 10微秒)
digitalWrite(trigPin, LOW); // 讓觸發腳位trigPin 確實降回低電位,持續 20微秒 (us)
delayMicroseconds(20);
digitalWrite(trigPin, HIGH); // 觸發腳位trigPin 變成高電位,持續 20微秒 (us)
delayMicroseconds(20);
digitalWrite(trigPin, LOW); // 觸發腳位trigPin 再降回低電位,完成脈衝,使HC-SR04產生聲音脈衝出去
duration = pulseIn(echoPin, HIGH); // 於回音接收腳位(Echo Pin) 收到高電位(High) 的時間 (微秒 us)
distance = duration*0.0170; // 將聲音 "來回" 時間(us) 換算成距離 (cm) [ 假設聲波速率為 340 m/s ]
if (distance >= 25 ){
digitalWrite(green, HIGH);
digitalWrite(yellow, LOW); digitalWrite(red, LOW);
tone(v, m1, 200);
delay(250);
}
else if (distance >= 10 and distance <= 25){
digitalWrite(yellow, HIGH);
digitalWrite(green, LOW); digitalWrite(red, LOW);
tone(v, m5, 150);
delay(200);
}
else {
digitalWrite(red, HIGH);
digitalWrite(green, LOW); digitalWrite(yellow, LOW);
tone(v, h5, 100);
delay(150);
}
//Serial.println(distance); // 將 距離distance 傳送到序列埠暫存器
Serial.flush(); // 等待序列埠暫存器中的數據傳送至電腦完畢
delayMicroseconds(duration*1.5); //避免收到更早發出的聲音脈衝 回音
}
這次的線比較麻煩,但照著程式碼還是挺簡單的,如果沒有看著程式碼接線的能力的話,會有挺大的障礙