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

#25
AVR and Arduino / Re: Essentials
November 16, 2023, 02:29:49 PM
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 นาทีจนกระทั่งตัวจับเวลาถึงศูนย์ หากอีเมลสำคัญมาถึงในช่วงเวลานี้คุณจะพลาดมัน

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

ในบทช่วยสอนนี้ คุณจะได้เรียนรู้วิธีตั้งค่าตัวจับเวลาที่คล้ายกัน
#26
AVR and Arduino / Re: Essentials
November 16, 2023, 11:13:22 AM
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
#27
AVR and Arduino / Re: Essentials
November 16, 2023, 10:53:55 AM
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


#28
AVR and Arduino / Re: Essentials
November 16, 2023, 09:38:25 AM
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
#29
AVR and Arduino / Re: Essentials
November 16, 2023, 08:48:43 AM
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


#30
AVR and Arduino / Re: Essentials
November 15, 2023, 04:22:58 PM
Code

ในโปรแกรมด้านล่าง สิ่งแรกสุดที่คุณทำใน the setup function คือเริ่ม serial communications, ที่ข้อมูล 9600 บิตต่อวินาที ระหว่างบอร์ดและคอมพิวเตอร์ของคุณด้วยบรรทัด:

Serial.begin(9600);

จากนั้น เริ่มต้น digital pin 2, the pin ที่จะอ่านเอาต์พุตจาก button ของคุณ, เป็น an input:

pinMode(2,INPUT);

เมื่อการตั้งค่าของคุณเสร็จสมบูรณ์แล้ว ให้ย้ายไปยัง the main loop ของโค้ดของคุณ เมื่อ button ของคุณถูกกด ไฟ 5 โวลต์จะไหลผ่านวงจรของคุณอย่างอิสระ และเมื่อไม่ได้กดปุ่ม the input pin จะเชื่อมต่อกับกราวด์ผ่านตัวต้านทาน 10k โอห์ม นี่คือ a digital input, ซึ่งหมายความว่าสวิตช์สามารถอยู่ในสถานะอย่างใดอย่างหนึ่งเท่านั้น an on state (ถูกเห็นโดย your Arduino เป็น a "1", หรือ HIGH) หรือ an off state (Arduino ของคุณเห็นเป็น "0" หรือ LOW) โดยไม่มีอะไรอยู่ระหว่างนั้น

สิ่งแรกที่คุณต้องทำใน the main loop ของโปรแกรมของคุณคือสร้างตัวแปรเพื่อเก็บข้อมูลที่เข้ามาจากสวิตช์ของคุณ เนื่องจากข้อมูลที่เข้ามาจากสวิตช์จะเป็นอย่างใดอย่างหนึ่ง "1" หรือ "0" คุณสามารถใช้ an intdatatype เรียกตัวแปรนี้ว่า sensorValue นี้ และตั้งค่ามันให้เท่ากับสิ่งที่อ่านได้บน digital pin 2. คุณสามารถทำทั้งหมดนี้ให้สำเร็จได้ด้วยโค้ดเพียงบรรทัดเดียว:

int sensorValue = digitalRead(2);

เมื่อบอร์ดอ่าน the input แล้ว ทำให้มันพิมพ์ข้อมูลนี้กลับไปที่คอมพิวเตอร์เป็น a decimal value. คุณสามารถดำเนินการนี้ได้ด้วยคำสั่ง Serial.println() ในโค้ดบรรทัดสุดท้ายของเรา:

Serial.println(sensorValue);

ตอนนี้ เมื่อคุณเปิด your Serial Monitor ใน the Arduino Software (IDE), คุณจะเห็นการไหลของ "0" หากสวิตช์ของคุณถูกเปิด หรือ "1" หากสวิตช์ของคุณถูกปิด.

/*
  DigitalReadSerial

  Reads a digital input on pin 2, prints the result to the Serial Monitor

  This example code is in the public domain.

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

// digital pin 2 has a pushbutton attached to it. Give it a name:
int pushButton = 2;

// the setup routine runs once when you press reset:
void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
  // make the pushbutton's pin an input:
  pinMode(pushButton, INPUT);
}

// the loop routine runs over and over again forever:
void loop() {
  // read the input pin:
  int buttonState = digitalRead(pushButton);
  // print out the state of the button:
  Serial.println(buttonState);
  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
#31
AVR and Arduino / Re: Essentials
November 15, 2023, 03:30:35 PM
https://docs.arduino.cc/built-in-examples/basics/DigitalReadSerial

Digital Read Serial

อ่าน a switch, พิมพ์สถานะออกไปยัง the Arduino Serial Monitor.

LAST REVISION: 31/10/2566 21:11

ตัวอย่างนี้แสดงให้คุณเห็นวิธีเฝ้าดูสถานะของ a switch โดยการสร้าง serial communication ระหว่าง your Arduino และ your computer ผ่าน USB.

Hardware Required

  •  Arduino Board
  •  A momentary switch, button, or toggle 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 โวลต์

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

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

Schematic

#32
AVR and Arduino / Re: Essentials
November 15, 2023, 11:22:16 AM
Code

หลังจากที่คุณสร้างวงจรแล้ว ให้เสียบบอร์ด Arduino เข้ากับคอมพิวเตอร์ของคุณ ให้เริ่มซอฟต์แวร์ Arduino (IDE) แล้วป้อนโค้ดด้านล่าง คุณยังอาจโหลดมันได้จากเมนู File/Examples/01.Basics/Blink สิ่งแรกที่คุณทำคือเริ่มต้นพิน LED_BUILTIN เป็นพินเอาท์พุตด้วยบรรทัดนี้

pinMode(LED_BUILTIN, OUTPUT);

ใน the main loop, คุณเปิดไฟ the LED ด้วยบรรทัดนี้:

digitalWrite(LED_BUILTIN, HIGH);

นี้จะจ่ายไฟ 5 volts ให้กับ the LED anode. ซึ่งสร้างความต่างแรงดันไฟฟ้าคร่อมพินของ LED และสว่างขึ้น จากนั้นคุณปิดมันด้วยบรรทัด:

digitalWrite(LED_BUILTIN, LOW);

นั่นจะนำพิน LED_BUILTIN กลับไปที่ 0 โวลต์และปิด LED ในระหว่างการเปิดและปิด คุณต้องการเวลาที่เพียงพอให้บุคคลเห็นการเปลี่ยนแปลง ดังนั้นคำสั่ง delay() จะบอกให้บอร์ดไม่ต้องทำอะไรเลยเป็นเวลา 1,000 มิลลิวินาทีหรือหนึ่งวินาที เมื่อคุณใช้คำสั่ง delay() จะไม่มีอะไรเกิดขึ้นอีกในช่วงเวลาดังกล่าว เมื่อคุณเข้าใจตัวอย่างพื้นฐานแล้ว ลองดูตัวอย่าง BlinkWithoutDelay เพื่อเรียนรู้วิธีสร้าง a delay ขณะทำสิ่งอื่น

เมื่อคุณเข้าใจตัวอย่างนี้แล้ว ลองดูตัวอย่าง DigitalReadSerial เพื่อเรียนรู้วิธีอ่าน a switch ที่เชื่อมต่อกับบอร์ด

/*
  Blink

  Turns an LED on for one second, then off for one second, repeatedly.

  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

  modified 8 May 2014
  by Scott Fitzgerald
  modified 2 Sep 2016
  by Arturo Guadalupi
  modified 8 Sep 2016
  by Colby Newman

  This example code is in the public domain.

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

// the setup function runs once when you press reset or power the board
void setup() {
  // initialize digital pin LED_BUILTIN as an output.
  pinMode(LED_BUILTIN, OUTPUT);
}

// the loop function runs over and over again forever
void loop() {
  digitalWrite(LED_BUILTIN, HIGH);  // turn the LED on (HIGH is the voltage level)
  delay(1000);                      // wait for a second
  digitalWrite(LED_BUILTIN, LOW);   // turn the LED off by making the voltage LOW
  delay(1000);                      // wait for a second
}

Learn more

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

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

Last revision 2015/07/28 by SM