Page 1

Arduino Let’s Interacting in Arduino

[
Class
2
]

April 15 2008 , MAO


what’s for Today? •

模擬現實? ∼〜~ Analog Input & Output

電腦的介入∼〜~ ∼〜~ Communication: Computer ⇄ Arduino

Flash?! ∼〜~ Tools help the Arduino talk with Flash.

先到http://wiki.arch.nctu.edu.tw/Arduino/Arduino 下載今天所要用到的上課檔案。

Arduino Let’s Interacting in Arduino


Analog

Arduino Let’s Interacting in Arduino


What is Analog ?

Image via http://www.gearfuse.com/digital-clock-is-all-hands-no-face/

Arduino Let’s Interacting in Arduino


Analog Input

Arduino Let’s Interacting in Arduino


Potentiometer

Arduino Let’s Interacting in Arduino


Photocell

get value

get value

get value

Arduino Let’s Interacting in Arduino


Analog Input

Arduino Let’s Interacting in Arduino


Analog Input int ledPin = 3; int analogPin = 0; int val = 0;

// LED connected to digital pin 2 // photocell connected to analog pin 0

void setup() { pinMode(ledPin, OUTPUT); } void loop() { val = analogRead(analogPin); if(val<80) { digitalWrite(ledPin, HIGH); } else { digitalWrite(ledPin, LOW); } delay(50); }

analogRead(pin) Arduino的Analog Input的值介於0~1023

// sets the digital pin as output

// read the value from the sensor // sets the LED on

// sets the LED off

if...else...

Arduino Let’s Interacting in Arduino


Analog Output PWM (Pulse Width Modulation) 電腦與微處理器是不可能實際輸出類比的電壓(僅能0~5V)。 但我們可以假造出類似的效果。 若快速在兩個電壓中做切換,我們可以得到一個平均值: Output Voltage = High_time(%) * Max_Voltage

Arduino 的PWM pin只有3,5,6,9,10,11

Arduino Let’s Interacting in Arduino


Analog Output

Arduino Letâ&#x20AC;&#x2122;s Interacting in Arduino


Analog Output

int ledPin = 3; int analogPin = 0; int val = 0; void setup() { pinMode(ledPin, OUTPUT); } void loop() { val = analogRead(analogPin); analogWrite(ledPin, val/4); delay(50); }

// LED connected to digital pin 2 // photocell connected to analog pin 0

// sets the digital pin as output

// read the value from the sensor // set the output value 0~255

analogWrite(pin) Arduino的Analog Output的值介於0~255

Arduino Let’s Interacting in Arduino


Communication

Arduino Letâ&#x20AC;&#x2122;s Interacting in Arduino


Serial

Arduino 並不是真的透過USB來跟電腦溝通,而是透過RS-232 Serial的方式。 透過一連串HIGH / LOW的編碼訊號,可以轉換成我們要的訊息:

不論電腦端用什麼軟體,只要能透過Serial port傳送訊息,就可以跟Arduino溝通。 故我們可以用 C/C++,VB, MAX/MSP,VVVV, Processing 或是FLASH(需要第三方軟體的幫助)

Arduino Let’s Interacting in Arduino


Arduino ➝ Computer

Arduino Let’s Interacting in Arduino


Arduino ➝ Computer

baud rate 設定

你要傳的訊息輸入 傳送來的訊息

我們可以先用Arduino Software提供的Serial Monitor來先測試Arduino板子端 是否運作正確。

Arduino Let’s Interacting in Arduino


Arduino â&#x17E;? Computer

int ledPin = 3; int analogPin = 0; int val = 0; void setup() { pinMode(ledPin, OUTPUT); Serial.begin(9600); } void loop() { val = analogRead(analogPin);

// LED connected to digital pin 2 // photocell connected to analog pin 0

// sets the digital pin as output

// read the value from the sensor

Serial.println(val); analogWrite(ledPin, val/4); delay(50);

// set the output value 0~255

}

Serial.begin(speed) , Serial.println(str)

Arduino Letâ&#x20AC;&#x2122;s Interacting in Arduino


Arduino ← Computer

Arduino Let’s Interacting in Arduino


Arduino â&#x17E;? Computer int ledPin = 3; int analogPin = 0; int activeLED = 0; void setup() { pinMode(ledPin, OUTPUT); Serial.begin(9600); }

// LED connected to digital pin 2 // photocell connected to analog pin 0

// sets the digital pin as output

void loop() { if(Serial.available()>0) { activeLED = Serial.read(); if(activeLED=='o') { digitalWrite(ledPin, HIGH); } if(activeLED=='f') { digitalWrite(ledPin, LOW); } } delay(50) }

Serial.availabe() , Serial.read()

Arduino Letâ&#x20AC;&#x2122;s Interacting in Arduino


How about FLASH?

Arduino Letâ&#x20AC;&#x2122;s Interacting in Arduino


Arduino2Flash

Read / Send

Arduino Letâ&#x20AC;&#x2122;s Interacting in Arduino


Arduino2Flash int int int int

ledPin = 3; analogPin = 0; activeLED = 0; val = 0;

void setup() { pinMode(ledPin, OUTPUT); Serial.begin(9600); } void loop() { if(Serial.available()>0) { activeLED = Serial.read(); if(activeLED=='o') { digitalWrite(ledPin, HIGH); } if(activeLED=='f') { digitalWrite(ledPin, LOW); } } val = analogRead(analogPin); printInteger(val); printByte(0);

//print interger //print \0 as end mark of the XML message

delay(50); }

Arduino Letâ&#x20AC;&#x2122;s Interacting in Arduino


Serial Server Serial Server需要java才能執行,故window必須先安裝好java執行環境(MAC OSX已經內建了)。 Win XP: 1. 下載並安裝JRE

2. 下載Serial Server,並解壓縮,解完後,在第一層目錄可以看到ss6.jar,這是執行檔,但我們得先 作好環境設定,到 serialserver/rxtx_drivers/Windows/ 可以看到 RXTXcomm.jar 和 rxtxSerial.dll。

C:\Program Files\Java\j2re1.4.2_04\lib\ext

C:\Program Files\Java\j2re1.4.2_04\bin

3. 找到並點選打開 ss6.jar,將左方的socket改成5333, 並將右方port選為你Arduino板的COM port,下方的數 字改成9600。

Arduino Let’s Interacting in Arduino


FLASH 開啟Flash2Arduino.fla檔案。

Arduino Let’s Interacting in Arduino


FLASH

Arduino Letâ&#x20AC;&#x2122;s Interacting in Arduino


Reference

Web: • Arduino官網

http://www.arduino.cc/

• Arduino Playground

http://www.arduino.cc/playground/

• Arduino 樂園

http://arduino.tw/

• MSM

http://203.68.163.135/msm/

• DesignLab

http://designlab.tw/

• ITP Physical Computing

http://itp.nyu.edu/physcomp/

• sparkfun ELECTRONICS

http://www.sparkfun.com/

• Adafruit Industries

http://www.adafruit.com/

• Thinkerlog

http://tinkerlog.com/

Book: • Pysical Computing: Sensing and Controlling the Physical World with Computer, Tom Iqoe & Dan O’Sullivan. (2004) • 圖解電氣迴路, 稻見辰夫 & 稻見昌彥 著, 宋家豪 & 陳曉梅 譯. (2006)

Arduino Let’s Interacting in Arduino


mao_arduino_02  

my arduino tutorial

Advertisement
Read more
Read more
Similar to
Popular now
Just for you