阿爾杜伊諾 Arduino: Lv. 2 Mutienliao.TW 智慧生活與創新設計, 2013-03-25
Aug 31, 2014
阿爾杜伊諾Arduino: Lv. 2
Mutienliao.TW 智慧生活與創新設計, 2013-03-25
ArduinoIntroduction
Analog Out類比輸出
Analog Out
Analog Output
PWM (Pulse Width Modulation)
電腦與微處理器是不可能實際輸出類比的電壓(僅能0~5V)。
但我們可以假造出類似的效果。若快速在兩個電壓中做切換,我們可以得到⼀一個平均值:
Output Voltage = High_time(%) * Max_Voltage
Arduino 的PWM pin只有3,5,6,9,10,11
Arduino 的PWM pin只有3,5,6,9,10,11
類比輸出0~5V對應的數值為0~255analogWrite( pin, val )
#4 | Fade 實作呼吸燈的效果
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
#5 | Fade_and_Blink 實作呼吸燈與LED閃爍共存:活用millis()來進行多工
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
輸入才是互動的精華
Analog Input類比輸入
Analog In
Potentiometer
Photocell
get value get value
get value
Arduino 的類比輸入使用A0~A5
類比輸入0~5V對應的數值為0~1023analogRead( pin )
#10 | analog_control
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
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進來的數值
我們可以先用Arduino Software提供的Serial Monitor來先測試Arduino板子端傳來的訊息。
546756456575456745674567447
baud rate 設定
你要傳的訊息輸入
傳送來的訊息
#11 | AnalogInOutSerial
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
#10 | analog_control
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
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進來的數值
#11 | AnalogInOutSerial
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
Communication溝通
Communication
Arduino 並不是真的透過USB來跟電腦溝通,而是透過RS-232 Serial的方式。
透過⼀一連串HIGH / LOW的編碼訊號,可以轉換成我們要的訊息:
不論電腦端用什麼軟體,只要能透過Serial port傳送訊息,就可以跟Arduino溝通。故我們可以用 C/C++, VB, MAX/MSP, VVVV, Processing 或是FLASH(需要第三方軟體的幫助)
#12 | PC to Arduino #12的練習程式,在File > Example > Communication > PhysicalPixel
#13 | PC to Arduino #13的練習程式,在 http://mutienliao.tw/arduino/PC_to_Arduino_analog.pde
#14 | Arduino to Processing [Processing] 在 http://mutienliao.tw/processing/Graph.pde
[Arduino] 在 File > Examples > Communication > Graph
Processing...
#15 | Processing to Arduino[Processing] 在 http://mutienliao.tw/processing/Dimmer.pde
[Arduino] 在 File > Examples > Communication > Dimmer
Processing...