Main Menu

Recent posts

#11
AVR and Arduino / Re: Essentials
Last post by tha - November 29, 2023, 10:44:14 AM
https://docs.arduino.cc/built-in-examples/analog/Smoothing

Smoothing Readings From an Analog Input

การอ่าน an analog input หลายครั้งราบรื่น.

LAST REVISION: 15/11/2566 22:52

sketch นี้จะอ่านซ้ำๆ จาก an analog input, โดยคำนวณ a running average และพิมพ์มันลงในคอมพิวเตอร์ ตัวอย่างนี้มีประโยชน์ในการปรับค่าจากเซ็นเซอร์ที่ไม่สม่ำเสมอหรือไม่แน่นอน และยังสาธิตการใช้อาร์เรย์ในการจัดเก็บข้อมูลอีกด้วย

Hardware

  •  Arduino Board
  •  10k ohm potentiometer

Circuit



ต่อพินหนึ่งของ a potentiometer เข้ากับ 5V, the center pin เข้ากับ analog pin 0, และพินสุดท้ายเข้ากับ ground.

Schematic



Code

โค้ดด้านล่างจะเก็บค่าที่อ่านได้ 10 ค่าจาก analog sensor ของคุณลงในอาร์เรย์ตามลำดับ ทีละค่า ด้วยแต่ละค่าใหม่ ผลรวมของตัวเลขทั้งหมดจะถูกสร้างและหาร ทำให้เกิดค่าเฉลี่ยซึ่งจากนั้นจะนำไปใช้เพื่อทำให้ข้อมูลภายนอกราบรื่นขึ้น เนื่องจากการเฉลี่ยนี้เกิดขึ้นทุกครั้งที่ค่าใหม่ถูกเพิ่มลงในอาร์เรย์ (เช่น แทนที่จะรอค่าใหม่ 10 ค่า) จึงไม่มีเวลาล่าช้าในการคำนวณ running average นี้

การเปลี่ยนขนาดของอาร์เรย์ที่ใช้โดยการเปลี่ยน numReadings เป็นค่าที่มากขึ้นจะทำให้ข้อมูลที่รวบรวมเรียบยิ่งขึ้น

/*
  Smoothing

  Reads repeatedly from an analog input, calculating a running average and
  printing it to the computer. Keeps ten readings in an array and continually
  averages them.

  The circuit:
  - analog sensor (potentiometer will do) attached to analog input 0

  created 22 Apr 2007
  by David A. Mellis  <dam@mellis.org>
  modified 9 Apr 2012
  by Tom Igoe

  This example code is in the public domain.

  https://www.arduino.cc/en/Tutorial/BuiltInExamples/Smoothing
*/

// Define the number of samples to keep track of. The higher the number, the
// more the readings will be smoothed, but the slower the output will respond to
// the input. Using a constant rather than a normal variable lets us use this
// value to determine the size of the readings array.
const int numReadings = 10;

int readings[numReadings];  // the readings from the analog input
int readIndex = 0;          // the index of the current reading
int total = 0;              // the running total
int average = 0;            // the average

int inputPin = A0;

void setup() {
  // initialize serial communication with computer:
  Serial.begin(9600);
  // initialize all the readings to 0:
  for (int thisReading = 0; thisReading < numReadings; thisReading++) {
    readings[thisReading] = 0;
  }
}

void loop() {
  // subtract the last reading:
  total = total - readings[readIndex];
  // read from the sensor:
  readings[readIndex] = analogRead(inputPin);
  // add the reading to the total:
  total = total + readings[readIndex];
  // advance to the next position in the array:
  readIndex = readIndex + 1;

  // if we're at the end of the array...
  if (readIndex >= numReadings) {
    // ...wrap around to the beginning:
    readIndex = 0;
  }

  // calculate the average:
  average = total / numReadings;
  // send it to the computer as ASCII digits
  Serial.println(average);
  delay(1);  // delay in between reads for stability
}

Learn more

คุณสามารถค้นหาบทช่วยสอนพื้นฐานเพิ่มเติมได้ใน the built-in examples section.

คุณยังสามารถสำรวจ the language reference, ซึ่งเป็นคอลเล็กชันรายละเอียดของ the Arduino programming language.

Last revision 2015/07/29 by SM
#12
AVR and Arduino / Re: Essentials
Last post by tha - November 29, 2023, 08:52:12 AM
https://docs.arduino.cc/built-in-examples/analog/Fading

Fading

ใช้ an analog output (PWM pin) เพื่อจาง an LED ลง.

LAST REVISION: 15/11/2566 22:52

ตัวอย่างนี้สาธิตการใช้ analog output (Pulse Width Modulation (PWM)) เพื่อทำให้ LED จางลง PWM เป็นเทคนิคในการรับพฤติกรรมแบบอะนาล็อกจาก a digital output โดยการปิดและเปิดอย่างรวดเร็วและมีอัตราส่วนระหว่างเวลาเปิดและปิดที่แตกต่างกัน

Hardware Required

  •  Arduino board
  •  LED
  •  220 ohm resistor
  •  hook-up wires
  •  breadboard

Circuit

An LED ถูกต่อกับ digital output pin 9 โดยผ่าน a 220 ohm resistor.



Schematic



Code

ในตัวอย่างนี้ สองลูปจะถูกดำเนินการทีละลูปเพื่อเพิ่มและจากนั้นลดค่าของเอาต์พุตบนพิน 9

/*

  Fading

  This example shows how to fade an LED using the analogWrite() function.

  The circuit:

  - LED attached from digital pin 9 to ground.

  created 1 Nov 2008

  by David A. Mellis

  modified 30 Aug 2011

  by Tom Igoe

  This example code is in the public domain.

  https://www.arduino.cc/en/Tutorial/Fading

*/

int ledPin = 9;    // LED connected to digital pin 9

void setup() {

  // nothing happens in setup
}

void loop() {

  // fade in from min to max in increments of 5 points:

  for (int fadeValue = 0 ; fadeValue <= 255; fadeValue += 5) {

    // sets the value (range from 0 to 255):

    analogWrite(ledPin, fadeValue);

    // wait for 30 milliseconds to see the dimming effect

    delay(30);

  }

  // fade out from max to min in increments of 5 points:

  for (int fadeValue = 255 ; fadeValue >= 0; fadeValue -= 5) {

    // sets the value (range from 0 to 255):

    analogWrite(ledPin, fadeValue);

    // wait for 30 milliseconds to see the dimming effect

    delay(30);

  }
}

Learn more

คุณสามารถค้นหาบทช่วยสอนพื้นฐานเพิ่มเติมได้ใน the built-in examples section.

คุณยังสามารถสำรวจ the language reference, ซึ่งเป็นคอลเล็กชันรายละเอียดของ the Arduino programming language.

Last revision 2015/07/29 by SM
#13
AVR and Arduino / Re: Essentials
Last post by tha - November 28, 2023, 02:25:31 PM
Code

ก่อนการตั้งค่า คุณต้องตั้งค่าเริ่มต้นสำหรับค่าต่ำสุดและสูงสุดดังนี้:



สิ่งเหล่านี้อาจดูกลับด้าน ในตอนแรก คุณตั้งค่าต่ำสุดเป็นสูงสุดและอ่านค่าใดๆ ที่ต่ำกว่านั้น แล้วบันทึกมันเป็นค่าต่ำสุดใหม่ ในทำนองเดียวกัน คุณตั้งค่าสูงสุดเป็นต่ำสุดและอ่านค่าใดๆ ที่สูงกว่าเป็นค่าสูงสุดใหม่ ดังนี้:



ด้วยวิธีนี้ การอ่านใดต่อไปที่คุณทำสามารถถูกแม็ปกับช่วงระหว่างค่าต่ำสุดและค่าสูงสุดนี้ได้ดังนี้:



นี่คือทั้งหมดของโปรแกรม

/*
  Calibration

  Demonstrates one technique for calibrating sensor input. The sensor readings
  during the first five seconds of the sketch execution define the minimum and
  maximum of expected values attached to the sensor pin.

  The sensor minimum and maximum initial values may seem backwards. Initially,
  you set the minimum high and listen for anything lower, saving it as the new
  minimum. Likewise, you set the maximum low and listen for anything higher as
  the new maximum.

  The circuit:
  - analog sensor (potentiometer will do) attached to analog input 0
  - LED attached from digital pin 9 to ground through 220 ohm resistor

  created 29 Oct 2008
  by David A Mellis
  modified 30 Aug 2011
  by Tom Igoe
  modified 07 Apr 2017
  by Zachary J. Fields

  This example code is in the public domain.

  https://www.arduino.cc/en/Tutorial/BuiltInExamples/Calibration
*/

// These constants won't change:
const int sensorPin = A0;  // pin that the sensor is attached to
const int ledPin = 9;      // pin that the LED is attached to

// variables:
int sensorValue = 0;   // the sensor value
int sensorMin = 1023;  // minimum sensor value
int sensorMax = 0;     // maximum sensor value


void setup() {
  // turn on LED to signal the start of the calibration period:
  pinMode(13, OUTPUT);
  digitalWrite(13, HIGH);

  // calibrate during the first five seconds
  while (millis() < 5000) {
    sensorValue = analogRead(sensorPin);

    // record the maximum sensor value
    if (sensorValue > sensorMax) {
      sensorMax = sensorValue;
    }

    // record the minimum sensor value
    if (sensorValue < sensorMin) {
      sensorMin = sensorValue;
    }
  }

  // signal the end of the calibration period
  digitalWrite(13, LOW);
}

void loop() {
  // read the sensor:
  sensorValue = analogRead(sensorPin);

  // in case the sensor value is outside the range seen during calibration
  sensorValue = constrain(sensorValue, sensorMin, sensorMax);

  // apply the calibration to the sensor reading
  sensorValue = map(sensorValue, sensorMin, sensorMax, 0, 255);

  // fade the LED using the calibrated value:
  analogWrite(ledPin, sensorValue);
}

Learn more

คุณสามารถค้นหาบทช่วยสอนพื้นฐานเพิ่มเติมได้ใน the built-in examples section.

คุณยังสามารถสำรวจ the language reference, ซึ่งเป็นคอลเล็กชันรายละเอียดของ the Arduino programming language.

Last revision 2015/07/29 by SM
#14
AVR and Arduino / Re: Essentials
Last post by tha - November 28, 2023, 11:42:11 AM
https://docs.arduino.cc/built-in-examples/analog/Calibration

Calibrate Sensor Input

กำหนด a maximum and minimum สำหรับ expected analog sensor values.

LAST REVISION: 15/11/2566 22:52

ตัวอย่างนี้สาธิตเทคนิคหนึ่งในการสอบเทียบ sensor input. บอร์ดใช้เวลาอ่านค่าเซ็นเซอร์เป็นเวลาห้าวินาทีในระหว่างการสตาร์ท และติดตามค่าสูงสุดและต่ำสุดที่ได้รับ การอ่านเซ็นเซอร์เหล่านี้ในช่วงห้าวินาทีแรกของ the sketch execution จะกำหนดค่าต่ำสุดและสูงสุดของค่าที่คาดหวังสำหรับการอ่านที่เกิดขึ้นระหว่าง the loop.

Hardware Required

  •  Arduino board
  •  LED
  •  analog sensor (a photoresistor will do)
  •  10k ohm resistor
  •  220 ohm resistor
  •  hook-up wires
  •  breadboard

Circuit

Analog sensor (ตัวอย่างเช่น potentiometer, light sensor) บน Analog input 2. LED บน Digital pin 9.



ต่อ an LED เข้ากับ digital pin 9 โดยมี a 220 ohm current limiting resistor ต่ออนุกรมกัน. ต่อ a photoresistor กับ 5V และจากนั้นต่อกับ analog pin 0 โดยมี a 10K ohm resistor ต่อลงกราวด์.

Schematic

#15
AVR and Arduino / Re: Essentials
Last post by tha - November 28, 2023, 09:49:52 AM
https://docs.arduino.cc/built-in-examples/analog/AnalogWriteMega

Analog Write with 12 LEDs on an Arduino Mega

เปิดและปิดไฟ LED 12 ดวงทีละดวงโดยใช้ an Arduino Mega board.

LAST REVISION: 15/11/2566 22:52

ตัวอย่างนี้ทำให้ไฟ LED 12 ดวงสว่างขึ้นและจางลงทีละดวงบนบอร์ด Arduino Mega โดยใช้ประโยชน์จากจำนวน PWM enabled digital pins ที่มีเพิ่มขึ้นของบอร์ดนี้

Hardware Required

  •  Arduino Mega Board
  •  12 Red LEDs
  •  12 220 ohm resistors
  •  hook-up wires
  •  breadboard

Circuit



เชื่อมต่อขาบวกที่ยาวกว่า (แอโนด) ของ LED 12 ดวงเข้ากับ digital pins 2-13 ผ่านตัวต้านทานจำกัดกระแส 220 โอห์ม เชื่อมต่อขาขั้วลบ (แคโทด) ที่สั้นกว่าเข้ากับกราวด์

Schematic



Code

ใน the setup() function ของ the code ข้างล่าง, a for() loop ถูกใช้เพื่อกำหนด digital pins 2-13 ของ the Mega เป็น outputs.

ถัดไป ในฟังก์ชัน loop() ของโปรแกรมด้านล่าง แบบซ้อนสาม for() loops ถูกใช้.

สิ่งแรกของ loops เหล่านี้,

for (int thisPin =lowestPin; thisPin <= highestPin; thisPin++)

เคลื่อนที่ผ่านแต่ละ LEDS ทีละดวงจากพินต่ำสุดไปพินสูงสุด ก่อนที่ลูปนี้จะได้รับอนุญาตให้ย้ายจากพินหนึ่งไปยังอีกพินหนึ่ง ต้องทำสองสิ่งให้สำเร็จก่อน ขั้นแรก คุณเพิ่มความสว่างให้กับ LED แต่ละดวงโดยใช้โค้ดบรรทัดเหล่านี้:

for (int brightness = 0; brightness < 255; brightness++) {
analogWrite(thisPin, brightness); delay(2); }

เมื่อแต่ละรอบผ่านลูปด้านบน ความสว่างของตัวแปรจะเพิ่มขึ้นหนึ่งจุด และค่านั้นจะถูกเขียนไปยังพินที่เลือกปัจจุบันใน the main loop. เมื่อพินนั้นถึงค่า PWM สูงสุด (255) ลูปต่อไปนี้จะเริ่มทำงาน:

for (int brightness = 255; brightness >= 0; brightness--) {
analogWrite(thisPin, brightness); delay(2); }

ลูปนี้จะลบหนึ่งจุดออกจากตัวแปร brightness หรี่ไฟ LED กลับลงไปที่ 0 เมื่อถึงศูนย์ the main for() loop จะเริ่มทำงาน และโปรแกรมจะย้ายไปยังพิน LED ถัดไป โดยทำซ้ำขั้นตอนทั้งหมดที่กล่าวถึงข้างต้น

/*

  Mega analogWrite() test

  This sketch fades LEDs up and down one at a time on digital pins 2 through 13.

  This sketch was written for the Arduino Mega, and will not work on other boards.

  The circuit:

  - LEDs attached from pins 2 through 13 to ground.

  created 8 Feb 2009

  by Tom Igoe

  This example code is in the public domain.

  https://www.arduino.cc/en/Tutorial/AnalogWriteMega

*/

// These constants won't change. They're used to give names to the pins used:

const int lowestPin = 2;

const int highestPin = 13;

void setup() {

  // set pins 2 through 13 as outputs:

  for (int thisPin = lowestPin; thisPin <= highestPin; thisPin++) {

    pinMode(thisPin, OUTPUT);

  }
}

void loop() {

  // iterate over the pins:

  for (int thisPin = lowestPin; thisPin <= highestPin; thisPin++) {

    // fade the LED on thisPin from off to brightest:

    for (int brightness = 0; brightness < 255; brightness++) {

      analogWrite(thisPin, brightness);

      delay(2);

    }

    // fade the LED on thisPin from brightest to off:

    for (int brightness = 255; brightness >= 0; brightness--) {

      analogWrite(thisPin, brightness);

      delay(2);

    }

    // pause between LEDs:

    delay(100);

  }
}

Learn more

คุณสามารถค้นหาบทช่วยสอนพื้นฐานเพิ่มเติมได้ใน the built-in examples section.

คุณยังสามารถสำรวจ the language reference, ซึ่งเป็นคอลเล็กชันรายละเอียดของ the Arduino programming language.

Last revision 2015/07/28 by SM
#16
AVR and Arduino / Re: Essentials
Last post by tha - November 27, 2023, 03:59:31 PM
Code

ที่จุดเริ่มต้นของ sketch นี้ the variable sensorPin จะถูกตั้งค่าเป็น analog pin 0 ที่ที่โพเทนชิออมิเตอร์ของคุณติดอยู่ และ ledPin ตั้งค่าเป็น digital pin 13 นอกจากนี้คุณยังจะสร้างตัวแปรอีกตัวหนึ่งคือ sensorValue เพื่อจัดเก็บค่าที่อ่านจากเซ็นเซอร์ของคุณ .

คำสั่ง analogRead() จะแปลง the input voltage ช่วง 0 ถึง 5 โวลต์ให้เป็นค่าดิจิทัลระหว่าง 0 ถึง 1,023 ซึ่งทำได้โดยวงจรภายในไมโครคอนโทรลเลอร์ที่เรียกว่า an analog-to-digital converter or ADC.

ด้วยการหมุนแกนของโพเทนชิออมิเตอร์ คุณจะเปลี่ยนปริมาณความต้านทานที่ด้านใดด้านหนึ่งของ the center pin (หรือ wiper) ของโพเทนชิออมิเตอร์ การดำเนินการนี้จะเปลี่ยนความต้านทานสัมพัทธ์ระหว่างพินตรงกลางและพินด้านนอกสองพิน ทำให้คุณได้รับแรงดันไฟฟ้าที่แตกต่างกันที่ the analog input. เมื่อแกนถูกหมุนไปในทิศทางเดียวจนสุด จะไม่มีความต้านทานระหว่าง the center pin กับพินที่เชื่อมต่อกับกราวด์ แรงดันไฟฟ้าที่ the center pin จะเป็น 0 โวลต์ และ analogRead() ส่งคืนกลับค่า 0 เมื่อหมุนแกนไปในทิศทางอีกทางจนสุด จะไม่มีความต้านทานระหว่าง the center pin กับพินที่ต่อกับ +5 โวลต์ แรงดันไฟฟ้าที่ the center pin จะเป็น 5 โวลต์ และ analogRead() ส่งคืนกลับค่า 1023 ในระหว่างทางนี้ analogRead() จะส่งคืนกลับตัวเลขระหว่าง 0 ถึง 1023 ซึ่งเป็นสัดส่วนกับปริมาณแรงดันไฟฟ้าที่ปรากฎกับ the pin.

ค่านั้นถูกจัดเก็บไว้ใน sensorValue ถูกใช้เพื่อตั้งค่า a delay() สำหรับรอบการกะพริบของคุณ ยิ่งค่าสูง รอบยิ่งนาน ค่ายิ่งน้อย รอบก็จะสั้นลง ค่าถูกอ่านที่จุดเริ่มต้นของ the cycle ดังนั้นเวลาเปิด/ปิดจึงเท่ากันเสมอ

/*
  Analog Input

  Demonstrates analog input by reading an analog sensor on analog pin 0 and
  turning on and off a light emitting diode(LED) connected to digital pin 13.
  The amount of time the LED will be on and off depends on the value obtained
  by analogRead().

  The circuit:
  - potentiometer
    center pin of the potentiometer to the analog input 0
    one side pin (either one) to ground
    the other side pin to +5V
  - LED
    anode (long leg) attached to digital output 13 through 220 ohm resistor
    cathode (short leg) attached to ground

  - Note: because most Arduinos have a built-in LED attached to pin 13 on the
    board, the LED is optional.

  created by David Cuartielles
  modified 30 Aug 2011
  By Tom Igoe

  This example code is in the public domain.

  https://www.arduino.cc/en/Tutorial/BuiltInExamples/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);
}
#17
AVR and Arduino / Re: Essentials
Last post by tha - November 27, 2023, 02:52:47 PM
https://docs.arduino.cc/built-in-examples/analog/AnalogInput

Analog Input

ใช้ a potentiometer เพื่อควบคุมการกระพริบของ an LED.

LAST REVISION: 15/11/2566 22:52

ในตัวอย่างนี้ เราใช้ตัวต้านทานแบบปรับค่าได้ (โพเทนชิออมิเตอร์หรือโฟโตรีซีสเตอร์) เราอ่านค่าของมันโดยใช้ analog input ตัวหนึ่งของบอร์ด Arduino และเปลี่ยนอัตราการกะพริบของ the built-in LED สอดคล้องกัน ค่าอะนาล็อกของตัวต้านทานจะถูกอ่านเป็นแรงดันไฟฟ้า เนื่องจากนี่คือวิธีการทำงานของอินพุตแบบอะนาล็อก

Hardware Required

  •  Arduino Board
  •  Potentiometer or
  •  10K ohm photoresistor and 10K ohm resistor
  •  built-in LED on pin 13 or
  •  220 ohm resistor and red LED

Circuit

ด้วย a potentiometer



ด้วย a photoresistor



เชื่อมต่อสายไฟสามเส้นเข้ากับบอร์ด Arduino อันแรกจะต่อลงกราวด์จากพินด้านนอกตัวใดตัวหนึ่งของโพเทนชิออมิเตอร์ สายที่สองไปจาก 5 โวลต์ไปยังพินด้านนอกอีกอันของโพเทนชิออมิเตอร์ สายที่สามไปจาก analog input 0 ไปยังพินกลางของโพเทนชิออมิเตอร์

สำหรับตัวอย่างนี้ คุณสามารถใช้ LED ในตัวของบอร์ดที่ต่อกับพิน 13 ได้ หากต้องการใช้ LED เพิ่มเติม ให้ติดขาที่ยาวกว่า (ขาบวกหรืออาโนด) เข้ากับพินดิจิทัล 13 โดยอนุกรมตัวต้านทาน 220 โอห์ม และขาสั้นกว่า (ขาลบหรือแคโทด) ถึงพินกราวด์ (GND) ถัดจากพิน 13

The circuit based on a photoresistor uses a resistor divider to allow the high impedance Analog input to measure the voltage. These inputs do not draw almost any current, therefore by Ohm's law the voltage measured on the other end of a resistor connected to 5V is always 5V, regardless the resistor's value. To get a voltage proportional to the photoresistor value, a resistor divider is necessary. This circuit uses a variable resistor, a fixed resistor and the measurement point is in the middle of the resistors. The voltage measured (Vout) follows this formula:

วงจรที่ใช้โฟโตรีซีสเตอร์ใช้ a resistor divider เพื่อให้ the high impedance Analog input ใช้วัดแรงดันไฟฟ้า อินพุตเหล่านี้แทบไม่ได้ดึงกระแสใดๆเลย ดังนั้นตามกฎของโอห์ม แรงดันไฟฟ้าที่วัดที่ปลายอีกด้านของตัวต้านทานที่เชื่อมต่อกับ 5V จะเป็น 5V เสมอ ไม่ว่าค่าของตัวต้านทานจะเป็นเท่าใด เพื่อให้ได้แรงดันไฟฟ้าที่เป็นสัดส่วนกับค่าโฟโตรีซิสเตอร์ จำเป็นต้องมี a resistor divider วงจรนี้ใช้ตัวต้านทานแบบปรับค่าได้ ตัวต้านทานแบบคงที่ และมีจุดวัดอยู่ที่กึ่งกลางของตัวต้านทาน แรงดันไฟฟ้าที่วัดได้ (Vout) เป็นไปตามสูตรนี้:

Vout=Vin*(R2/(R1+R2))

โดยที่ Vin คือ 5V, R2 คือ 10k ohm และ R1 คือค่าโฟโตรีซิสเตอร์ซึ่งมีช่วงจาก 1M ohm ในความมืดไปจนถึง 10k ohm ในเวลากลางวัน (10 ลูเมน) และน้อยกว่า 1k ohm ในแสงจ้าหรือแสงแดด (>100 ลูเมน)

Schematic

Potentiometer


#18
AVR and Arduino / Re: Essentials
Last post by tha - November 25, 2023, 09:59:37 AM
Code

ใน the sketch ด้านล่าง หลังจากประกาศการกำหนดพินสองพิน (analog 0 สำหรับ potentiometer ของเรา และ digital 9 สำหรับ LED ของคุณ) และตัวแปรสองตัว ได้แก่ sensorValue และ outputValue, สิ่งเดียวที่คุณทำในฟังก์ชัน the setup() คือเริ่ม serial communication.

ถัดไป ใน the main loop, sensorValue จะถูกกำหนดให้จัดเก็บ the raw analog value ที่อ่านจาก the potentiometer. Arduino มี an analogRead ช่วงจาก 0 ถึง 1023 และan analogWrite ช่วงจาก 0 ถึง 255 เท่านั้น ดังนั้นข้อมูลจาก the potentiometer จะต้องถูกแปลงให้พอดีกับช่วงที่เล็กลงก่อนที่จะใช้มันเพื่อหรี่ไฟ LED

เพื่อแปลงค่านี้ value, ใช้ a function ที่เรียกว่า map():

outputValue = map(sensorValue, 0, 1023, 0, 255);

outputValue ได้รับการกำหนดให้เท่ากับค่าที่ปรับขนาดจากthe potentiometer. map() รับห้าอาร์กิวเมนต์คือ: ค่าที่จะถูกแมป ช่วงต่ำและค่าสูงของ the input data และค่าต่ำและสูงสำหรับข้อมูลนั้นที่จะแมปใหม่ ในกรณีนี้ the sensor data จะถูกแมปลงจากช่วงเดิมที่ 0 ถึง 1,023 ไปเป็น 0 ถึง 255

จากนั้น sensor data ที่แมปใหม่จะถูกส่งออกไปยัง AnalogOutPin ซึ่งจะทำให้ LED มืดลงหรือสว่างขึ้นตาม the potentiometer ที่ถูกหมุน ในที่สุด ทั้งค่าเซ็นเซอร์แบบดิบและแบบปรับขนาดจะถูกส่งไปยัง the Arduino Software (IDE) serial monitor window, ในกระแสที่มั่นคงของข้อมูล

/*

  Analog input, analog output, serial output

  Reads an analog input pin, maps the result to a range from 0 to 255 and uses

  the result to set the pulse width modulation (PWM) of an output pin.

  Also prints the results to the Serial Monitor.

  The circuit:

  - potentiometer connected to analog pin 0.

    Center pin of the potentiometer goes to the analog pin.

    side pins of the potentiometer go to +5V and ground

  - LED connected from digital pin 9 to ground

  created 29 Dec. 2008

  modified 9 Apr 2012

  by Tom Igoe

  This example code is in the public domain.

  https://www.arduino.cc/en/Tutorial/AnalogInOutSerial

*/

// These constants won't change. They're used to give names to the pins used:

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 2 milliseconds before the next loop for the analog-to-digital

  // converter to settle after the last reading:

  delay(2);
}

Learn more

คุณสามารถค้นหาบทช่วยสอนพื้นฐานเพิ่มเติมได้ใน the built-in examples section.

คุณยังสามารถสำรวจ the language reference, ซึ่งเป็นคอลเล็กชันรายละเอียดของ the Arduino programming language.

Last revision 2015/07/28 by SM
#19
AVR and Arduino / Re: Essentials
Last post by tha - November 25, 2023, 09:10:32 AM
https://docs.arduino.cc/built-in-examples/analog/AnalogInOutSerial

Analog

Analog In, Out Serial

อ่าน an analog input pin, แมปผลลัพธ์ จากนั้นใช้ข้อมูลนั้นเพื่อหรี่หรือเพิ่มความสว่างให้กับ LED.

LAST REVISION: 15/11/2566 22:52

ตัวอย่างนี้แสดงวิธีการอ่าน an analog input pin, แมปผลลัพธ์เป็นช่วงตั้งแต่ 0 ถึง 255 ใช้ผลลัพธ์นั้นเพื่อตั้งค่า the pulse width modulation (PWM) ของ an output pin เพื่อลดหรือเพิ่มความสว่างให้กับ LED และพิมพ์ค่าบน the serial monitor ของ the Arduino Software (IDE).

Hardware Required

  •  Arduino Board
  •  Potentiometer
  •  Red LED
  •  220 ohm resistor

Circuit



เชื่อมต่อหนึ่งพินจาก pot ของคุณเข้ากับ 5V, พินตรงกลางเข้ากับ analog pin 0 และพินที่เหลือลงกราวด์ จากนั้น เชื่อมต่อตัวต้านทานจำกัดกระแส 220 โอห์มเข้ากับ digital pin 9, โดยมีไฟ LED ต่ออนุกรม ขาบวกที่ยาวกว่า (the anode) ของ LED ถูกเชื่อมต่อกับ the output จากตัวต้านทาน โดยขาขั้วลบที่สั้นกว่า (แคโทด) เชื่อมต่อกับกราวด์

Schematic

#20
AVR and Arduino / Re: Essentials
Last post by tha - November 24, 2023, 11:21:42 AM
https://docs.arduino.cc/built-in-examples/digital/tonePitchFollower

Pitch follower using the tone() function

เล่นระดับเสียงบน a piezo speaker โดยขึ้นอยู่กับ an analog input.

LAST REVISION: 15/11/2566 22:52

ตัวอย่างนี้แสดงวิธีใช้คำสั่ง tone() เพื่อสร้างระดับเสียงที่ตามค่าของ an analog input. โดยใช้ a photoresistor บอร์ด Arduino ของคุณจะกลายเป็น a simplified light theremin.

Hardware Required

  •  Arduino board
  •  8 ohm speaker
  •  photoresistor
  •  4.7K ohm resistor
  •  100 ohm resistor
  •  hook-up wires
  •  breadboard

Circuit



เชื่อมต่อขั้วต่อหนึ่งของลำโพงเข้ากับ digital pin 9 ผ่านตัวต้านทาน 100 โอห์ม และขั้วต่ออีกขั้วหนึ่งของมันเข้ากับกราวด์ จ่ายไฟให้กับ  photoresistor ของคุณด้วยไฟ 5V และเชื่อมต่อมันกับ analog 0 ด้วยการเพิ่มตัวต้านทาน 4.7K ลงไปที่กราวด์

Schematic



Code

The code สำหรับตัวอย่างนี้ง่ายมาก เพียงใช้ an analog input และแมปค่าของมันกับช่วงระดับเสียงที่ได้ยินได้ มนุษย์สามารถได้ยินคลื่นความถี่จาก 20 - 20,000 เฮิร์ตซ์ แต่ความถี่ 120 - 1,500 เฮิร์ตซ์ มักจะเหมาะกับ  sketch นี้

คุณจะต้องได้รับช่วงที่แท้จริงของ analog input ของคุณสำหรับการแมป ในวงจรที่แสดง the analog input value ช่วงอยู่ระหว่างประมาณ 400 ถึงประมาณ 1,000 เปลี่ยนค่าในคำสั่ง map() ให้ตรงกับช่วงของเซ็นเซอร์ของคุณ

The sketch เป็นดังนี้:

/*

  Pitch follower

  Plays a pitch that changes based on a changing analog input

  circuit:

  - 8 ohm speaker on digital pin 9

  - photoresistor on analog 0 to 5V

  - 4.7 kilohm resistor on analog 0 to ground

  created 21 Jan 2010

  modified 31 May 2012

  by Tom Igoe, with suggestion from Michael Flynn

  This example code is in the public domain.

  https://www.arduino.cc/en/Tutorial/Tone2

*/

void setup() {

  // initialize serial communications (for debugging only):

  Serial.begin(9600);
}

void loop() {

  // read the sensor:

  int sensorReading = analogRead(A0);

  // print the sensor reading so you know its range

  Serial.println(sensorReading);

  // map the analog input range (in this case, 400 - 1000 from the photoresistor)

  // to the output pitch range (120 - 1500Hz)

  // change the minimum and maximum input numbers below depending on the range

  // your sensor's giving:

  int thisPitch = map(sensorReading, 400, 1000, 120, 1500);

  // play the pitch:

  tone(9, thisPitch, 10);

  delay(1);        // delay in between reads for stability
}

Learn more

คุณสามารถค้นหาบทช่วยสอนพื้นฐานเพิ่มเติมได้ใน the built-in examples section.

คุณยังสามารถสำรวจ the language reference, ซึ่งเป็นคอลเล็กชันรายละเอียดของ the Arduino programming language.

Last revision 2015/08/11 by SM