阿爾杜伊諾    Arduino: Lv. 2Mutienliao.TW 智慧生活與創新設計, 2013-03-25
ArduinoIntroduction
Analog Out        Analog Out         類比輸出
Analog Output                                         Arduino 的PWM pin只有3,5,6,9,10,11 PWM (Pulse Width Modulation) 電腦與微處理器...
Arduino 的PWM pin只有3,5,6,9,10,11類比輸出0~5V對應的數值為0~255analogWrite( pin, val )
#4 | Fade   實作呼吸燈的效果
#4的練習程式,在 File > Examples > Basic > Fadeint brightness = 0;       // how bright the LED isint fadeAmount = 5;       // how...
#5 | Fade_and_Blink   實作呼吸燈與LED閃爍共存:活用millis()來進行多工
#5的練習程式,在 http://mutienliao.tw/arduino/Fade_and_Blink.pdeconst int ledPin = 13;           // the number of the LED pin for...
輸入才是互動的精華
Analog In     Analog Input      類比輸入
Potentiometer
Photocell     get value         get value                 get value
Arduino 的類比輸入使用A0~A5類比輸入0~5V對應的數值為0~1023analogRead( pin )
#10 | analog_control
#10的練習程式,在 http://mutienliao.tw/arduino/analog_control.pdeint ledPin = 13;                   // LED connected to digital p...
修改#10的練習程式,取得analogRead進來的數值int ledPin = 13;                   // LED connected to digital pin 13int analogPin = 0;       ...
我們可以先用Arduino Software提供的Serial Monitor來先測試Arduino板子端傳來的訊息。                                            你要傳的訊息輸入     傳送來的訊息...
#11 | AnalogInOutSerial
#11的練習程式,在 File > Examples > Analog > AnalogInOutSerialconst int analogInPin = A0; // Analog input pin that the potentiome...
#10 | analog_control
#10的練習程式, File > Examples > Analog > AnalogInputint sensorPin = A0;    // select the input pin for the potentiometerint le...
修改#10的練習程式,取得analogRead進來的數值int sensorPin = A0;    // select the input pin for the potentiometerint ledPin = 13;       // ...
#11 | AnalogInOutSerial
#11的練習程式,在 File > Examples > Analog > AnalogInOutSerialconst int analogInPin = A0; // Analog input pin that the potentiome...
CommunicationCommunication   溝通
Arduino 並不是真的透過USB來跟電腦溝通,而是透過RS-232 Serial的方式。透過⼀一連串HIGH / LOW的編碼訊號,可以轉換成我們要的訊息:不論電腦端用什麼軟體,只要能透過Serial port傳送訊息,就可以跟Arduin...
#12 | PC to Arduino   #12的練習程式,在File > Example > Communication > PhysicalPixel
#13 | PC to Arduino   #13的練習程式,在 http://mutienliao.tw/arduino/PC_to_Arduino_analog.pde
[Arduino] 在 File > Examples > Communication > Graph#14 | Arduino to Processing                              [Processing] 在...
Processing...
[Arduino] 在 File > Examples > Communication > Dimmer#15 | Processing to Arduino                              [Processing] ...
Processing...
Upcoming SlideShare
Loading in...5
×

Arduino: Analog I/O

2,145

Published on

Published in: Education
0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
2,145
On Slideshare
0
From Embeds
0
Number of Embeds
5
Actions
Shares
0
Downloads
72
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide

Transcript of "Arduino: Analog I/O"

  1. 1. 阿爾杜伊諾 Arduino: Lv. 2Mutienliao.TW 智慧生活與創新設計, 2013-03-25
  2. 2. ArduinoIntroduction
  3. 3. Analog Out Analog Out 類比輸出
  4. 4. Analog Output Arduino 的PWM pin只有3,5,6,9,10,11 PWM (Pulse Width Modulation) 電腦與微處理器是不可能實際輸出類比的電壓(僅能0~5V)。 但我們可以假造出類似的效果。 若快速在兩個電壓中做切換,我們可以得到⼀一個平均值: Output Voltage = High_time(%) * Max_Voltage
  5. 5. Arduino 的PWM pin只有3,5,6,9,10,11類比輸出0~5V對應的數值為0~255analogWrite( pin, val )
  6. 6. #4 | Fade 實作呼吸燈的效果
  7. 7. #4的練習程式,在 File > Examples > Basic > Fadeint brightness = 0; // how bright the LED isint fadeAmount = 5; // how many points to fade the LED byvoid 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);}
  8. 8. #5 | Fade_and_Blink 實作呼吸燈與LED閃爍共存:活用millis()來進行多工
  9. 9. #5的練習程式,在 http://mutienliao.tw/arduino/Fade_and_Blink.pdeconst int ledPin = 13; // the number of the LED pin for blinkconst int fadePin = 9; // the number of the LED pin for fadeint ledState = LOW; // ledState used to set the LEDlong previousMillis = 0; // will store last time LED was updatedlong 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 byvoid 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);}
  10. 10. 輸入才是互動的精華
  11. 11. Analog In Analog Input 類比輸入
  12. 12. Potentiometer
  13. 13. Photocell get value get value get value
  14. 14. Arduino 的類比輸入使用A0~A5類比輸入0~5V對應的數值為0~1023analogRead( pin )
  15. 15. #10 | analog_control
  16. 16. #10的練習程式,在 http://mutienliao.tw/arduino/analog_control.pdeint 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);}
  17. 17. 修改#10的練習程式,取得analogRead進來的數值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);}
  18. 18. 我們可以先用Arduino Software提供的Serial Monitor來先測試Arduino板子端傳來的訊息。 你要傳的訊息輸入 傳送來的訊息 546756456575456745674567447 baud rate 設定
  19. 19. #11 | AnalogInOutSerial
  20. 20. #11的練習程式,在 File > Examples > Analog > AnalogInOutSerialconst int analogInPin = A0; // Analog input pin that the potentiometer is attached toconst int analogOutPin = 9; // Analog output pin that the LED is attached toint 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);}
  21. 21. #10 | analog_control
  22. 22. #10的練習程式, File > Examples > Analog > AnalogInputint 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 sensorvoid 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);}
  23. 23. 修改#10的練習程式,取得analogRead進來的數值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 sensorvoid 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);}
  24. 24. #11 | AnalogInOutSerial
  25. 25. #11的練習程式,在 File > Examples > Analog > AnalogInOutSerialconst int analogInPin = A0; // Analog input pin that the potentiometer is attached toconst int analogOutPin = 9; // Analog output pin that the LED is attached toint 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);}
  26. 26. CommunicationCommunication 溝通
  27. 27. Arduino 並不是真的透過USB來跟電腦溝通,而是透過RS-232 Serial的方式。透過⼀一連串HIGH / LOW的編碼訊號,可以轉換成我們要的訊息:不論電腦端用什麼軟體,只要能透過Serial port傳送訊息,就可以跟Arduino溝通。故我們可以用 C/C++,VB, MAX/MSP,VVVV, Processing 或是FLASH(需要第三方軟體的幫助)
  28. 28. #12 | PC to Arduino #12的練習程式,在File > Example > Communication > PhysicalPixel
  29. 29. #13 | PC to Arduino #13的練習程式,在 http://mutienliao.tw/arduino/PC_to_Arduino_analog.pde
  30. 30. [Arduino] 在 File > Examples > Communication > Graph#14 | Arduino to Processing [Processing] 在 http://mutienliao.tw/processing/Graph.pde
  31. 31. Processing...
  32. 32. [Arduino] 在 File > Examples > Communication > Dimmer#15 | Processing to Arduino [Processing] 在 http://mutienliao.tw/processing/Dimmer.pde
  33. 33. Processing...
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×