SlideShare a Scribd company logo
阿爾杜伊諾
    Arduino: Lv. 2




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




        Analog Out
         類比輸出
Analog Output                                         Arduino 的PWM pin只有3,5,6,9,10,11


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

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

        Output Voltage = High_time(%) * Max_Voltage
Arduino 的PWM pin只有3,5,6,9,10,11


類比輸出0~5V對應的數值為0~255
analogWrite( pin, val )
#4 | Fade   實作呼吸燈的效果
#4的練習程式,在 File > Examples > Basic > Fade




int brightness = 0;       // how bright the LED is
int 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);
}
#5 | Fade_and_Blink   實作呼吸燈與LED閃爍共存:活用millis()來進行多工
#5的練習程式,在 http://mutienliao.tw/arduino/Fade_and_Blink.pde
const int ledPin = 13;           // the number of the LED pin for blink
const int fadePin = 9;           // the number of the LED pin for fade

int ledState = LOW;                 // ledState used to set the LED
long 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 is
int 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);
}
輸入才是互動的精華
Analog In




     Analog Input
      類比輸入
Potentiometer
Photocell




     get value         get value




                 get value
Arduino 的類比輸入使用A0~A5


類比輸入0~5V對應的數值為0~1023
analogRead( pin )
#10 | analog_control
#10的練習程式,在 http://mutienliao.tw/arduino/analog_control.pde




int ledPin = 13;                   // LED connected to digital pin 13
int analogPin = 0;                 // photocell connected to analog pin 0
int 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的練習程式,取得analogRead進來的數值




int ledPin = 13;                   // LED connected to digital pin 13
int analogPin = 0;                 // photocell connected to analog pin 0
int 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);
}
我們可以先用Arduino Software提供的Serial Monitor來先測試Arduino板子端
傳來的訊息。




                                            你要傳的訊息輸入

     傳送來的訊息

              546756456575456745674567447




                                                       baud rate 設定
#11 | AnalogInOutSerial
#11的練習程式,在 File > Examples > Analog > AnalogInOutSerial



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

int sensorValue = 0;         // value read from the pot
int 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);
}
#10 | analog_control
#10的練習程式, File > Examples > Analog > AnalogInput




int sensorPin = A0;    // select the input pin for the potentiometer
int ledPin = 13;       // select the pin for the LED
int 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的練習程式,取得analogRead進來的數值




int sensorPin = A0;    // select the input pin for the potentiometer
int ledPin = 13;       // select the pin for the LED
int 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);
}
#11 | AnalogInOutSerial
#11的練習程式,在 File > Examples > Analog > AnalogInOutSerial




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

int sensorValue = 0;         // value read from the pot
int 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);
}
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
[Arduino] 在 File > Examples > Communication > Graph
#14 | Arduino to Processing
                              [Processing] 在 http://mutienliao.tw/processing/Graph.pde
Processing...
[Arduino] 在 File > Examples > Communication > Dimmer
#15 | Processing to Arduino
                              [Processing] 在 http://mutienliao.tw/processing/Dimmer.pde
Processing...

More Related Content

PDF
Mao arduino
PDF
Arduino Workshop 2011.05.31
PDF
Programming arduino makeymakey
PDF
Arduino workshop
PDF
Etapes fab-venti-v2
PPTX
Arduino cic3
PPTX
Arduino programming
PDF
Cassiopeia Ltd - standard Arduino workshop
Mao arduino
Arduino Workshop 2011.05.31
Programming arduino makeymakey
Arduino workshop
Etapes fab-venti-v2
Arduino cic3
Arduino programming
Cassiopeia Ltd - standard Arduino workshop

What's hot (20)

PDF
Up and running with Teensy 3.1
KEY
Intro to Arduino
PDF
DSL Junior Makers - electronics workshop
PDF
Arduino Lecture 3 - Interactive Media CS4062 Semester 2 2009
PDF
Arduino 8-step drum sequencer 3 channels
PPTX
Arduino Day 1 Presentation
PPTX
Arduino Programming
TXT
Arduino code
DOCX
Arduino windows remote control
DOCX
Basic arduino sketch example
DOCX
All about ir arduino - cool
PPTX
Arduino Workshop Day 2
PDF
Starting with Arduino
DOCX
Direct analog
PDF
arduino
PDF
Aurduino coding for transformer interfacing
PPTX
Hackathon Taiwan 5th : Arduino 101
PPTX
LED Cube Presentation Slides
ODP
Fpga creating counter with external clock
PDF
The IoT Academy IoT training Arduino Part 2 Arduino IDE
Up and running with Teensy 3.1
Intro to Arduino
DSL Junior Makers - electronics workshop
Arduino Lecture 3 - Interactive Media CS4062 Semester 2 2009
Arduino 8-step drum sequencer 3 channels
Arduino Day 1 Presentation
Arduino Programming
Arduino code
Arduino windows remote control
Basic arduino sketch example
All about ir arduino - cool
Arduino Workshop Day 2
Starting with Arduino
Direct analog
arduino
Aurduino coding for transformer interfacing
Hackathon Taiwan 5th : Arduino 101
LED Cube Presentation Slides
Fpga creating counter with external clock
The IoT Academy IoT training Arduino Part 2 Arduino IDE
Ad

Viewers also liked (20)

PDF
Comprehensive Waag Society Overview
PDF
LASS的下一步: 從環境感測到環境教育
PDF
Optimal Forms
PPT
Analog Devices B Inc
PPT
Analog Devices A Inc
PPTX
EDF2014: Ralf-Peter Schaefer, Head of Traffic Product Unit, TomTom, Germany: ...
PPTX
Open Innovation Methodologies @Waag Society
PPTX
EDF2014: Talk of Frank Kresin, Research Director, Waag Society, Netherlands: ...
PDF
Plc analog Tutorial
PDF
PLC input and output devices
PPT
Sensors And Actuators
PDF
[系列活動] 機器學習速遊
ODP
Challenges in raising the social and economic impact of Open Data Policy in B...
PDF
What Makes Great Infographics
PDF
Masters of SlideShare
PDF
STOP! VIEW THIS! 10-Step Checklist When Uploading to Slideshare
PDF
You Suck At PowerPoint!
PDF
10 Ways to Win at SlideShare SEO & Presentation Optimization
PDF
How To Get More From SlideShare - Super-Simple Tips For Content Marketing
PDF
A Guide to SlideShare Analytics - Excerpts from Hubspot's Step by Step Guide ...
Comprehensive Waag Society Overview
LASS的下一步: 從環境感測到環境教育
Optimal Forms
Analog Devices B Inc
Analog Devices A Inc
EDF2014: Ralf-Peter Schaefer, Head of Traffic Product Unit, TomTom, Germany: ...
Open Innovation Methodologies @Waag Society
EDF2014: Talk of Frank Kresin, Research Director, Waag Society, Netherlands: ...
Plc analog Tutorial
PLC input and output devices
Sensors And Actuators
[系列活動] 機器學習速遊
Challenges in raising the social and economic impact of Open Data Policy in B...
What Makes Great Infographics
Masters of SlideShare
STOP! VIEW THIS! 10-Step Checklist When Uploading to Slideshare
You Suck At PowerPoint!
10 Ways to Win at SlideShare SEO & Presentation Optimization
How To Get More From SlideShare - Super-Simple Tips For Content Marketing
A Guide to SlideShare Analytics - Excerpts from Hubspot's Step by Step Guide ...
Ad

Similar to Arduino: Analog I/O (20)

PPT
01 Intro to the Arduino and it's basics.ppt
PPTX
Arduino.pptx
PPTX
INTODUCTION OF IOT AND INTERFACING WITH ARDUINO UNO
PPTX
Introduction to arduino Programming with
PPT
Physical prototyping lab2-analog_digital
PPT
Physical prototyping lab2-analog_digital
KEY
Scottish Ruby Conference 2010 Arduino, Ruby RAD
PPTX
INTRODUCTION TO ARDUINO and sensors for arduino.pptx
PDF
Arduino based applications part 1
PDF
Arduino uno basic Experiments for beginner
PDF
4 IOT 18ISDE712 MODULE 4 IoT Physical Devices and End Point-Aurdino Uno.pdf
PPSX
Arduino اردوينو
PDF
L6 Visual Output LED_7SEGMEN_LEDMATRIX upate.pdf
PDF
L6 Visual Output LED_7SEGMEN_LEDMATRIX upate.pdf
PPTX
Arduino Workshop (3).pptx
PPTX
Fun with arduino
PPTX
Arduino intro.pptx
PPTX
Led fade
PPTX
Mom presentation_monday_arduino in the physics lab
01 Intro to the Arduino and it's basics.ppt
Arduino.pptx
INTODUCTION OF IOT AND INTERFACING WITH ARDUINO UNO
Introduction to arduino Programming with
Physical prototyping lab2-analog_digital
Physical prototyping lab2-analog_digital
Scottish Ruby Conference 2010 Arduino, Ruby RAD
INTRODUCTION TO ARDUINO and sensors for arduino.pptx
Arduino based applications part 1
Arduino uno basic Experiments for beginner
4 IOT 18ISDE712 MODULE 4 IoT Physical Devices and End Point-Aurdino Uno.pdf
Arduino اردوينو
L6 Visual Output LED_7SEGMEN_LEDMATRIX upate.pdf
L6 Visual Output LED_7SEGMEN_LEDMATRIX upate.pdf
Arduino Workshop (3).pptx
Fun with arduino
Arduino intro.pptx
Led fade
Mom presentation_monday_arduino in the physics lab

More from June-Hao Hou (12)

PDF
Processing Basics 1
PDF
Arduino: Intro and Digital I/O
PDF
跨領域設計
PDF
ETH+NCTU workshop 0419 Kyle
PDF
ETH+NCTU workshop 0412 ETH
PDF
ETH+NCTU workshop 0412 Kyle group
PDF
ETH+NCTU workshop 0412 MS
PDF
Taiwanese Tea culture
PDF
ETH_NCTU_Workshop_0405_Kyle
PDF
ETH_NCTU_Workshop_0405_MS
PDF
ETH_NCTU_Workshop_0329
PDF
Design Collaboration: Harvard-NCTU Experiences
Processing Basics 1
Arduino: Intro and Digital I/O
跨領域設計
ETH+NCTU workshop 0419 Kyle
ETH+NCTU workshop 0412 ETH
ETH+NCTU workshop 0412 Kyle group
ETH+NCTU workshop 0412 MS
Taiwanese Tea culture
ETH_NCTU_Workshop_0405_Kyle
ETH_NCTU_Workshop_0405_MS
ETH_NCTU_Workshop_0329
Design Collaboration: Harvard-NCTU Experiences

Recently uploaded (20)

PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
Pre independence Education in Inndia.pdf
PPTX
Pharma ospi slides which help in ospi learning
PDF
RMMM.pdf make it easy to upload and study
PPTX
Institutional Correction lecture only . . .
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
01-Introduction-to-Information-Management.pdf
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
Insiders guide to clinical Medicine.pdf
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PDF
Computing-Curriculum for Schools in Ghana
PPTX
Cell Structure & Organelles in detailed.
PPTX
PPH.pptx obstetrics and gynecology in nursing
PDF
Basic Mud Logging Guide for educational purpose
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
Renaissance Architecture: A Journey from Faith to Humanism
STATICS OF THE RIGID BODIES Hibbelers.pdf
Pre independence Education in Inndia.pdf
Pharma ospi slides which help in ospi learning
RMMM.pdf make it easy to upload and study
Institutional Correction lecture only . . .
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
Module 4: Burden of Disease Tutorial Slides S2 2025
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
01-Introduction-to-Information-Management.pdf
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Insiders guide to clinical Medicine.pdf
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
Computing-Curriculum for Schools in Ghana
Cell Structure & Organelles in detailed.
PPH.pptx obstetrics and gynecology in nursing
Basic Mud Logging Guide for educational purpose

Arduino: Analog I/O

  • 1. 阿爾杜伊諾 Arduino: Lv. 2 Mutienliao.TW 智慧生活與創新設計, 2013-03-25
  • 3. Analog Out Analog Out 類比輸出
  • 4. Analog Output Arduino 的PWM pin只有3,5,6,9,10,11 PWM (Pulse Width Modulation) 電腦與微處理器是不可能實際輸出類比的電壓(僅能0~5V)。 但我們可以假造出類似的效果。 若快速在兩個電壓中做切換,我們可以得到⼀一個平均值: Output Voltage = High_time(%) * Max_Voltage
  • 6. #4 | Fade 實作呼吸燈的效果
  • 7. #4的練習程式,在 File > Examples > Basic > Fade int brightness = 0; // how bright the LED is int 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); }
  • 8. #5 | Fade_and_Blink 實作呼吸燈與LED閃爍共存:活用millis()來進行多工
  • 9. #5的練習程式,在 http://mutienliao.tw/arduino/Fade_and_Blink.pde const int ledPin = 13; // the number of the LED pin for blink const int fadePin = 9; // the number of the LED pin for fade int ledState = LOW; // ledState used to set the LED long 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 is int 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); }
  • 11. Analog In Analog Input 類比輸入
  • 13. Photocell get value get value get value
  • 16. #10的練習程式,在 http://mutienliao.tw/arduino/analog_control.pde int ledPin = 13; // LED connected to digital pin 13 int analogPin = 0; // photocell connected to analog pin 0 int 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. 修改#10的練習程式,取得analogRead進來的數值 int ledPin = 13; // LED connected to digital pin 13 int analogPin = 0; // photocell connected to analog pin 0 int 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. 我們可以先用Arduino Software提供的Serial Monitor來先測試Arduino板子端 傳來的訊息。 你要傳的訊息輸入 傳送來的訊息 546756456575456745674567447 baud rate 設定
  • 20. #11的練習程式,在 File > Examples > Analog > AnalogInOutSerial const int analogInPin = A0; // Analog input pin that the potentiometer is attached to const int analogOutPin = 9; // Analog output pin that the LED is attached to int sensorValue = 0; // value read from the pot int 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); }
  • 22. #10的練習程式, File > Examples > Analog > AnalogInput int sensorPin = A0; // select the input pin for the potentiometer int ledPin = 13; // select the pin for the LED int 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); }
  • 23. 修改#10的練習程式,取得analogRead進來的數值 int sensorPin = A0; // select the input pin for the potentiometer int ledPin = 13; // select the pin for the LED int 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); }
  • 25. #11的練習程式,在 File > Examples > Analog > AnalogInOutSerial const int analogInPin = A0; // Analog input pin that the potentiometer is attached to const int analogOutPin = 9; // Analog output pin that the LED is attached to int sensorValue = 0; // value read from the pot int 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); }
  • 27. Arduino 並不是真的透過USB來跟電腦溝通,而是透過RS-232 Serial的方式。 透過⼀一連串HIGH / LOW的編碼訊號,可以轉換成我們要的訊息: 不論電腦端用什麼軟體,只要能透過Serial port傳送訊息,就可以跟Arduino溝通。 故我們可以用 C/C++,VB, MAX/MSP,VVVV, Processing 或是FLASH(需要第三方軟體的幫助)
  • 28. #12 | PC to Arduino #12的練習程式,在File > Example > Communication > PhysicalPixel
  • 29. #13 | PC to Arduino #13的練習程式,在 http://mutienliao.tw/arduino/PC_to_Arduino_analog.pde
  • 30. [Arduino] 在 File > Examples > Communication > Graph #14 | Arduino to Processing [Processing] 在 http://mutienliao.tw/processing/Graph.pde
  • 32. [Arduino] 在 File > Examples > Communication > Dimmer #15 | Processing to Arduino [Processing] 在 http://mutienliao.tw/processing/Dimmer.pde