第一小時:準備
1.在桌面上找到Arduino UNO程式並啟動它,【確認可以執行】確認可以執行後請【關閉程式】。如果無法執行請舉手。
2.啟動chrome瀏覽器,打開網址【www.myiot.idv.tw】。如果無法打開網頁請舉手。
開啟網頁後選擇右邊【推廣課程】【職業資訊體驗課程】
3.開啟chrome瀏覽器的新分頁,打開網址【chatgpt.com】。如果無法打開網頁請舉手。
4.開啟Arduino IDE編譯器。並依照老師指示改介面為中文。
連接上 Arduino 開發板,在【工具】【板子】確認 板子型號為(ARDUINO UNO),
在【工具】【序列埠】選擇使用的COMXX (ARDUINO UNO)
5.在chatGPT下達腳色扮演命令,【你是一位Arduino 與processing的程式設計師】
【我在Arduino 上接了四顆LED1-4分別連接在arduino的pin13-10,LED是低電位時發光,
幫我設計一個輪流點亮LED1-4的程式。】
提示:善用 CTRL+A、CTRL+V
6. 在chatGPT下達腳色扮演命令,
【按一下開關S1(A1,按下時為低電位)讓LED1-4輪流亮起,不斷重複。
按一下開關S2讓LED1-4暫停。
按一下開關S3讓LED1-4反向輪流亮起,不斷重複。】
7.將七段顯示器的參考範例給AI分析。【範例請從網站複製】
8.在七段顯示器上從0向上計數。
9. 在七段顯示器上從100倒數計時。
10.在七段顯示器上從30倒數計時,到達0的時候,蜂鳴器發出警報聲3秒鐘。
*Define shift register pins used for seven segment display */
#define LATCH_DIO 4
#define CLK_DIO 7
#define DATA_DIO 8
/* Segment byte maps for numbers 0 to 9 */
const byte SEGMENT_MAP[] ={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90} ;
/* Byte maps to select digit 1 to 4 */
const byte SEGMENT_SELECT[] ={0xF1,0XF2,0XF4,0XF8};
void setup ()
{
/* Set DIO pins to outputs */
pinMode (LATCH_DIO,OUTPUT);
pinMode (CLK_DIO,OUTPUT) ;
pinMode (DATA_DIO,OUTPUT) ;
}
/* Main program */
void loop()
{
/* Update the display with the current counter value */
WriteNumberToSegment(0,0);
WriteNumberToSegment(1,1);
WriteNumberToSegment(2,2);
WriteNumberToSegment(3,3);
}
/* Write a decimal number between O and 9 to one of the 4 digits of the display */
void WriteNumberToSegment(byte Segment, byte Value)
{
digitalWrite(LATCH_DIO,LOW);
shiftOut(DATA_DIO, CLK_DIO, MSBFIRST, SEGMENT_MAP[Value]);
shiftOut(DATA_DIO, CLK_DIO, MSBFIRST, SEGMENT_SELECT[Segment] );
digitalWrite(LATCH_DIO,HIGH);
}
確認開發板型號
確認串列埠號碼
外接元件時先斷電,接好後先確認VCC與GND沒有接錯才可以復電。
開啟ARDUINO IDE後第一步先開啟【工具】【開發板】確認板子
接上ARDUINO IDE後第一步先開啟【工具】【串列埠】確認COM埠
範例一:
測試Arduino UNO
開啟Arduino IDE
使用USB接上Arduino 與電腦
選擇 【工具】【開發板】選【Arduino Uno】
選擇 【工具】【序列埠】選【COM??:(Arduino Uno)】
選擇 【檔案】【範例】選【內建範例】【01.Basic】【Blink】
選擇【快速選單】第二個圓形箭頭或者【草稿碼】【下載】,下載程式至Arduino
板子左上方會有一顆LED閃爍發光。
恭喜電路板應該是正常的。
chatGPT腳色設定:【你是一位善於撰寫Arduino IDE與Processing的程式設計師】
【今天今天實驗用板子的設定:
按鈕S1(A1)
按鈕S2(A2)
按鈕S3(A3)
LED4(D10)
LED3(D11)
LED2(D12)
LED1(D13)
LED低電位發光
蜂鳴器(D3)
74HC595控制共陽極七段顯示器設定
74HC595 Latch(D4)
74HC595 Clock(D7)
74HC595 Data(D8)
dp(bit7),G(bit6),F(bit5),E(bit4),D(bit3),C(bit2),B(bit1),A(bit0)
】
題目一:【寫一個程式讓七段顯示器從0000開始向上計數 】
題目二:【寫一個程式讓七段顯示器從0099開始倒數 】
題目三:【按下按鈕S1啟動倒數,按下按鈕S2暫停倒數,按下按鈕S3重置計數值為0099 】
題目四:【按下按鈕S1讓LED1-LED4依序點亮,按下按鈕S2暫停,按下按鈕S3讓LED1-LED4反向點亮 】
*Define shift register pins used for seven segment display */
#define LATCH_DIO 4
#define CLK_DIO 7
#define DATA_DIO 8
/* Segment byte maps for numbers 0 to 9 */
const byte SEGMENT_MAP[] ={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90} ;
/* Byte maps to select digit 1 to 4 */
const byte SEGMENT_SELECT[] ={0xF1,0XF2,0XF4,0XF8};
void setup ()
{
/* Set DIO pins to outputs */
pinMode (LATCH_DIO,OUTPUT);
pinMode (CLK_DIO,OUTPUT) ;
pinMode (DATA_DIO,OUTPUT) ;
}
/* Main program */
void loop()
{
/* Update the display with the current counter value */
WriteNumberToSegment(0,0);
WriteNumberToSegment(1,1);
WriteNumberToSegment(2,2);
WriteNumberToSegment(3,3);
}
/* Write a decimal number between O and 9 to one of the 4 digits of the display */
void WriteNumberToSegment(byte Segment, byte Value)
{
digitalWrite(LATCH_DIO,LOW);
shiftOut(DATA_DIO, CLK_DIO, MSBFIRST, SEGMENT_MAP[Value]);
shiftOut(DATA_DIO, CLK_DIO, MSBFIRST, SEGMENT_SELECT[Segment] );
digitalWrite(LATCH_DIO,HIGH);
}
1 I2C Interface (A4 SDA, A5 SCL)
Connect with I2C communication modules, e.g. display
2 Button Module(D2, D3)
Turn on/off LED, Buzzer .etc
3 DHT11 Temperature&Humidity Sensor Module(D4)
Temp Range: 0-50°C, HUM Range: 20%-90%RH
4 Rotary Potentiometer Module(A0)
Rotation Angle: 270°, Output Voltage: 0-3.3/5V, Resistance: 10K
5 Reset button
6 3mm LED module(D13, D12)
7 RGB LED Module(D9-11)
8 Digital Ports (D7, D8)
Connect with digital signal modules, e.g. leds, buttons.
9 UART Port
Connect with UART communication modules, e.g. WiFi, Bluethooth
10 IR Receiver Module(D6)
Control range 1-8m, frequency 38kHZ.
11 Brightness Sensor Module(A1)
12 LM35D Temperature Sensor Module(A2)
Temp Range: -50-+150°C, High sensitivity
13 Passive Buzzer Module(D5)
Can emit simple tones, music needs to be programmed
14 Analog Port (A3)
Connect with analog signal modules, e.g. sensors
範例二:
更改閃爍時間
void loop() {
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH 是點亮LED)
delay(1000); // wait for a second,亮的時間
digitalWrite(LED_BUILTIN, LOW); // turn the LED off 透過LOW關閉LED
delay(1000); // wait for a second,熄滅的時間
}
範例三:
程式的基礎架構
void setup() {
// put your setup code here, to run once:
通常放置一些定義,只被執行一次
}
void loop() {
// put your main code here, to run repeatedly:
主要的程式區域,會一直被執行
}
範例四:
按鈕直接控制LED
選擇 【檔案】【範例】選【內建範例】【02.Digital】【Button】
執行
想改成按下去LED發光,放開時關閉LED,要如何修改?
範例四B:
按鈕直接控制LED 程式解說
選擇 【檔案】【範例】選【內建範例】【02.Digital】【Button】
/* 多行註解開始
Button
.... 註解
http://www.arduino.cc/en/Tutorial/Button
*/ 多行註解結束
// 單行註解constants won't change. They're used here to set pin numbers:
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
常數 整數變數 變數名稱 常數變數在程式中不能被改變
// variables will change:
int buttonState = 0; // variable for reading the pushbutton status
整數變數 變數名稱 一般數變數在程式中可以被改變
void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
}
void loop() {
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// turn LED on:
digitalWrite(ledPin, HIGH);
} else {
// turn LED off:
digitalWrite(ledPin, LOW);
}
}
範例四C:
按鈕直接控制LED 變化題
改成控制紅色LED的亮滅
改用另一顆按鈕控制
範例五:
單一按鈕開關LED ,按一下亮,再按一下滅。
選擇 【檔案】【範例】選【內建範例】【02.Digital】【StateChangeDetection】
執行好像是按4下才會改變?
想改成按一下亮,再按一下滅,要如何修改?
範例五B:
按一下亮,再按一下滅 程式解說
/*
State change detection (edge detection)
...
http://www.arduino.cc/en/Tutorial/ButtonStateChange
*/
// this constant won't change:
const int buttonPin = 3; // the pin that the pushbutton is attached to
const int ledPin = 12; // the pin that the LED is attached to
// Variables will change:
int buttonPushCounter = 0; // counter for the number of button presses
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button
void setup() {
// initialize the button pin as a input:
pinMode(buttonPin, INPUT);
// initialize the LED as an output:
pinMode(ledPin, OUTPUT);
// initialize serial communication:
Serial.begin(9600);
}
void loop() {
// read the pushbutton input pin:
buttonState = digitalRead(buttonPin);
// compare the buttonState to its previous state
if (buttonState != lastButtonState) {
// if the state has changed, increment the counter
if (buttonState == HIGH) {
// if the current state is HIGH then the button went from off to on:
buttonPushCounter++;
Serial.println("on");
Serial.print("number of button pushes: ");
Serial.println(buttonPushCounter);
} else {
// if the current state is LOW then the button went from on to off:
Serial.println("off");
}
// Delay a little bit to avoid bouncing
delay(50);
}
// save the current state as the last state, for next time through the loop
lastButtonState = buttonState;
// turns on the LED every four button pushes by checking the modulo of the
// button push counter. the modulo function gives you the remainder of the
// division of two numbers:
if (buttonPushCounter % 4 == 0) {
digitalWrite(ledPin, HIGH);
} else {
digitalWrite(ledPin, LOW);
}
}
範例六:
撰寫一個程式讓紅色與藍色LED輪流點亮,使用Chat GTP協助
啟動ChatGPT並且註冊帳號