Essentials

Started by tha, November 13, 2023, 09:48:47 AM

Previous topic - Next topic

tha

https://docs.arduino.cc/built-in-examples/basics/Fade

Fading a LED

สาธิตการใช้ analog output เพื่อจางลง an LED.

LAST REVISION: 31/10/2566 21:11

ตัวอย่างนี้สาธิตการใช้ฟังก์ชัน analogWrite() ในการหรี่ไฟ LED ปิดและเปิด AnalogWrite ใช้ pulse width modulation (PWM), เพื่อเปิดและปิด digital pin อย่างรวดเร็วด้วยอัตราส่วนระหว่างการเปิดและปิดที่แตกต่างกัน เพื่อสร้าง a fading effect.

Hardware Required

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

Circuit

เชื่อมต่อขั้วบวก (ขาบวกที่ยาวกว่า) ของ LED ของคุณเข้ากับ digital output pin 9 บนบอร์ดผ่านตัวต้านทาน 220 โอห์ม เชื่อมต่อแคโทด (ขาขั้วลบที่สั้นกว่า) เข้ากับกราวด์โดยตรง



Schematic



tha

Code

หลังจากประกาศพิน 9 เป็น ledPin ของคุณแล้ว คุณจะไม่ต้องทำอะไรใน the setup() function ของโค้ดของคุณ

The analogWrite() function ที่คุณจะใช้ใน the main loop ของโค้ดของคุณจำเป็นต้องมี two arguments: ตัวหนึ่งบอกฟังก์ชันว่าพินไหนที่จะเขียนถึง และอีกตัวระบุว่าค่า PWM เท่าไหร่ที่จะเขียน.

เพื่อให้ LED ของคุณปิดจางลงและเปิดขึ้น ให้ค่อยๆ เพิ่มค่า PWM ของคุณจาก 0 (สุดทาง off) เป็น 255 (สุดทาง on) จากนั้นกลับเป็น 0 อีกครั้งเพื่อให้ the cycle เสร็จสมบูรณ์ ใน the sketch ด้านล่าง ค่า PWM จะถูกตั้งค่าโดยใช้ตัวแปรที่เรียกว่า brightness แต่ละครั้งที่วนซ้ำ ค่านั้นจะเพิ่มขึ้นตามค่าของตัวแปร fadeAmount

หาก  brightness อยู่ที่ค่าสุดทางทางใดทางหนึ่ง (0 หรือ 255) fadeAmount จะเปลี่ยนเป็นค่าลบของมัน กล่าวอีกนัยหนึ่ง หาก fadeAmount เป็น 5 จะถูกตั้งค่าเป็น -5 หากเป็น -5 จะถูกตั้งค่าเป็น 5 ครั้งถัดไปที่ผ่าน the loop, การเปลี่ยนแปลงนี้จะทำให้ brightness เปลี่ยนทิศทางเช่นกัน

analogWrite() สามารถเปลี่ยนค่า PWM ได้อย่างรวดเร็ว ดังนั้น the delay ที่จุดสิ้นสุด  the sketch จะควบคุมความเร็วของ the fade. ลองเปลี่ยนค่าของ  the delay และดูว่ามันเปลี่ยน the fading effect. อย่างไร

/*
  Fade

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

  The analogWrite() function uses PWM, so if you want to change the pin you're
  using, be sure to use another PWM capable pin. On most Arduino, the PWM pins
  are identified with a "~" sign, like ~3, ~5, ~6, ~9, ~10 and ~11.

  This example code is in the public domain.

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

int led = 9;         // the PWM pin the LED is attached to
int brightness = 0;  // how bright the LED is
int fadeAmount = 5;  // how many points to fade the LED by

// the setup routine runs once when you press reset:
void setup() {
  // declare pin 9 to be an output:
  pinMode(led, OUTPUT);
}

// the loop routine runs over and over again forever:
void loop() {
  // set the brightness of pin 9:
  analogWrite(led, 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);
}

Learn more

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

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

Last revision 2015/07/29 by SM

tha

https://docs.arduino.cc/built-in-examples/basics/ReadAnalogVoltage

Read Analog Voltage

อ่าน an analog input และพิมพ์ the voltage ไปยัง the Serial Monitor.

LAST REVISION: 10/31/2023, 02:11 PM

ตัวอย่างนี้แสดงให้คุณเห็นวิธีการอ่าน an analog input บน analog pin 0, แปลงค่าจาก analogRead() เป็นแรงดันไฟฟ้า และพิมพ์มันออกมาบน the serial monitor ของ the Arduino Software (IDE).

Hardware Required

  •  Arduino Board
  •  10k ohm potentiometer

Circuit



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

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

The microcontroller ของ the board มีวงจรอยู่ภายในที่เรียกว่า an analog-to-digital converter หรือ ADC ซึ่งจะอ่านแรงดันไฟฟ้าที่เปลี่ยนแปลงนี้และแปลงมันเป็นตัวเลขระหว่าง 0 ถึง 1,023 เมื่อแกนถูกหมุนไปจนสุดในทิศทางเดียว จะมีแรงดันไฟฟ้า 0 โวลต์ ไปที่พิน โดยค่าอินพุตเป็น 0 เมื่อหมุนแกนไปในทิศทางตรงกันข้ามจนสุด จะมีไฟ 5 โวลต์ไปที่พิน และค่าอินพุตเป็น 1023 ในระหว่างนั้น analogRead() จะส่งคืนตัวเลขระหว่าง 0 และ 1,023 ที่เป็นสัดส่วนกับปริมาณแรงดันไฟฟ้าที่กำลังปรากฎกับพิน

Schematic




tha

Code

ใน the sketch ข้างล่าง, สิ่งแรกสุดที่คุณทำใน the setup function คือเริ่มต้น serial communications, ที่ 9600 bits of data ต่อ second, ระหว่าง your board และ your computer ด้วยคำสั่ง:

Serial.begin(9600);

ถัดไป, ใน the main loop ของ your code, คุณจำเป็นต้องสร้างตัวแปรเพื่อเก็บ the resistance value (ซึ่งจะอยู่ระหว่าง 0 และ 1023, เหมาะสมคือ an int datatype) ที่มาจาก your potentiometer:

int sensorValue = analogRead(A0);

เพื่อเปลี่ยนค่าจาก 0-1023 เป็นช่วงที่ตรงกันกับแรงดันไฟฟ้าที่พินกำลังอ่าน คุณจะต้องสร้างตัวแปรอีกตัวหนึ่ง, a float, และทำการคำนวณเลขเล็กน้อย หากต้องการปรับขนาดตัวเลขให้อยู่ระหว่าง 0.0 ถึง 5.0 ให้หาร 5.0 ด้วย 1,023.0 และคูณด้วย sensorValue :

float voltage= sensorValue * (5.0 / 1023.0);

สุดท้ายนี้ คุณจำเป็นต้องพิมพ์ข้อมูลนี้ไปยัง your serial monitor window. คุณสามารถทำสิ่งนี้ด้วยคำสั่ง Serial.println() ในโค้ดบรรทัดสุดท้าย:

Serial.println(voltage)

ตอนนี้ เมื่อคุณเปิด your Serial Monitor ใน the Arduino Software (IDE) (โดยการคลิ๊ก the icon ที่ดูเหมือน a lens, บนด้านขวา, ใน the green top bar หรือโดยใช้ the keyboard shortcut Ctrl+Shift+M), คุณจะเห็น ตัวเลขที่ไหลต่อเนื่องกันช่วงจาก 0.0 - 5.0. ตามที่คุณหมุน the pot. ค่าก็จะเปลี่ยน ตรงกันกับ the voltage ที่มาลงใน pin A0.

/*
  ReadAnalogVoltage

  Reads an analog input on pin 0, converts it to voltage, and prints the result to the Serial Monitor.
  Graphical representation is available using Serial Plotter (Tools > Serial Plotter menu).
  Attach the center pin of a potentiometer to pin A0, and the outside pins to +5V and ground.

  This example code is in the public domain.

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

// the setup routine runs once when you press reset:
void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
}

// the loop routine runs over and over again forever:
void loop() {
  // read the input on analog pin 0:
  int sensorValue = analogRead(A0);
  // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
  float voltage = sensorValue * (5.0 / 1023.0);
  // print out the value you read:
  Serial.println(voltage);
}

Learn more

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

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

Last revision Nov. 20, 2022 by Killaship

tha

https://docs.arduino.cc/built-in-examples/digital/BlinkWithoutDelay

Digital

Blink Without Delay

กระพริบ an LED โดยไม่ต้องใช้ the delay() function.

LAST REVISION: 31/10/2566 21:11

บางครั้งคุณจำเป็นต้องทำสองสิ่งพร้อมกัน ตัวอย่างเช่น คุณอาจต้องการกระพริบไฟ LED ขณะที่อ่านการกดปุ่ม ในกรณีนี้ คุณไม่สามารถใช้ delay()ได้ เนื่องจาก Arduino จะหยุดโปรแกรมของคุณชั่วคราวในระหว่าง the delay(). หาก the button ถูกกดในขณะที่ Arduino หยุดชั่วคราวเพื่อรอให้ the delay() ผ่านไป โปรแกรมของคุณจะพลาด the button press.

sketch นี้สาธิตวิธีการกะพริบไฟ LED โดยไม่ใช้ delay(). มันจะเปิดไฟ LED แล้วจดบันทึกเวลา จากนั้นแต่ละครั้งที่ผ่าน loop(), มันจะตรวจสอบเพื่อดูว่าเวลากะพริบที่ต้องการผ่านไปแล้วหรือไม่ หากมันผ่านไป ระบบจะเปิดหรือปิด LED และจดบันทึก the new time. ด้วยวิธีนี้ LED จะกะพริบอย่างต่อเนื่องในขณะที่ the sketch execution จะไม่ล่าช้าบนคำสั่งเดียว

การอุปมาเหมือนการอุ่นพิซซ่าในไมโครเวฟและรออีเมลสำคัญด้วย คุณใส่พิซซ่าในไมโครเวฟแล้วตั้งไว้ 10 นาที อุปมาการใช้ delay() คือการนั่งอยู่หน้าไมโครเวฟดูตัวจับเวลานับถอยหลังจาก 10 นาทีจนกระทั่งตัวจับเวลาถึงศูนย์ หากอีเมลสำคัญมาถึงในช่วงเวลานี้คุณจะพลาดมัน

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

ในบทช่วยสอนนี้ คุณจะได้เรียนรู้วิธีตั้งค่าตัวจับเวลาที่คล้ายกัน

tha

Hardware Required

  •  Arduino Board
  •  LED
  •  220 ohm resistor

Circuit



เพื่อสร้างวงจร ให้เชื่อมต่อปลายด้านหนึ่งของตัวต้านทานเข้ากับ pin 13 ของบอร์ด เชื่อมต่อขายาวของ LED (ขาบวกเรียกว่าขั้วอาโนด) เข้ากับปลายอีกด้านของตัวต้านทาน เชื่อมต่อขาสั้นของ LED (ขาลบเรียกว่าแคโทด) เข้ากับบอร์ด GND ดังแสดงในแผนภาพด้านบนและแผนผังด้านล่าง

บอร์ด Arduino ส่วนใหญ่มีไฟ LED ติดไว้ที่ขา 13 บนบอร์ดอยู่แล้ว หากคุณรันตัวอย่างนี้โดยไม่ได้ต่อฮาร์ดแวร์ไว้ คุณก็จะเห็นไฟ LED กะพริบ

Schematic



หลังจากที่คุณสร้างวงจรแล้ว ให้เสียบบอร์ดของคุณเข้ากับคอมพิวเตอร์ ให้เริ่ม the Arduino Software (IDE),  และป้อนโค้ดด้านล่าง

tha

Code

โค้ดด้านล่างใช้ฟังก์ชัน millis() ซึ่งเป็นคำสั่งที่ส่งคืนกลับจำนวนมิลลิวินาทีนับตั้งแต่บอร์ดเริ่มรัน sketch ปัจจุบันของมัน เพื่อกะพริบไฟ LED

/*
  Blink without Delay

  Turns on and off a light emitting diode (LED) connected to a digital pin,
  without using the delay() function. This means that other code can run at the
  same time without being interrupted by the LED code.

  The circuit:
  - Use the onboard LED.
  - Note: Most Arduinos have an on-board LED you can control. On the UNO, MEGA
    and ZERO it is attached to digital pin 13, on MKR1000 on pin 6. LED_BUILTIN
    is set to the correct LED pin independent of which board is used.
    If you want to know what pin the on-board LED is connected to on your
    Arduino model, check the Technical Specs of your board at:
    https://www.arduino.cc/en/Main/Products

  created 2005
  by David A. Mellis
  modified 8 Feb 2010
  by Paul Stoffregen
  modified 11 Nov 2013
  by Scott Fitzgerald
  modified 9 Jan 2017
  by Arturo Guadalupi

  This example code is in the public domain.

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

// constants won't change. Used here to set a pin number:
const int ledPin = LED_BUILTIN;  // the number of the LED pin

// Variables will change:
int ledState = LOW;  // ledState used to set the LED

// Generally, you should use "unsigned long" for variables that hold time
// The value will quickly become too large for an int to store
unsigned long previousMillis = 0;  // will store last time LED was updated

// constants won't change:
const long interval = 1000;  // interval at which to blink (milliseconds)

void setup() {
  // set the digital pin as output:
  pinMode(ledPin, OUTPUT);
}

void loop() {
  // here is where you'd put code that needs to be running all the time.

  // check to see if it's time to blink the LED; that is, if the difference
  // between the current time and last time you blinked the LED is bigger than
  // the interval at which you want to blink the LED.
  unsigned long currentMillis = millis();

  if (currentMillis - previousMillis >= interval) {
    // save the last time you blinked the LED
    previousMillis = currentMillis;

    // if the LED is off turn it on and vice-versa:
    if (ledState == LOW) {
      ledState = HIGH;
    } else {
      ledState = LOW;
    }

    // set the LED with the ledState of the variable:
    digitalWrite(ledPin, ledState);
  }
}

Learn more

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

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

Last revision 2015/07/28 by SM

tha

https://docs.arduino.cc/built-in-examples/digital/Button

How to Wire and Program a Button

เรียนรู้วิธีวางสายและโปรแกรม a pushbutton เพื่อควบคุม an LED.

LAST REVISION: 15/11/2566 22:52

Pushbuttons หรือ switches ต่อสองจุดในวงจรเมื่อคุณกดพวกมัน. ตัวอย่างนี้จะเปิด the built-in LED บน pin 13 เมื่อคุณกด the button.

Hardware

  •  Arduino Board
  •  Momentary button or Switch
  •  10K ohm resistor
  •  hook-up wires
  •  breadboard

Circuit



เชื่อมต่อสายไฟสามเส้นเข้ากับบอร์ด สายสองเส้นแรก สีแดงและสีดำ เชื่อมต่อกับแถวแนวตั้งยาวสองแถวที่ด้านข้างของ the breadboard เพื่อจัดให้เข้าถึงแหล่งจ่ายไฟ 5V และกราวด์ ได้ สายที่สามต่อจาก digital pin 2 ไปยังขาข้างหนึ่งของปุ่มกด ขาเดียวกันของ the button นั้นเชื่อมต่อผ่าน a pull-down resistor (ที่นี่ 10k โอห์ม) ลงกราวด์ ขาอีกข้างของ the button เชื่อมต่อกับแหล่งจ่ายไฟ 5 โวลต์

เมื่อ the pushbutton เปิดอยู่ (ไม่ได้กด) จะไม่มีการเชื่อมต่อระหว่างขาทั้งสองข้างของ the pushbutton, ดังนั้น  the pin จึงเชื่อมต่อกับกราวด์ (ผ่าน  the pull-down resistor) และอ่านเป็น LOW หรือ 0. เมื่อ the button ถูกปิด (กด ) มันจะทำการเชื่อมต่อระหว่างขาทั้งสองข้างของมันเชื่อมต่อ the pin เข้ากับไฟ 5 โวลต์ ดังนั้น the pin อ่านเป็น HIGH หรือ 1

คุณยังสามารถต่อวงจรนี้ในทิศทางตรงกันข้ามได้ ด้วย a pullup resistorจะรักษาอินพุตไว้เป็น HIGH และจะต่อเป็น LOW เมื่อ the button ถูกกด หากเป็นเช่นนั้น พฤติกรรมของ the sketch จะกลับกัน โดยที่ LED ปกติจะเปิดและจะปิดเมื่อคุณกด the button.

หากคุณถอด the digital i/o pin ออกจากทุกสิ่ง ไฟ LED อาจกระพริบผิดปกติ เนื่องจาก the input เป็น "การลอย"  กล่าวคือ มันไม่มีการเชื่อมต่อที่มั่นคงกับแรงดันไฟฟ้าหรือกราวด์ และมันจะส่งคืนค่าอย่างใดอย่างหนึ่ง HIGH หรือ LOW แบบสุ่ม นั่นเป็นสาเหตุว่าทำไมคุณต้องมี a pull-up หรือ pull-down resistor ในวงจร

Schematic



Code

/*
  Button

  Turns on and off a light emitting diode(LED) connected to digital pin 13,
  when pressing a pushbutton attached to pin 2.

  The circuit:
  - LED attached from pin 13 to ground through 220 ohm resistor
  - pushbutton attached to pin 2 from +5V
  - 10K resistor attached to pin 2 from ground

  - Note: on most Arduinos there is already an LED on the board
    attached to pin 13.

  created 2005
  by DojoDave <http://www.0j0.org>
  modified 30 Aug 2011
  by Tom Igoe

  This example code is in the public domain.

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

// constants won't change. They're used here to set pin numbers:
const int buttonPin = 2;  // the number of the pushbutton pin
const int ledPin = 13;    // the number of the LED pin

// variables will change:
int buttonState = 0;  // variable for reading the pushbutton status

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);
}

void loop() {
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
  if (buttonState == HIGH) {
    // turn LED on:
    digitalWrite(ledPin, HIGH);
  } else {
    // turn LED off:
    digitalWrite(ledPin, LOW);
  }
}

Learn more

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

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

Last revision 2015/07/28 by SM