Arduino week2
Download
Report
Transcript Arduino week2
語法:Variables & Functions
實作:Digital out (Button+LED)
簡報:廖崇義
pinMode(pin, mode)
• pin 欲指定的腳位
數位腳位 D0~D13
• mode (INPUT, OUTPUT, or INPUT_PULLUP)
INPUT 將腳位設定為輸入使用
OUTPUT 將腳位設定為輸出使用
INPUT_PULLUP 將腳位設定為輸入使用,並且使用內部提升電
阻(see next page)
EX1. pinMode ( 13 , OUTPUT )
EX2. int ledpin = 13 ;
pinMode ( ledpin, OUTPUT )
PULLUP內部提升電阻
5V
(深紅色部份)
EX.pinMode(10, INPUT_PULLUP)
當SW OFF時D10為HIGH
當SW ON時D10為LOW
實作1.
R
SW
GND
void setup()
{
pinMode(13, OUT P UT );
//sets thedigital pin 13 as output
pinMode(10, INP UT _P ULL
UP );
}
void loop()
{
boolean val digitalRead(10);
digitalWrite(13,val);
}
//sets thedigital pin 10 as input_pullup
D10
IN
digitalRead(pin)
◦
◦
◦
◦
◦
pin D0~D13
回傳值HIGH或LOW
必需配合變數讀取回傳值,變數可為boolean或int形態
boolean true or false (1、0或HIGH 、LOW)
INT -32,768 to 32,767 ,使用digitalRead( )回傳值為
HIGH時為1 , LOW時為0
digitalWrite(pin, value)
• pin D0~D13
• value為HIGH或LOW
• I/O最大限制40mA
實作2.
int ledP in = 13;
void setup()
{
//LED connectedto digital pin 13
pinMode(ledP in, OUT P UT );
//sets thedigital pin as output
}
void loop()
{
digitalWrite(ledP in,HIGH); // sets theLED on
delay(1000);
//waits for a second
digitalWrite(ledP in,LOW); // sets theLED off
delay(1000);
//waits for a second
}
實作3.
•
輸出腳改到pin10,並使用300Ω限流電阻
delay(ms)
• ms :設定程式欲暫停執行的時間,單位為millisecond
(0.001秒)
delayMicroseconds(us)
• us : 設定程式欲暫停執行的時間,單位為microsecond
(0.001ms)
Ex.
digitalWrite(ledPin,HIGH); // sets theLED on
delay(1000);
//waits for a second
digitalWrite(ledPin,LOW); // sets theLED off
delay(1000);
//waits for a second
資料型別
數值範圍
佔用記憶體
表示型態
boolean
true、false
1byte
true、false
char
-128~127
1byte
‘A’、65
unsigned char
0~255
1byte
‘A’、65
byte
0~255
1byte
B00010010 (D18)
int
-32,768 ~ 32,767
2byte(16bit) 12345
unsigned int
0~65535
2byte(16bit) 12345
long
-2,147,483,648 ~
2,147,483,647
4byte(32bit) 1234567890
unsigned long
0 ~ 4,294,967,295
4byte(32bit) 1234567890
float
-3.4028235E+38~
3.4028235E+38
4byte(32bit)
回傳Arduino晶片的時間計數值
按Reset或程式重載時歸零
回傳值為unsigned long型別
約50天後溢位歸零重數
Ex. unsigned long timex ;
timex =millis() ;
if (conditional) //判斷式
{
// do something here
}
判斷成立執行大括號內程式碼
判段式運算符 ==, !=, <, >
◦== ex.if( val == HIGH )
◦!= ex.if( val != HIGH )
◦< ex.if( val < 5 )
◦> ex.if( val > 5 )
int ledP in = 10;
//LED
int buttonP in 5;
//buttonPin
int val 0; //按鍵計數值
booleanled_status 1; //LED狀態變數
boolean btn_status; //按鍵狀態
void setup()
{
pinMode(ledP in,OUT P UT );
pinMode(buttonP in,INP UT _P ULL
UP );
}
void loop()
{
btn_status digitalRead( buttonP in); //讀取按鍵狀態
if( btn_status LOW ) //當按鍵被按下時
{
val val 1; //計數加1
if( val 3) // 當按下button3次時LED燈動作
{
digitalWrite(ledP in,led_status) ; //LED輸出變換
val 0; //歸零計數
led_status !led_status; //下次LED輸出狀態反相
}
}
}
int ledPin = 13;
//LED
int buttonPin = 10; //buttonPin
int val = 0;
//按鍵計數值
boolean led_status =1; //LED狀態變數
boolean btn_status;
//按鍵狀態
void setup()
{
pinMode(ledPin,OUTPUT);
pinMode(buttonPin,INPUT_PULLUP);
}
void loop()
{
btn_status=digitalRead(buttonPin); //讀取按鍵狀態
if(btn_status == LOW)
//當按鍵被按下時
{
delay(500); //暫停0.5秒避開抖動雜訊
val=val+1; //計數加1
if(val == 3) //當按下button3次時LED燈動作
{
digitalWrite(ledPin,led_status);//LED輸出變換
val=0;
//計數歸零
led_status = !led_status;
//下次LED輸出狀態反相
}
}
}
取樣分析(加分題)
10~100次
∞次
10~100次
5V
0V
雜訊區
>100ms
密集區
雜訊區
>100ms
Arduino
pin
顯示
號碼
2
a
7
3
b
6
4
c
4
5
d
2
6
e
1
7
f
9
8
g
10
0
1
1
1
1
1
1
0
1
0
1
1
0
0
0
0
2
1
1
0
1
1
0
1
3
1
1
1
1
0
0
1
4
0
1
1
0
0
1
1
5
1
0
1
1
0
1
1
6
1
0
1
1
1
1
1
7
1
1
1
0
0
0
0
8
1
1
1
1
1
1
1
9
1
1
1
0
0
1
1
10
6
8
7
9
1
2
3
4
10912467
byte seven_disp[10][7] = { { 1,1,1,1,1,1,0 },
{ 0,1,1,0,0,0,0 },
{ 1,1,0,1,1,0,1 },
{ 1,1,1,1,0,0,1 },
{ 0,1,1,0,0,1,1 },
{ 1,0,1,1,0,1,1 },
{ 1,0,1,1,1,1,1 },
{ 1,1,1,0,0,0,0 },
{ 1,1,1,1,1,1,1 },
{ 1,1,1,0,0,1,1 }
};
int i=0;
void setup()
{
pinMode( 2, OUTPUT );
pinMode( 3, OUTPUT );
pinMode( 4, OUTPUT );
pinMode( 5, OUTPUT );
pinMode( 6, OUTPUT );
pinMode( 7, OUTPUT );
pinMode( 8, OUTPUT );
}
// = 0
// = 1
// = 2
// = 3
// = 4
// = 5
// = 6
// = 7
// = 8
// = 9
void loop()
{
digitalWrite( 2 , seven_disp[i][0] ) ;
digitalWrite( 3 , seven_disp[i][1] ) ;
digitalWrite( 4 , seven_disp[i][2] ) ;
digitalWrite( 5 , seven_disp[i][3] ) ;
digitalWrite( 6 , seven_disp[i][4] ) ;
digitalWrite( 7 , seven_disp[i][5] ) ;
digitalWrite( 8 , seven_disp[i][6] ) ;
delay(500);
i=i+1;
if(i==10)
i=0;
}
加分題
承上題七段顯示器,按一次按鍵數字加1