arduino: intro and digital i/o
DESCRIPTION
Arduino -- Introduction & Basic Digital Input/Output, by Mao WuTRANSCRIPT
阿爾杜伊諾Arduino: Lv. 1
Mutienliao.TW ¶�èÖēB°Ĥġ, 2013-03-18
ArduinoIntroduction
Human Computer Interactive / Interface ??
physical computing system
What is Arduino?
Arduino Hardware Arduino Software Ű Open Source
Physical Computing Platform & GroupŰ
• �ĸ8ĸ2414Ą Ŵ Digital Pins 0-13
• Digital Pins 0-1/Serial In/Out - TX/RX
- ė�ºé>Serial portēŖĐý-ĸŭ�ĩ=�é Pin 0,1.
• ŝÑĸ246Ą Ŵ Analog Input Pins 0-5
• ŝÑĸ8 Analog Output * (Digital Pins 3,5,6,9,10,11)
• Reset ¡ň- S1
• ŜjŖÙĸ2
• PĺľJumper:§êUSB"Ŗ�DCŖÙĸ2 (Duemilanove�ĒE:§)
• USBP�ø�êŖĐüĸ2>¿�îµáŭ�ďTŵ"ĆŖ • ŖÙ"�Vin, 5V, 3.3V (Diecimila�î¿r �»)
各式各樣的Arduino........族繁不及備載
Digital Out Digital In Analog In Analog Out Communication
Digital Out
Analog Out
Digital In
Analog In
Communication
Digital In Digital Out
Analog In Analog Out
Communication
Emotion Experience
| Prepare to test Arduino board
- děArduino�ø�
- Ō\LED Blinkÿ!ÈFile > Examples > Basic > Blink
| Set up your board
• ł©���éî¿r: Tools > Board
[ Mac OS X ]
[ Mac OS X ]
• ł©Arduino�bîserial port: Tools > Serila Port
Macîâ½ïº�/dev/tty.usbserial- Ōř*
[ Windows ]
[ Windows ]
| Upload the program
ã�;űĊĨîãÔŭ�JŊħĢ�ŅºŞöbÍ
| Upload the program
Ċzø�
Vertify
Update to board*
* Arduino NG î�éĎŭ_ÚĔ¿rÓ»ĒEResetCďŭb¡UpdateňAŭ�Ř1¡�Arudino ¿r�îResetňŭ�5b�)��E�îã��ŭ£Ěb¬ŌResetň�ŭŢ�¡�Updateňŭ�ďÌõ�-ø�Ŭ
TX/RXîLEDã�ݺ�Ŋŋß
ë-ĸą¾�ŭýā2~3÷ŭPin13îpinđºŋß(_Ú�&�-îø�)
• Ō\ File > Examples > Basic > Blink
• ĥĚ¡ÜQŃî×øŭ{ø��->¿r�I
• ė�C....¿r�îݺÐŕ�÷��ÊŬ
# | Troubleshooting
ėÓ�-�Cŭ>�º´�ŧ[śDų
• ÉÁ�´Vł©Ìõî¿rēSerial portŭņ°Ĥv�ÊŬ
• ĦÕ»[śŭ$ÌĦÕ��P��Ŭ
• Serial Port ÌĜ�é�ŭŎ¢5�ÌbéÍSerila portîø�
• ¿r�îŖÙJump»Ó»ĤvÌõŭ»Ì�"ŖŮDuemilanove/UNO ºĒE:§ů
• ¿rÌbìä�ô�ŭ¡�¿r�îResetŭ�5<qGŭÛ�b¬ŌResetŵŭŢ�¡�ĴťüîUpdateŬ
• �£îŖij��ArudinoĭĶľŦŭĒE#Ī±¢USBŬ
• ��mPďî´ŭ�î¿r�-ĸĉh�Ŭ
breadborad
Digital Out�ĸ8
Digital Out
#1 | Blink
�ŧ´�ų
• Only 1 or 0 / High or LOW / ON or OFF
HIGH
LOW
1
0
int ledPin = 13; // LED connected to digital pin 3
void setup(){ pinMode(ledPin, OUTPUT); // sets the digital pin as output}
void loop(){ digitalWrite(ledPin, HIGH); // sets the LED on delay(1000); // waits for a second digitalWrite(ledPin, LOW); // sets the LED off delay(1000); // waits for a second}
pinMode(pin, Mode) digitalWrite(pin, value) delay(ms)
#1îċčø�ŭb File > Examples > Basic > Blink
LED ¦�
Z%pin ?
(ø��) �ĞéZ%pin?pinMode(who, ?)
(ø��) ĸ8digitalWrite(who,?)
#2 | Loop
int timer = 100; // The higher the number, the slower the timing.
void setup(){ int i;
// use a for loop to initialize each pin as an output: for (int thisPin = 2; thisPin <= 7; thisPin++ ) { pinMode(thisPin, OUTPUT); }}
void loop() { // loop from the lowest pin to the highest: for (int thisPin = 2; thisPin <=7 ; thisPin++) { // turn the pin on: digitalWrite(thisPin, HIGH); delay(timer); // turn the pin off: digitalWrite(thisPin, LOW); }
// loop from the highest pin to the lowest: for (int thisPin = 7; thisPin >= 2; thisPin--) { // turn the pin on: digitalWrite(thisPin, HIGH); delay(timer); // turn the pin off: digitalWrite(thisPin, LOW); }}
#2îċčø�ŭb File > Examples > Control > ForLoopIteration
#Bonus | Light Talk
int pins[] = { 2, 3, 4, 5, 6, 7 }; !! // LEDđ�WOint lights = 6; ! ! // LEDÝ%int pattens = 10; ! ! // a��4»�;ĞŞöbyte graphy[10][6] = { {0,1,1,1,0,0}, {1,1,1,1,0,0}, {1,1,1,1,1,0}, {0,1,1,1,1,0}, {0,0,1,1,1,1}, {0,0,1,1,1,1}, {0,1,1,1,1,0}, {1,1,1,1,1,0}, {1,1,1,1,0,0}, {0,1,1,1,0,0} }; !// a�îĄ�
void setup(){ for (int i = 0; i < lights; i++) ! // �éŒ;0ăîWO´ê0>light-1 pinMode(pins[i], OUTPUT); !! // {Ð%WOîđ�ĤÚĸ8Å�}
void loop(){ for(int k = 0; k < lights; k++) { ! // Ŏ¢�»Ýŭ�a��+ûí digitalWrite(pins[k], LOW); } delay(40); ! ! ! // �Ł40Ò÷ for (int i = 0; i < pattens; i++) { ! // a�îÐ�;... for(int j = 0; j < lights; j++) {! // 5�îÐŚLED... if(patten[i][j]==1) { digitalWrite(pins[j], HIGH); ! // ėĞŞöŭ��İĻŚLED }else { digitalWrite(pins[j], LOW); ! // ė�Şöŭ�Ŏ¢ĻŚLED } } delay(1); ! ! // �Ł1Ò÷ } for(int k = 0; k < lights; k++) { ! // Ŏ¢�»Ýŭ�a�Q+ûí digitalWrite(pins[k], LOW); } delay(40);! ! ! // �Ł40Ò÷}
Fig.1
[a]
[b]
#Bonusîċčø�ŭb http://mutienliao.tw/arduino/Light_Talk.pde
byte graphy[10][6] = { {0,1,1,1,0,0}, {1,1,1,1,0,0}, {1,1,1,1,1,0}, {0,1,1,1,1,0}, {0,0,1,1,1,1}, {0,0,1,1,1,1}, {0,1,1,1,1,0}, {1,1,1,1,1,0}, {1,1,1,1,0,0}, {0,1,1,1,0,0} }; !
#3 | Blink Without Delay
const int ledPin = 13; // �épin 13ÚLED
int ledState = LOW; // é ģʼn��ÊLEDîã�long previousMillis = 0; // é /s��ʸ°îŵō
long interval = 1000; // ĤvÐŕkĞÉÁ¸°îŵō 1000ms = 1sec
void setup() { pinMode(ledPin, OUTPUT); }
void loop(){ unsigned long currentMillis = millis(); // 1ó��æbîŵō if(currentMillis - previousMillis > interval) { // �æbŵōIJ�ʸ°ŵōÑĵ��ŭ´Vl³�ĤvîÉÁŵō previousMillis = currentMillis; // $«�ʸ°ŵōű«Úæbŵō
// �LEDîã�īE if (ledState == LOW) ledState = HIGH; else ledState = LOW;
// ¤?LEDî�· digitalWrite(ledPin, ledState); }}
#3îċčø�ŭb File > Examples > Digital > BlinkWithoutDelay
Ñĵ
Delay()î�Õ : Ñĵ,�îY�ŭ�ÊN)�� ŭO���ŵ��ŧ �Ņ�)ŭªàbńŭýŌ��º7) �Ŭ
ŀSébũœ�Ļ� �ŭ5š=þŪîãÔ
millis()î�Õ : »Ũ.´Š�î¼Fèŭ��ʼF�kw�ŭºÐŕ�ÏŵōIóóÐ�ÃîãÔŭPTŵŷç5� �Ŭ
ŀSébTŵĞ)�TãÔî ��
輸入才是互動的精華
Digital Input�ĸ2
Digital In
#6 | Button
#6 | Button
const int buttonPin = 2; // the number of the pushbutton pinconst int ledPin = 13; // the number of the LED pin
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); }}
#6îċčø�ŭb File > Examples > Digital > Button
#7 | StateChangDetection #7îċčø�ŭb File > Examples > Digital > StateChangDetection
#8 | Debounce #7îċčø�ŭb File > Examples > Digital > Debounce
#| ĈSċč
[ś ]ŭE�ĥĥóp�é�Ś¡ň¤?�%Ýŭ:§�%Ýî�·
1234