Main Menu
Menu

Show posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.

Show posts Menu

Messages - tha

#1
AVR and Arduino / Re: Essentials
January 02, 2024, 01:00:49 PM
https://docs.arduino.cc/built-in-examples/communication/Dimmer

Create a LED Dimmer

เลื่อน the mouse เพื่อเปลี่ยนความสว่างของ an LED.

LAST REVISION: 28/12/2566 14:23

ตัวอย่างนี้แสดงวิธีการส่ง data จาก a personal computer ไปยัง an Arduino board เพื่อควบคุมความสว่างของ an LED. The data จะถูกส่งเป็นแต่ละรายเป็น bytes, ซึ่งแต่ละไบต์มีค่าตั้งแต่ 0 ถึง 255. The sketch จะอ่านไบต์เหล่านี้และใช้พวกมันเพื่อเซ็ตความสว่างของ LED.

คุณสามารถส่งไบต์ไปยังบอร์ดจากซอฟต์แวร์ใดๆ ที่สามารถเข้าถึง the computer serial port. ตัวอย่างสำหรับ Processing และ Max/MSP version 5 ถูกแสดงไว้ด้านล่าง

Hardware Required

  •  Arduino Board
  •  LED
  •  220 ohm resistor

Software Required

  •  Processing or
  •  Max/MSP version 5

Circuit

ต่อ the 220 ohm current limiting resistor เข้ากับ digital pin 9, โดยมี LED ต่ออนุกรม ขาบวกที่ยาวกว่า(the anode) ของ LED ควรถูกต่อกับ the output จาก the resistor, โดยขาขั้วลบที่สั้นกว่า (แคโทด) ถูกต่อกับกราวด์



Schematic



Code

/*

  Dimmer

  Demonstrates sending data from the computer to the Arduino board, in this case

  to control the brightness of an LED. The data is sent in individual bytes,

  each of which ranges from 0 to 255. Arduino reads these bytes and uses them to

  set the brightness of the LED.

  The circuit:

  - LED attached from digital pin 9 to ground.

  - Serial connection to Processing, Max/MSP, or another serial application

  created 2006

  by David A. Mellis

  modified 30 Aug 2011

  by Tom Igoe and Scott Fitzgerald

  This example code is in the public domain.

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

*/

const int ledPin = 9;      // the pin that the LED is attached to

void setup() {

  // initialize the serial communication:

  Serial.begin(9600);

  // initialize the ledPin as an output:

  pinMode(ledPin, OUTPUT);
}

void loop() {

  byte brightness;

  // check if data has been sent from the computer:

  if (Serial.available()) {

    // read the most recent byte (which will be from 0 to 255):

    brightness = Serial.read();

    // set the brightness of the LED:

    analogWrite(ledPin, brightness);

  }
}

/* Processing code for this example

  // Dimmer - sends bytes over a serial port

  // by David A. Mellis

  // This example code is in the public domain.

  import processing.serial.*;

  Serial port;

  void setup() {

    size(256, 150);

    println("Available serial ports:");

    // if using Processing 2.1 or later, use Serial.printArray()

    println(Serial.list());

    // Uses the first port in this list (number 0). Change this to select the port

    // corresponding to your Arduino board. The last parameter (e.g. 9600) is the

    // speed of the communication. It has to correspond to the value passed to

    // Serial.begin() in your Arduino sketch.

    port = new Serial(this, Serial.list()[0], 9600);

    // If you know the name of the port used by the Arduino board, you can specify

    // it directly like this.

    //port = new Serial(this, "COM1", 9600);

  }

  void draw() {

    // draw a gradient from black to white

    for (int i = 0; i < 256; i++) {

      stroke(i);

      line(i, 0, i, 150);

    }

    // write the current X-position of the mouse to the serial port as

    // a single byte

    port.write(mouseX);

  }

*/

/* Max/MSP v5 patch for this example

----------begin_max5_patcher----------

1008.3ocuXszaiaCD9r8uhA5rqAeHIa0aAMaAVf1S6hdoYQAsDiL6JQZHQ2M

YWr+2KeX4vjnjXKKkKhhiGQ9MeyCNz+X9rnMp63sQvuB+MLa1OlOalSjUvrC

ymEUytKuh05TKJWUWyk5nE9eSyuS6jesvHu4F4MxOuUzB6X57sPKWVzBLXiP

xZtGj6q2vafaaT0.BzJfjj.p8ZPukazsQvpfcpFs8mXR3plh8BoBxURIOWyK

rxspZ0YI.eTCEh5Vqp+wGtFXZMKe6CZc3yWZwTdCmYW.BBkdiby8v0r+ST.W

sD9SdUkn8FYspPbqvnBNFtZWiUyLmleJWo0vuKzeuj2vpJLaWA7YiE7wREui

FpDFDp1KcbAFcP5sJoVxp4NB5Jq40ougIDxJt1wo3GDZHiNocKhiIExx+owv

AdOEAksDs.RRrOoww1Arc.9RvN2J9tamwjkcqknvAE0l+8WnjHqreNet8whK

z6mukIK4d+Xknv3jstvJs8EirMMhxsZIusET25jXbX8xczIl5xPVxhPcTGFu

xNDu9rXtUCg37g9Q8Yc+EuofIYmg8QdkPCrOnXsaHwYs3rWx9PGsO+pqueG2

uNQBqWFh1X7qQG+3.VHcHrfO1nyR2TlqpTM9MDsLKNCQVz6KO.+Sfc5j1Ykj

jzkn2jwNDRP7LVb3d9LtoWBAOnvB92Le6yRmZ4UF7YpQhiFi7A5Ka8zXhKdA

4r9TRGG7V4COiSbAJKdXrWNhhF0hNUh7uBa4Mba0l7JUK+omjDMwkSn95Izr

TOwkdp7W.oPRmNRQsiKeu4j3CkfVgt.NYPEYqMGvvJ48vIlPiyzrIuZskWIS

xGJPcmPiWOfLodybH3wjPbMYwlbFIMNHPHFOtLBNaLSa9sGk1TxMzCX5KTa6

WIH2ocxSdngM0QPqFRxyPHFsprrhGc9Gy9xoBjz0NWdR2yW9DUa2F85jG2v9

FgTO4Q8qiC7fzzQNpmNpsY3BrYPVJBMJQ1uVmoItRhw9NrVGO3NMNzYZ+zS7

3WTvTOnUydG5kHMKLqAOjTe7fN2bGSxOZDkMrBrGQ9J1gONBEy0k4gVo8qHc

cxmfxVihWz6a3yqY9NazzUYkua9UnynadOtogW.JfsVGRVNEbWF8I+eHtcwJ

+wLXqZeSdWLo+FQF6731Tva0BISKTx.cLwmgJsUTTvkg1YsnXmxDge.CDR7x

D6YmX6fMznaF7kdczmJXwm.XSOOrdoHhNA7GMiZYLZZR.+4lconMaJP6JOZ8

ftCs1YWHZI3o.sIXezX5ihMSuXzZtk3ai1mXRSczoCS32hAydeyXNEu5SHyS

xqZqbd3ZLdera1iPqYxOm++v7SUSz

-----------end_max5_patcher-----------

*/

Processing Code

The Processing sketch ใน the code sample ข้างบนจะส่ง bytes ออก the computer serial port ไปยัง the board เพื่อหรี่ the LED.

Max code

The Max/MSP patch ใน the code sample ข้างบนมีลักษณะเหมือนภาพด้านล่าง คัดลอกและวางมันลงใน a new patch window.



Learn more

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

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

Last revision 2015/07/29 by SM
#2
AVR and Arduino / Re: Essentials
January 02, 2024, 09:42:04 AM
https://docs.arduino.cc/built-in-examples/communication/ASCIITable

Communication

ASCII Table

สาธิต advanced Arduino serial output functions.

LAST REVISION: 28/12/2566 14:23

ตัวอย่างนี้สาธิต the advanced serial printing functions โดยการสร้างบน the serial monitor ของ the Arduino Software (IDE) ตารางของอักขระและค่า ASCII ของพวกมันเป็น decimal, hexadecimal, octal, และ binary. สำหรับข้อมูลเพิ่มเติมเกี่ยวกับ ASCII, ดูที่ asciitable.com และ http://en.wikipedia.org/wiki/ASCII

Hardware Required

  •  Arduino Board

Circuit



ไม่มี, แต่ the board ต้องถูกต่อกับ the computer ผ่าน the serial port หรือ the USB port.

Code

The sketch รอสำหรับ a serial connection ใน the setup() จากนั้นพิมพ์ตาราง ASCII ทีละบรรทัดจนถึงอักขระตัวสุดท้ายที่สามารถพิมพ์ได้ เมื่อทำสิ่งนี้สำเร็จ มันจะเข้าสู่  loop ที่ไม่สิ้นสุดใน a while structure และไม่มีอะไรเกิดขึ้นอีก การปิดและเปิด the serial monitor window ของ the Arduino Software (IDE)  ควรรีเซ็ตบอร์ดและรีสตาร์ท the sketch ใหม่

/*
  ASCII table

  Prints out byte values in all possible formats:
  - as raw binary values
  - as ASCII-encoded decimal, hex, octal, and binary values

  For more on ASCII, see http://www.asciitable.com and http://en.wikipedia.org/wiki/ASCII

  The circuit: No external hardware needed.

  created 2006
  by Nicholas Zambetti <http://www.zambetti.com>
  modified 9 Apr 2012
  by Tom Igoe

  This example code is in the public domain.

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

void setup() {
  //Initialize serial and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ;  // wait for serial port to connect. Needed for native USB port only
  }

  // prints title with ending line break
  Serial.println("ASCII Table ~ Character Map");
}

// first visible ASCIIcharacter '!' is number 33:
int thisByte = 33;
// you can also write ASCII characters in single quotes.
// for example, '!' is the same as 33, so you could also use this:
// int thisByte = '!';

void loop() {
  // prints value unaltered, i.e. the raw binary version of the byte.
  // The Serial Monitor interprets all bytes as ASCII, so 33, the first number,
  // will show up as '!'
  Serial.write(thisByte);

  Serial.print(", dec: ");
  // prints value as string as an ASCII-encoded decimal (base 10).
  // Decimal is the default format for Serial.print() and Serial.println(),
  // so no modifier is needed:
  Serial.print(thisByte);
  // But you can declare the modifier for decimal if you want to.
  // this also works if you uncomment it:

  // Serial.print(thisByte, DEC);


  Serial.print(", hex: ");
  // prints value as string in hexadecimal (base 16):
  Serial.print(thisByte, HEX);

  Serial.print(", oct: ");
  // prints value as string in octal (base 8);
  Serial.print(thisByte, OCT);

  Serial.print(", bin: ");
  // prints value as string in binary (base 2) also prints ending line break:
  Serial.println(thisByte, BIN);

  // if printed last visible character '~' or 126, stop:
  if (thisByte == 126) {  // you could also use if (thisByte == '~') {
    // This loop loops forever and does nothing
    while (true) {
      continue;
    }
  }
  // go on to the next character
  thisByte++;
}

Output

ASCII Table ~ Character Map
!, dec: 33, hex: 21, oct: 41, bin: 100001
", dec: 34, hex: 22, oct: 42, bin: 100010
#, dec: 35, hex: 23, oct: 43, bin: 100011
$, dec: 36, hex: 24, oct: 44, bin: 100100
%, dec: 37, hex: 25, oct: 45, bin: 100101
&, dec: 38, hex: 26, oct: 46, bin: 100110
', dec: 39, hex: 27, oct: 47, bin: 100111
(, dec: 40, hex: 28, oct: 50, bin: 101000
), dec: 41, hex: 29, oct: 51, bin: 101001
*, dec: 42, hex: 2A, oct: 52, bin: 101010
+, dec: 43, hex: 2B, oct: 53, bin: 101011
,, dec: 44, hex: 2C, oct: 54, bin: 101100
-, dec: 45, hex: 2D, oct: 55, bin: 101101
., dec: 46, hex: 2E, oct: 56, bin: 101110
/, dec: 47, hex: 2F, oct: 57, bin: 101111
0, dec: 48, hex: 30, oct: 60, bin: 110000
1, dec: 49, hex: 31, oct: 61, bin: 110001
2, dec: 50, hex: 32, oct: 62, bin: 110010
3, dec: 51, hex: 33, oct: 63, bin: 110011
4, dec: 52, hex: 34, oct: 64, bin: 110100
5, dec: 53, hex: 35, oct: 65, bin: 110101
6, dec: 54, hex: 36, oct: 66, bin: 110110
7, dec: 55, hex: 37, oct: 67, bin: 110111
8, dec: 56, hex: 38, oct: 70, bin: 111000
9, dec: 57, hex: 39, oct: 71, bin: 111001
:, dec: 58, hex: 3A, oct: 72, bin: 111010
;, dec: 59, hex: 3B, oct: 73, bin: 111011
<, dec: 60, hex: 3C, oct: 74, bin: 111100
=, dec: 61, hex: 3D, oct: 75, bin: 111101
>, dec: 62, hex: 3E, oct: 76, bin: 111110
?, dec: 63, hex: 3F, oct: 77, bin: 111111
@, dec: 64, hex: 40, oct: 100, bin: 1000000
A, dec: 65, hex: 41, oct: 101, bin: 1000001
B, dec: 66, hex: 42, oct: 102, bin: 1000010
C, dec: 67, hex: 43, oct: 103, bin: 1000011
D, dec: 68, hex: 44, oct: 104, bin: 1000100
E, dec: 69, hex: 45, oct: 105, bin: 1000101
...

Learn more

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

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

Last revision 2015/07/28 by SM
#3
AVR and Arduino / Re: Essentials
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
#4
AVR and Arduino / Re: Essentials
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
#5
AVR and Arduino / Re: Essentials
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
#6
AVR and Arduino / Re: Essentials
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

#7
AVR and Arduino / Re: Essentials
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
#8
AVR and Arduino / Re: Essentials
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);
}