Arduino Let’s Interacting in Arduino

[ Class 2 ]

April 15 2008 , MAO

what’s for Today? •

Flash？！ ∼〜～ Tools help the Arduino talk with Flash.

Arduino Let’s Interacting in Arduino

Analog

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

What is Analog ?

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

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

Analog Input

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

Potentiometer

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

Photocell

get value

get value

get value

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

Analog Input

Arduino Letâ&#x20AC;&#x2122;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的編碼訊號，可以轉換成我們要的訊息：

Arduino Let’s Interacting in Arduino

Arduino ➝ Computer

Arduino Let’s Interacting in Arduino

Arduino ➝ Computer

baud rate 設定

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
mao_arduino_02

my arduino tutorial

Advertisement