Top Banner
阿爾杜伊諾 Arduino: Lv. 2 Mutienliao.TW 智慧生活與創新設計, 2013-03-25
33

Arduino: Analog I/O

Aug 31, 2014

Download

Education

June-Hao Hou

 
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Arduino: Analog I/O

阿爾杜伊諾Arduino: Lv. 2

Mutienliao.TW 智慧生活與創新設計, 2013-03-25

Page 2: Arduino: Analog I/O

ArduinoIntroduction

Page 3: Arduino: Analog I/O

Analog Out類比輸出

Analog Out

Page 4: Arduino: Analog I/O

Analog Output

PWM (Pulse Width Modulation)

電腦與微處理器是不可能實際輸出類比的電壓(僅能0~5V)。

但我們可以假造出類似的效果。若快速在兩個電壓中做切換,我們可以得到⼀一個平均值:

Output Voltage = High_time(%) * Max_Voltage

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

Page 5: Arduino: Analog I/O

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

類比輸出0~5V對應的數值為0~255analogWrite( pin, val )

Page 6: Arduino: Analog I/O

#4 | Fade 實作呼吸燈的效果

Page 7: Arduino: Analog I/O

int brightness = 0; // how bright the LED isint fadeAmount = 5; // how many points to fade the LED by

void setup() { // declare pin 9 to be an output: pinMode(9, OUTPUT);}

void loop() { // set the brightness of pin 9: analogWrite(9, brightness);

// change the brightness for next time through the loop: brightness = brightness + fadeAmount;

// reverse the direction of the fading at the ends of the fade: if (brightness == 0 || brightness == 255) { fadeAmount = -fadeAmount ; } // wait for 30 milliseconds to see the dimming effect delay(30); }

#4的練習程式,在 File > Examples > Basic > Fade

Page 8: Arduino: Analog I/O

#5 | Fade_and_Blink 實作呼吸燈與LED閃爍共存:活用millis()來進行多工

Page 9: Arduino: Analog I/O

const int ledPin = 13; // the number of the LED pin for blinkconst int fadePin = 9; // the number of the LED pin for fade

int ledState = LOW; // ledState used to set the LEDlong previousMillis = 0; // will store last time LED was updated

long interval = 1000; // interval at which to blink (milliseconds)

int brightness = 0; // how bright the LED isint fadeAmount = 5; // how many points to fade the LED by

void setup() { pinMode(ledPin, OUTPUT); pinMode(fadePin, OUTPUT);}

void loop(){ unsigned long currentMillis = millis(); if(currentMillis - previousMillis > interval) { previousMillis = currentMillis; if (ledState == LOW) ledState = HIGH; else ledState = LOW;

digitalWrite(ledPin, ledState); } analogWrite(fadePin, brightness); brightness = brightness + fadeAmount; if (brightness == 0 || brightness == 255) { fadeAmount = -fadeAmount ; } delay(30); }

#5的練習程式,在 http://mutienliao.tw/arduino/Fade_and_Blink.pde

Page 10: Arduino: Analog I/O

輸入才是互動的精華

Page 11: Arduino: Analog I/O

Analog Input類比輸入

Analog In

Page 12: Arduino: Analog I/O

Potentiometer

Page 13: Arduino: Analog I/O

Photocell

get value get value

get value

Page 14: Arduino: Analog I/O

Arduino 的類比輸入使用A0~A5

類比輸入0~5V對應的數值為0~1023analogRead( pin )

Page 15: Arduino: Analog I/O

#10 | analog_control

Page 16: Arduino: Analog I/O

int ledPin = 13; // LED connected to digital pin 13int analogPin = 0; // photocell connected to analog pin 0int val = 0;

void setup(){ pinMode(ledPin, OUTPUT); // sets the digital pin as output}

void loop(){ val = analogRead(analogPin); // read the value from the sensor if(val<80) { digitalWrite(ledPin, HIGH); // sets the LED on } else { digitalWrite(ledPin, LOW); // sets the LED off } delay(50); }

#10的練習程式,在 http://mutienliao.tw/arduino/analog_control.pde

Page 17: Arduino: Analog I/O

int ledPin = 13; // LED connected to digital pin 13int analogPin = 0; // photocell connected to analog pin 0int val = 0;

void setup(){ pinMode(ledPin, OUTPUT); // sets the digital pin as output Serial.begin(9600);}

void loop(){ val = analogRead(analogPin); // read the value from the sensor Serial.println(val); if(val<80) { digitalWrite(ledPin, HIGH); // sets the LED on } else { digitalWrite(ledPin, LOW); // sets the LED off } delay(50); }

修改#10的練習程式,取得analogRead進來的數值

Page 18: Arduino: Analog I/O

我們可以先用Arduino Software提供的Serial Monitor來先測試Arduino板子端傳來的訊息。

546756456575456745674567447

baud rate 設定

你要傳的訊息輸入

傳送來的訊息

Page 19: Arduino: Analog I/O

#11 | AnalogInOutSerial

Page 20: Arduino: Analog I/O

const int analogInPin = A0; // Analog input pin that the potentiometer is attached toconst int analogOutPin = 9; // Analog output pin that the LED is attached to

int sensorValue = 0; // value read from the potint outputValue = 0; // value output to the PWM (analog out)

void setup() { // initialize serial communications at 9600 bps: Serial.begin(9600); }

void loop() { // read the analog in value: sensorValue = analogRead(analogInPin); // map it to the range of the analog out: outputValue = map(sensorValue, 0, 1023, 0, 255); // change the analog out value: analogWrite(analogOutPin, outputValue);

// print the results to the serial monitor: Serial.print("sensor = " ); Serial.print(sensorValue); Serial.print("\t output = "); Serial.println(outputValue);

// wait 10 milliseconds before the next loop // for the analog-to-digital converter to settle // after the last reading: delay(10); }

#11的練習程式,在 File > Examples > Analog > AnalogInOutSerial

Page 21: Arduino: Analog I/O

#10 | analog_control

Page 22: Arduino: Analog I/O

int sensorPin = A0; // select the input pin for the potentiometerint ledPin = 13; // select the pin for the LEDint sensorValue = 0; // variable to store the value coming from the sensor

void setup() { // declare the ledPin as an OUTPUT: pinMode(ledPin, OUTPUT); }

void loop() { // read the value from the sensor: sensorValue = analogRead(sensorPin); // turn the ledPin on digitalWrite(ledPin, HIGH); // stop the program for <sensorValue> milliseconds: delay(sensorValue); // turn the ledPin off: digitalWrite(ledPin, LOW); // stop the program for for <sensorValue> milliseconds: delay(sensorValue); }

#10的練習程式, File > Examples > Analog > AnalogInput

Page 23: Arduino: Analog I/O

int sensorPin = A0; // select the input pin for the potentiometerint ledPin = 13; // select the pin for the LEDint sensorValue = 0; // variable to store the value coming from the sensor

void setup() { // declare the ledPin as an OUTPUT: pinMode(ledPin, OUTPUT); Serial.begin(9600);}

void loop() { // read the value from the sensor: sensorValue = analogRead(sensorPin); Serial.println(sensorValue); // turn the ledPin on digitalWrite(ledPin, HIGH); // stop the program for <sensorValue> milliseconds: delay(sensorValue); // turn the ledPin off: digitalWrite(ledPin, LOW); // stop the program for for <sensorValue> milliseconds: delay(sensorValue); }

修改#10的練習程式,取得analogRead進來的數值

Page 24: Arduino: Analog I/O

#11 | AnalogInOutSerial

Page 25: Arduino: Analog I/O

const int analogInPin = A0; // Analog input pin that the potentiometer is attached toconst int analogOutPin = 9; // Analog output pin that the LED is attached to

int sensorValue = 0; // value read from the potint outputValue = 0; // value output to the PWM (analog out)

void setup() { // initialize serial communications at 9600 bps: Serial.begin(9600); }

void loop() { // read the analog in value: sensorValue = analogRead(analogInPin); // map it to the range of the analog out: outputValue = map(sensorValue, 0, 1023, 0, 255); // change the analog out value: analogWrite(analogOutPin, outputValue);

// print the results to the serial monitor: Serial.print("sensor = " ); Serial.print(sensorValue); Serial.print("\t output = "); Serial.println(outputValue);

// wait 10 milliseconds before the next loop // for the analog-to-digital converter to settle // after the last reading: delay(10); }

#11的練習程式,在 File > Examples > Analog > AnalogInOutSerial

Page 26: Arduino: Analog I/O

Communication溝通

Communication

Page 27: Arduino: Analog I/O

Arduino 並不是真的透過USB來跟電腦溝通,而是透過RS-232 Serial的方式。

透過⼀一連串HIGH / LOW的編碼訊號,可以轉換成我們要的訊息:

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

Page 28: Arduino: Analog I/O

#12 | PC to Arduino #12的練習程式,在File > Example > Communication > PhysicalPixel

Page 29: Arduino: Analog I/O

#13 | PC to Arduino #13的練習程式,在 http://mutienliao.tw/arduino/PC_to_Arduino_analog.pde

Page 30: Arduino: Analog I/O

#14 | Arduino to Processing [Processing] 在 http://mutienliao.tw/processing/Graph.pde

[Arduino] 在 File > Examples > Communication > Graph

Page 31: Arduino: Analog I/O

Processing...

Page 32: Arduino: Analog I/O

#15 | Processing to Arduino[Processing] 在 http://mutienliao.tw/processing/Dimmer.pde

[Arduino] 在 File > Examples > Communication > Dimmer

Page 33: Arduino: Analog I/O

Processing...