43
阿爾杜伊諾 Arduino: Lv. 1 Mutienliao.TW ¶èÖēB°Ĥġ, 2013-03-18

Arduino: Intro and Digital I/O

Embed Size (px)

DESCRIPTION

Arduino -- Introduction & Basic Digital Input/Output, by Mao Wu

Citation preview

Page 1: Arduino: Intro and Digital I/O

阿爾杜伊諾Arduino: Lv. 1

Mutienliao.TW ¶�èÖēB°Ĥġ, 2013-03-18

Page 2: Arduino: Intro and Digital I/O

ArduinoIntroduction

Page 3: Arduino: Intro and Digital I/O

Human Computer Interactive / Interface ??

Page 4: Arduino: Intro and Digital I/O

physical computing system

Page 5: Arduino: Intro and Digital I/O

What is Arduino?

Page 6: Arduino: Intro and Digital I/O

Arduino Hardware Arduino Software Ű Open Source

Physical Computing Platform & GroupŰ

Page 7: Arduino: Intro and Digital I/O

• ­�ĸ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 �»)

Page 8: Arduino: Intro and Digital I/O

各式各樣的Arduino........族繁不及備載

Page 9: Arduino: Intro and Digital I/O

Digital Out Digital In Analog In Analog Out Communication

Page 10: Arduino: Intro and Digital I/O

Digital Out

Page 11: Arduino: Intro and Digital I/O

Analog Out

Page 12: Arduino: Intro and Digital I/O

Digital In

Page 13: Arduino: Intro and Digital I/O

Analog In

Page 14: Arduino: Intro and Digital I/O

Communication

Page 15: Arduino: Intro and Digital I/O

Digital In Digital Out

Analog In Analog Out

Communication

Emotion Experience

Page 16: Arduino: Intro and Digital I/O

| Prepare to test Arduino board

- děArduino�ø�

- Ō\LED Blinkÿ!ÈFile > Examples > Basic > Blink

Page 17: Arduino: Intro and Digital I/O

| 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 ]

Page 18: Arduino: Intro and Digital I/O

| Upload the program

ã�;űĊĨîãÔŭ�JŊħĢ�ŅºŞöbÍ

Page 19: Arduino: Intro and Digital I/O

| 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�îݺÐŕ�÷��ÊŬ

Page 20: Arduino: Intro and Digital I/O

# | 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�Ŭ

Page 21: Arduino: Intro and Digital I/O

breadborad

Page 22: Arduino: Intro and Digital I/O

Digital Out­�ĸ8

Digital Out

Page 23: Arduino: Intro and Digital I/O

#1 | Blink

Page 24: Arduino: Intro and Digital I/O

�ŧ´­�ų

• Only 1 or 0 / High or LOW / ON or OFF

HIGH

LOW

1

0

Page 25: Arduino: Intro and Digital I/O

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

Page 26: Arduino: Intro and Digital I/O

LED ¦�

Z%pin ?

(ø��) �ĞéZ%pin?pinMode(who, ?)

(ø��) ĸ8digitalWrite(who,?)

Page 27: Arduino: Intro and Digital I/O

#2 | Loop

Page 28: Arduino: Intro and Digital I/O

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

Page 29: Arduino: Intro and Digital I/O

#Bonus | Light Talk

Page 30: Arduino: Intro and Digital I/O

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

Page 31: Arduino: Intro and Digital I/O

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} }; !

Page 32: Arduino: Intro and Digital I/O

#3 | Blink Without Delay

Page 33: Arduino: Intro and Digital I/O

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

Page 34: Arduino: Intro and Digital I/O

Ñĵ

Delay()î�Õ : Ñĵ,�îY�ŭ�ÊN)�� ŭO���ŵ��ŧ �Ņ�)ŭªàbńŭýŌ��º7) �Ŭ

ŀSébũœ�Ļ� �ŭ5š=þŪîãÔ

millis()î�Õ : »Ũ.´Š�î¼Fèŭ��ʼF�kw�ŭºÐŕ�ÏŵōIóóÐ�ÃîãÔŭPTŵŷç5� �Ŭ

ŀSébTŵĞ)�TãÔî ��

Page 35: Arduino: Intro and Digital I/O

輸入才是互動的精華

Page 36: Arduino: Intro and Digital I/O

Digital Input­�ĸ2

Digital In

Page 37: Arduino: Intro and Digital I/O

#6 | Button

Page 38: Arduino: Intro and Digital I/O

#6 | Button

Page 39: Arduino: Intro and Digital I/O

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

Page 40: Arduino: Intro and Digital I/O
Page 41: Arduino: Intro and Digital I/O

#7 | StateChangDetection #7îċčø�ŭb File > Examples > Digital > StateChangDetection

Page 42: Arduino: Intro and Digital I/O

#8 | Debounce #7îċčø�ŭb File > Examples > Digital > Debounce

Page 43: Arduino: Intro and Digital I/O

#| ĈSċč

[ś ]ŭE�ĥĥóp�é�Ś¡ň¤?�%Ýŭ:§�%Ýî�·

1234