113/10/14 本週教學內容:
Tinkercad的使用方法
thingiverse網站認識
3D列印實作嘗試
這次上課在了解如何實際操作3D列印機後自己設計並印了一隻小雪人出來,第一次接觸3D列印覺得很新奇,也很有興趣,期待未來的課程。
113/10/21 本週教學內容:
麵包版與Arduino的基本認識
Arduino的程式語言認識(pinMode、OUTPUT/INPUT...等)並了解上拉電阻及其啟動方法
設計內容:
練習1:
點亮兩個LED燈泡並且讓兩個燈泡輪流閃爍,控制閃爍的頻率在2秒一次。
const int ledPin1 = 13; // 第一個LED連接到腳位13
const int ledPin2 = 12; // 第二個LED連接到腳位12
void setup() {
// 設定腳位為輸出
pinMode (ledPin1, OUTPUT) ; //設定腳位功能
pinMode (ledPin2, OUTPUT);
}
//一直做某件事
void loop() {
// 打開第一個LED,關閉第二個LED
digitalWrite (ledPin1, HIGH); //執行腳位的高低點位輸出狀態
digitalWrite (ledPin2, LOW);
delay (2000);// 等待2秒
//打開第二個LED,關閉第一個LED
digitalWrite (ledPin1, LOW);
digitalWrite (ledPin2, HIGH);
delay (2000); // 等待2秒
}
本次上課又接觸了許多從未摸過的東西,在經過簡單了解後實際上手仍有些難度,但在最後成功達到目的時的成就感讓人感受到十分有成就感。
113/11/11 本週教學內容:
按鈕與其程式語言
序列阜、Serial.(用於控制序列阜)系列的程式語言學習
if...else...的語法學習與實際練習
設計內容:
練習1:
int buttonState = 0;
void setup()
{
pinMode(13, OUTPUT); // 13pin是Arduino內建LED的顯示角位
pinMode(7, INPUT_PULLUP); // 設定7pin是讀取訊號的位置
}
void loop()
{
buttonState = digitalRead(7);
if (buttonState == HIGH)
{
digitalWrite(13, LOW);
}
else
{
digitalWrite(13, HIGH);
}
}
PS:網頁上的程式碼並不是正確解答,需要自行修改,此程式碼LOW時燈是不會亮的,而HIGH時燈會亮。
實際練習時總是做不到助教要求的按一下亮,後來才發現原來程式碼需要自己發現修改,但這種小陷阱也讓我很喜歡,像解謎一樣
113/11/18 本週教學內容:
函式庫的認識與如何使用/導入
實際練習
設計內容:
練習一:
#include <Wire.h>
#include <ThreeWire.h>
#include <RtcDS1302.h>
ThreeWire myWire(A3, A4, A5); //接腳: 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);
}
實驗結果會在序列埠視窗中跑出11/18/2024 09:42:08
練習二:
#include <TM1637Display.h>
#define countof(a) (sizeof(a) / sizeof(a[0]))
#define CLK 11
#define DIO 12
TM1637Display display(CLK, DIO);
ThreeWire myWire(7, 6, 5); //接腳: 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);
}
在顯示板上顯示現在時間
這次上課的內容因為逐漸習慣麵包版與Arduino,做起來覺得挺簡單的,只是剛起床有點腦子不清醒,但最後還是順利完成了。
11/25 本週教學內容:
使用光敏電阻與馬達做出自動遊玩小恐龍的裝置
設計內容:
當仙人掌出現時,光線會變弱,電阻值會變小,使得馬達轉動,按下空白鍵,促使小恐龍跳起來;而當光線亮時,馬達不會轉動。
程式碼:
#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 <= 200){ //如果光敏電阻的數值小於或大於某一數值時,則伺服馬達轉動。
myservo.write(30); //旋轉到30度
delay(50); //延遲0.5秒
myservo.write(0); //旋轉到0度
delay(50); //延遲0.5秒 可自行更改
}
}
如果要將其變成夜晚模式,則要改變光敏電阻的數值到非常大
沒想到只是一個小電阻跟馬達就可以做到我以為很困難的自動遊戲機器人,一開始雖然覺得很無厘頭,可卻也看到了他可以再去做深入探討的可塑性,在做好裝置實際運用時,也有許多需要特別注意的地方(如對著鍵盤的角度跟程式碼中光敏電阻感測值的大小設定),是個有趣的課程
113/12/02 本週課程內容:
利用DHT溫溼度模組(需要函式庫)來偵測當前室內的溫度與濕度
利用超聲波感測器與蜂鳴器製作一個倒車雷達
設計內容:
1、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秒顯示一次
}
2、利用蜂鳴器、超聲波感測器、LED燈,做出類似倒車雷達的裝置。
#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); //避免收到更早發出的聲音脈衝 回音
}
在接線時有一些粗心所以遇到了點小障礙,但找到問題點後就成功完成了