Main Menu

Recent posts

#21
AVR and Arduino / Re: Essentials
Last post by tha - November 24, 2023, 09:37:45 AM
https://docs.arduino.cc/built-in-examples/digital/toneMultiple

Tone on Multiple Speakers

เล่นโทนเสียงจากลำโพงหลายตัวตามลำดับโดยใช้คำสั่ง tone().

LAST REVISION: 15/11/2566 22:52

ตัวอย่างนี้แสดงวิธีใช้คำสั่ง tone() เพื่อเล่นโน้ตที่แตกต่างกันบนเอาต์พุตหลายตัว

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

The tone() command ทำงานโดยเข้าควบคุม the Atmega's internal timers ตัวใดตัวหนึ่ง, ตั้งค่ามันให้เป็นความถี่ที่คุณต้องการ และใช้ the timer เพื่อพัลส์ an output pin. เนื่องจากมันใช้ timer เพียงตัวเดียว, คุณจึงสามารถเล่นโน้ตได้ครั้งละหนึ่งตัวเท่านั้น. อย่างไรก็ตาม คุณสามารถเล่นโน้ตบนพินที่แตกต่างตามลำดับได้. ในการดำเนินการนี้ คุณจะต้องปิด the timer สำหรับพินหนึ่งก่อนจึงจะย้ายไปยังพินถัดไปได้.

ขอขอบคุณ Greg Borenstein สำหรับการชี้แจงเรื่องนี้

Hardware Required

  •  Arduino Board
  •  3 8 ohm speakers
  •  3 100 ohm resistor
  •  hook-up wires
  •  breadboard

Circuit



Schematic



Code

The sketch ด้านล่างจะเล่นเสียงบนลำโพงแต่ละตัวตามลำดับ โดยปิดลำโพงตัวก่อนหน้าก่อน โปรดทราบว่าระยะเวลาของแต่ละโทนเสียงจะเท่ากันกับ the delay ที่ตามมันมา

ที่นี่คือ the main sketch:

/*

  Multiple tone player

  Plays multiple tones on multiple pins in sequence

  circuit:

  - three 8 ohm speakers on digital pins 6, 7, and 8

  created 8 Mar 2010

  by Tom Igoe

  based on a snippet from Greg Borenstein

  This example code is in the public domain.

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

*/

void setup() {

}

void loop() {

  // turn off tone function for pin 8:

  noTone(8);

  // play a note on pin 6 for 200 ms:

  tone(6, 440, 200);

  delay(200);

  // turn off tone function for pin 6:

  noTone(6);

  // play a note on pin 7 for 500 ms:

  tone(7, 494, 500);

  delay(500);

  // turn off tone function for pin 7:

  noTone(7);

  // play a note on pin 8 for 300 ms:

  tone(8, 523, 300);

  delay(300);
}

Learn more

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

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

Last revision 2015/08/11 by SM
#22
AVR and Arduino / Re: Essentials
Last post by tha - November 23, 2023, 11:11:05 AM
https://docs.arduino.cc/built-in-examples/digital/toneMelody

Play a Melody using the tone() function

เล่น a melody ด้วย a Piezo speaker.

LAST REVISION: 15/11/2566 22:52

ตัวอย่างนี้แสดงวิธีใช้ the tone() command เพื่อสร้างโน๊ต. มันเล่น a little melody ที่คุณอาจเคยได้ยินมาก่อน.

Hardware Required

  •  Arduino board
  •  piezo buzzer or a speaker
  •  hook-up wires

Circuit


Schematic



Code

The code ด้านล่างใช้ไฟล์พิเศษ pitches.h ไฟล์นี้มีค่าระดับเสียงทั้งหมดสำหรับโน๊ตทั่วไป ตัวอย่างเช่น NOTE_C4 คือ middle C ส่วน NOTE_FS4 คือ F Sharp เป็นต้น ตารางโน๊ตนี้เดิมเขียนโดย Brett Hagman ซึ่งเขาใช้คำสั่ง tone() เป็นพื้นฐาน คุณอาจพบว่ามีประโยชน์เมื่อใดก็ตามที่คุณต้องการทำโน้ตดนตรี

The main sketch เป็นดังต่อไปนี้:

/*

  Melody

  Plays a melody

  circuit:

  - 8 ohm speaker on digital pin 8

  created 21 Jan 2010

  modified 30 Aug 2011

  by Tom Igoe

  This example code is in the public domain.

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

*/

#include "pitches.h"

// notes in the melody:
int melody[] = {

  NOTE_C4, NOTE_G3, NOTE_G3, NOTE_A3, NOTE_G3, 0, NOTE_B3, NOTE_C4
};

// note durations: 4 = quarter note, 8 = eighth note, etc.:
int noteDurations[] = {

  4, 8, 8, 4, 4, 4, 4, 4
};

void setup() {

  // iterate over the notes of the melody:

  for (int thisNote = 0; thisNote < 8; thisNote++) {

    // to calculate the note duration, take one second divided by the note type.

    //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.

    int noteDuration = 1000 / noteDurations[thisNote];

    tone(8, melody[thisNote], noteDuration);

    // to distinguish the notes, set a minimum time between them.

    // the note's duration + 30% seems to work well:

    int pauseBetweenNotes = noteDuration * 1.30;

    delay(pauseBetweenNotes);

    // stop the tone playing:

    noTone(8);

  }
}

void loop() {

  // no need to repeat the melody.
}

เพื่อทำ the pitches.h file, คลิ๊กบน the button ข้างล่าง the serial monitor icon แล้วเลือก "New Tab", หรือใช้ Ctrl+Shift+N อย่างใดอย่างหนึ่ง.

จากนั้นวางโค้ดต่อไปนี้:

/*************************************************

 * Public Constants

 *************************************************/

#define NOTE_B0  31
#define NOTE_C1  33
#define NOTE_CS1 35
#define NOTE_D1  37
#define NOTE_DS1 39
#define NOTE_E1  41
#define NOTE_F1  44
#define NOTE_FS1 46
#define NOTE_G1  49
#define NOTE_GS1 52
#define NOTE_A1  55
#define NOTE_AS1 58
#define NOTE_B1  62
#define NOTE_C2  65
#define NOTE_CS2 69
#define NOTE_D2  73
#define NOTE_DS2 78
#define NOTE_E2  82
#define NOTE_F2  87
#define NOTE_FS2 93
#define NOTE_G2  98
#define NOTE_GS2 104
#define NOTE_A2  110
#define NOTE_AS2 117
#define NOTE_B2  123
#define NOTE_C3  131
#define NOTE_CS3 139
#define NOTE_D3  147
#define NOTE_DS3 156
#define NOTE_E3  165
#define NOTE_F3  175
#define NOTE_FS3 185
#define NOTE_G3  196
#define NOTE_GS3 208
#define NOTE_A3  220
#define NOTE_AS3 233
#define NOTE_B3  247
#define NOTE_C4  262
#define NOTE_CS4 277
#define NOTE_D4  294
#define NOTE_DS4 311
#define NOTE_E4  330
#define NOTE_F4  349
#define NOTE_FS4 370
#define NOTE_G4  392
#define NOTE_GS4 415
#define NOTE_A4  440
#define NOTE_AS4 466
#define NOTE_B4  494
#define NOTE_C5  523
#define NOTE_CS5 554
#define NOTE_D5  587
#define NOTE_DS5 622
#define NOTE_E5  659
#define NOTE_F5  698
#define NOTE_FS5 740
#define NOTE_G5  784
#define NOTE_GS5 831
#define NOTE_A5  880
#define NOTE_AS5 932
#define NOTE_B5  988
#define NOTE_C6  1047
#define NOTE_CS6 1109
#define NOTE_D6  1175
#define NOTE_DS6 1245
#define NOTE_E6  1319
#define NOTE_F6  1397
#define NOTE_FS6 1480
#define NOTE_G6  1568
#define NOTE_GS6 1661
#define NOTE_A6  1760
#define NOTE_AS6 1865
#define NOTE_B6  1976
#define NOTE_C7  2093
#define NOTE_CS7 2217
#define NOTE_D7  2349
#define NOTE_DS7 2489
#define NOTE_E7  2637
#define NOTE_F7  2794
#define NOTE_FS7 2960
#define NOTE_G7  3136
#define NOTE_GS7 3322
#define NOTE_A7  3520
#define NOTE_AS7 3729
#define NOTE_B7  3951
#define NOTE_C8  4186
#define NOTE_CS8 4435
#define NOTE_D8  4699
#define NOTE_DS8 4978

และเซฟมันเป็น pitches.h

Learn more

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

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

Last revision 2015/08/11 by SM
#23
AVR and Arduino / Re: Essentials
Last post by tha - November 23, 2023, 09:52:51 AM
Code

The sketch ด้านล่างอ่าน analog sensors สามตัว แต่ละค่าตรงกันกับ a note value ในอาร์เรย์ของ notes. หากเซ็นเซอร์ตัวใดตัวหนึ่งอยู่เหนือขอบเขตที่กำหนด ตัวโน้ตที่ตรงกันจะถูกเล่น

นี่คือ the main sketch:

/*
  Keyboard

  Plays a pitch that changes based on a changing analog input

  circuit:
  - three force-sensing resistors from +5V to analog in 0 through 5
  - three 10 kilohm resistors from analog in 0 through 5 to ground
  - 8 ohm speaker on digital pin 8

  created 21 Jan 2010
  modified 9 Apr 2012
  by Tom Igoe

  This example code is in the public domain.

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

#include "pitches.h"

const int threshold = 10;  // minimum reading of the sensors that generates a note

// notes to play, corresponding to the 3 sensors:
int notes[] = {
  NOTE_A4, NOTE_B4, NOTE_C3
};

void setup() {
}

void loop() {
  for (int thisSensor = 0; thisSensor < 3; thisSensor++) {
    // get a sensor reading:
    int sensorReading = analogRead(thisSensor);

    // if the sensor is pressed hard enough:
    if (sensorReading > threshold) {
      // play the note corresponding to this sensor:
      tone(8, notes[thisSensor], 20);
    }
  }
}

The sketch ใช้ไฟล์พิเศษ pitches.h ไฟล์นี้มีค่าระดับเสียงทั้งหมดสำหรับโน๊ตทั่วไป ตัวอย่างเช่น NOTE_C4 คือ middle C ส่วน NOTE_FS4 คือ F Sharp เป็นต้น ตารางโน๊ตนี้เดิมเขียนโดย Brett Hagman ซึ่งใช้คำสั่ง tone() เป็นพื้นฐาน คุณอาจพบว่ามีประโยชน์เมื่อใดก็ตามที่คุณต้องการทำโน้ตดนตรี

เพื่อทำ the pitches.h file, คลิ๊กบน the button ข้างล่าง the serial monitor icon แล้วเลือก "New Tab", หรือใช้ Ctrl+Shift+N อย่างใดอย่างหนึ่ง.

จากนั้นวางโค้ดต่อไปนี้:

/*************************************************

 * Public Constants

 *************************************************/

#define NOTE_B0  31
#define NOTE_C1  33
#define NOTE_CS1 35
#define NOTE_D1  37
#define NOTE_DS1 39
#define NOTE_E1  41
#define NOTE_F1  44
#define NOTE_FS1 46
#define NOTE_G1  49
#define NOTE_GS1 52
#define NOTE_A1  55
#define NOTE_AS1 58
#define NOTE_B1  62
#define NOTE_C2  65
#define NOTE_CS2 69
#define NOTE_D2  73
#define NOTE_DS2 78
#define NOTE_E2  82
#define NOTE_F2  87
#define NOTE_FS2 93
#define NOTE_G2  98
#define NOTE_GS2 104
#define NOTE_A2  110
#define NOTE_AS2 117
#define NOTE_B2  123
#define NOTE_C3  131
#define NOTE_CS3 139
#define NOTE_D3  147
#define NOTE_DS3 156
#define NOTE_E3  165
#define NOTE_F3  175
#define NOTE_FS3 185
#define NOTE_G3  196
#define NOTE_GS3 208
#define NOTE_A3  220
#define NOTE_AS3 233
#define NOTE_B3  247
#define NOTE_C4  262
#define NOTE_CS4 277
#define NOTE_D4  294
#define NOTE_DS4 311
#define NOTE_E4  330
#define NOTE_F4  349
#define NOTE_FS4 370
#define NOTE_G4  392
#define NOTE_GS4 415
#define NOTE_A4  440
#define NOTE_AS4 466
#define NOTE_B4  494
#define NOTE_C5  523
#define NOTE_CS5 554
#define NOTE_D5  587
#define NOTE_DS5 622
#define NOTE_E5  659
#define NOTE_F5  698
#define NOTE_FS5 740
#define NOTE_G5  784
#define NOTE_GS5 831
#define NOTE_A5  880
#define NOTE_AS5 932
#define NOTE_B5  988
#define NOTE_C6  1047
#define NOTE_CS6 1109
#define NOTE_D6  1175
#define NOTE_DS6 1245
#define NOTE_E6  1319
#define NOTE_F6  1397
#define NOTE_FS6 1480
#define NOTE_G6  1568
#define NOTE_GS6 1661
#define NOTE_A6  1760
#define NOTE_AS6 1865
#define NOTE_B6  1976
#define NOTE_C7  2093
#define NOTE_CS7 2217
#define NOTE_D7  2349
#define NOTE_DS7 2489
#define NOTE_E7  2637
#define NOTE_F7  2794
#define NOTE_FS7 2960
#define NOTE_G7  3136
#define NOTE_GS7 3322
#define NOTE_A7  3520
#define NOTE_AS7 3729
#define NOTE_B7  3951
#define NOTE_C8  4186
#define NOTE_CS8 4435
#define NOTE_D8  4699
#define NOTE_DS8 4978

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

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

Last revision 2015/08/11 by SM
#24
AVR and Arduino / Re: Essentials
Last post by tha - November 23, 2023, 09:02:57 AM
https://docs.arduino.cc/built-in-examples/digital/toneKeyboard

Simple keyboard using the tone() function

A three-key musical keyboard โดยใช้ force sensors และ a piezo speaker.

LAST REVISION: 15/11/2566 22:52

ตัวอย่างนี้แสดงวิธีใช้คำสั่ง tone() เพื่อสร้างระดับเสียงที่แตกต่างกัน ขึ้นอยู่กับเซ็นเซอร์ที่ถูกกด

Hardware Required

  •  Arduino Board
  •  8 ohm speaker
  •  3 force sensing resistors
  •  3 10k ohm resistors
  •  100 ohm resistor
  •  hook-up wires
  •  breadboard

Circuit

เชื่อมต่อขั้วต่อหนึ่งของลำโพงของคุณเข้ากับ digital pin 8 ผ่านตัวต้านทาน 100 โอห์ม และขั้วต่ออีกขั้วหนึ่งเข้ากับกราวด์

จ่ายไฟให้กับ FSR สามตัวของคุณ (หรือ analog sensor อื่นๆ) ด้วยไฟ 5V ขนานกัน เชื่อมต่อเซ็นเซอร์แต่ละตัวกับ analog pins 0-2 โดยใช้ตัวต้านทาน 10K เป็นตัวอ้างอิงถึงกราวด์บนแต่ละ input line.



Schematic


#25
AVR and Arduino / Re: Essentials
Last post by tha - November 18, 2023, 03:35:23 PM
Code

The sketch ด้านล่างจะอ่านสถานะของ the button อย่างต่อเนื่อง จากนั้นจะเปรียบเทียบสถานะของ the button กับสถานะในครั้งสุดท้ายของมันที่ผ่าน the main loop. หากสถานะของ the button ปัจจุบันแตกต่างจาก the last button state และ the current button state เป็น high จากนั้น the button จะเปลี่ยนจากปิดเป็นเปิด จากนั้น The sketch จะเพิ่ม a button push counter.

The sketch ยังตรวจสอบค่าของ  the button push counter อีกด้วยและหากมันเป็นผลคูณของสี่ ไฟ LED บนพิน 13 จะเปิด ถ้าเป็นอย่างอื่นมันก็จะปิด

/*
  State change detection (edge detection)

  Often, you don't need to know the state of a digital input all the time, but
  you just need to know when the input changes from one state to another.
  For example, you want to know when a button goes from OFF to ON. This is called
  state change detection, or edge detection.

  This example shows how to detect when a button or button changes from off to on
  and on to off.

  The circuit:
  - pushbutton attached to pin 2 from +5V
  - 10 kilohm resistor attached to pin 2 from ground
  - LED attached from pin 13 to ground through 220 ohm resistor (or use the
    built-in LED on most Arduino boards)

  created  27 Sep 2005
  modified 30 Aug 2011
  by Tom Igoe

  This example code is in the public domain.

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

// this constant won't change:
const int buttonPin = 2;  // the pin that the pushbutton is attached to
const int ledPin = 13;    // the pin that the LED is attached to

// Variables will change:
int buttonPushCounter = 0;  // counter for the number of button presses
int buttonState = 0;        // current state of the button
int lastButtonState = 0;    // previous state of the button

void setup() {
  // initialize the button pin as a input:
  pinMode(buttonPin, INPUT);
  // initialize the LED as an output:
  pinMode(ledPin, OUTPUT);
  // initialize serial communication:
  Serial.begin(9600);
}


void loop() {
  // read the pushbutton input pin:
  buttonState = digitalRead(buttonPin);

  // compare the buttonState to its previous state
  if (buttonState != lastButtonState) {
    // if the state has changed, increment the counter
    if (buttonState == HIGH) {
      // if the current state is HIGH then the button went from off to on:
      buttonPushCounter++;
      Serial.println("on");
      Serial.print("number of button pushes: ");
      Serial.println(buttonPushCounter);
    } else {
      // if the current state is LOW then the button went from on to off:
      Serial.println("off");
    }
    // Delay a little bit to avoid bouncing
    delay(50);
  }
  // save the current state as the last state, for next time through the loop
  lastButtonState = buttonState;


  // turns on the LED every four button pushes by checking the modulo of the
  // button push counter. the modulo function gives you the remainder of the
  // division of two numbers:
  if (buttonPushCounter % 4 == 0) {
    digitalWrite(ledPin, HIGH);
  } else {
    digitalWrite(ledPin, LOW);
  }
}

Learn more

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

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

Last revision 2015/07/28 by SM
#26
AVR and Arduino / Re: Essentials
Last post by tha - November 18, 2023, 02:45:59 PM
https://docs.arduino.cc/built-in-examples/digital/StateChangeDetection

State Change Detection (Edge Detection) for pushbuttons

นับจำนวนครั้งของ button ที่กด.

LAST REVISION: 15/11/2566 22:52

เมื่อคุณได้การทำงานของ pushbutton แล้ว คุณมักจะต้องการดำเนินการบางอย่างโดยพิจารณาจากจำนวนครั้งที่ the button ถูกกด ในการดำเนินการนี้ คุณจำเป็นต้องทราบว่าเมื่อใด the button เปลี่ยนสถานะจากปิดเป็นเปิด และนับจำนวนครั้งที่การเปลี่ยนแปลงสถานะนี้เกิดขึ้น สิ่งนี้เรียกว่า  state change detection หรือ edge detection. ในบทช่วยสอนนี้ เราได้เรียนรู้วิธีตรวจสอบการเปลี่ยนแปลงสถานะ เราจะส่งข้อความไปยัง Serial Monitor พร้อมข้อมูลที่เกี่ยวข้อง และเรานับการเปลี่ยนแปลงสถานะสี่ครั้งเพื่อเปิดและปิด LED

Hardware Required

  •  Arduino Board
  •  momentary button or switch
  •  10k ohm resistor
  •  hook-up wires
  •  breadboard

Circuit



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

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

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

Schematic


#27
AVR and Arduino / Re: Essentials
Last post by tha - November 18, 2023, 11:45:08 AM
Code

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

Serial.begin(9600);

ถัดไป, เริ่มต้น digital pin 2 เป็น an input ที่มี the internal pull-up resistor ถูกเปิดใช้งาน:

pinMode(2,INPUT_PULLUP);

บรรทัดต่อไปนี้ทำ pin 13, ที่มี the onboard LED, เป็น an output :

pinMode(13, OUTPUT);

ตอนนี้ setup ของคุณเสร็จสมบูรณ์แล้ว, ย้ายลงใน the main loop ของ your code. เมื่อ your button ไม่ถูกกด, the internal pull-up resistor ต่ออยู่กับ 5 volts. นี้เป็นเหตุให้ the Arduino รายงาน "1" หรือ HIGH. เมื่อ the button ถูกกด, the Arduino pin ถูกดึงลงกราวด์, เป็นเหตุให้ the Arduino รายงาน "0", หรือ LOW.

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

int sensorValue = digitalRead(2);

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

Serial.println(sensorValue, DEC);

ตอนนี้, เมื่อคุณเปิด your Serial Monitor ใน the Arduino environment, คุณจะเห็นการไหลของ "0"s ถ้า your switch ถูกปิด, หรือ "1"s ถ้า your switch ถูกเปิด.

The LED บน pin 13 จะสว่างเมื่อ the switch เป็น HIGH, และ turn off เมื่อ LOW.

/*
  Input Pull-up Serial

  This example demonstrates the use of pinMode(INPUT_PULLUP). It reads a digital
  input on pin 2 and prints the results to the Serial Monitor.

  The circuit:
  - momentary switch attached from pin 2 to ground
  - built-in LED on pin 13

  Unlike pinMode(INPUT), there is no pull-down resistor necessary. An internal
  20K-ohm resistor is pulled to 5V. This configuration causes the input to read
  HIGH when the switch is open, and LOW when it is closed.

  created 14 Mar 2012
  by Scott Fitzgerald

  This example code is in the public domain.

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

void setup() {
  //start serial connection
  Serial.begin(9600);
  //configure pin 2 as an input and enable the internal pull-up resistor
  pinMode(2, INPUT_PULLUP);
  pinMode(13, OUTPUT);
}

void loop() {
  //read the pushbutton value into a variable
  int sensorVal = digitalRead(2);
  //print out the value of the pushbutton
  Serial.println(sensorVal);

  // Keep in mind the pull-up means the pushbutton's logic is inverted. It goes
  // HIGH when it's open, and LOW when it's pressed. Turn on pin 13 when the
  // button's pressed, and off when it's not:
  if (sensorVal == HIGH) {
    digitalWrite(13, LOW);
  } else {
    digitalWrite(13, HIGH);
  }
}

Learn more

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

คุณยังสามารถสำรวจ the language reference, ซึ่งเป็นคอลเล็กชันรายละเอียดของ the Arduino programming language.
#28
AVR and Arduino / Re: Essentials
Last post by tha - November 18, 2023, 11:04:36 AM
https://docs.arduino.cc/built-in-examples/digital/InputPullupSerial

InputPullupSerial

สาธิตการใช้ INPUT_PULLUP กับ pinMode()

LAST REVISION: 15/11/2566 22:52

Input Pullup Serial

ตัวอย่างนี้สาธิตการใช้ INPUT_PULLUP กับ pinMode() มันเฝ้าดูสถานะของสวิตช์โดยการสร้าง serial communication ระหว่าง Arduino ของคุณและคอมพิวเตอร์ของคุณผ่าน USB

นอกจากนี้ เมื่ออินพุตเป็น HIGH ไฟ LED ออนบอร์ดที่ต่อกับพิน 13 จะเปิดขึ้น เมื่อ LOW ไฟ LED จะดับลง

Hardware Required

  •  Arduino Board
  •  A momentary switch, button, or toggle switch
  •  breadboard
  •  hook-up wire

Circuit



ต่อสายสองเส้นเข้ากับ the Arduino board. The black wire ที่ต่อ ground ไปยังขาหนึ่งของ the pushbutton. สายที่สองไปจาก digital pin 2 ไปยังอีกขาหนึ่งของ the pushbutton.

Pushbuttons หรือ switches ต่อสองจุดในวงจรเมื่อคุณกดพวกมัน. เมื่อ the pushbutton เปิดอยู่ (ไม่ถูกกด) จะไม่มีการต่อระหว่างของขาของ the pushbutton. เนื่องจาก the internal pull-up บน pin 2 ทำงานและต่อกับ 5V, เราจะอ่าน HIGH เมื่อ the button เปิดอยู่. เมื่อ the button ถูดปิด, the Arduino อ่าน LOW เนื่องจากการต่อถึงกราวด์เสร็จสมบูรณ์.

Schematic


#29
AVR and Arduino / Re: Essentials
Last post by tha - November 17, 2023, 10:15:24 AM
https://docs.arduino.cc/built-in-examples/digital/Debounce

Debounce on a Pushbutton

อ่าน a pushbutton, กรองสัญญานรบกวน.

LAST REVISION: 15/11/2566 22:52

Pushbuttons มักจะสร้างการเปลี่ยนการเปิด/ปิดปลอมเมื่อกด เนื่องจากปัญหาทางกลไกและทางกายภาพ การเปลี่ยนเหล่านี้อาจถูกอ่านเป็นการกดหลายครั้งในเวลาอันสั้นมาก ซึ่งเป็นการหลอกลวงโปรแกรม ตัวอย่างนี้สาธิตวิธีการ debounce อินพุต ซึ่งหมายถึงการตรวจสอบสองครั้งในช่วงเวลาสั้นๆ เพื่อให้แน่ใจว่ามีการกดปุ่มอย่างแน่นอน หากไม่มีการ debouncing, การกดปุ่มหนึ่งครั้งอาจทำให้เกิดผลลัพธ์ที่คาดเดาไม่ได้  sketch นี้ใช้ฟังก์ชัน millis() เพื่อติดตามเวลาที่ผ่านไปนับตั้งแต่ที่ the button ถูกกด

Hardware Required

  •  Arduino Board
  •  momentary button or switch
  •  10k ohm resistor
  •  hook-up wires
  •  breadboard

Circuit



Schematic



Code

The sketch ข้างล่างอิงตาม Limor Fried's version of debounce, แต่ the logic จะกลับกันจากตัวอย่างของเธอ. ในตัวอย่างของเธอ สวิตช์จะส่งกลับค่า LOW เมื่อปิด และค่า HIGH เมื่อเปิด ที่นี่สวิตช์จะส่งกลับค่า HIGH เมื่อกด และ LOW เมื่อไม่ได้กด.

/*
  Debounce

  Each time the input pin goes from LOW to HIGH (e.g. because of a push-button
  press), the output pin is toggled from LOW to HIGH or HIGH to LOW. There's a
  minimum delay between toggles to debounce the circuit (i.e. to ignore noise).

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

  - Note: On most Arduino boards, there is already an LED on the board connected
    to pin 13, so you don't need any extra components for this example.

  created 21 Nov 2006
  by David A. Mellis
  modified 30 Aug 2011
  by Limor Fried
  modified 28 Dec 2012
  by Mike Walters
  modified 30 Aug 2016
  by Arturo Guadalupi

  This example code is in the public domain.

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

// 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 ledState = HIGH;        // the current state of the output pin
int buttonState;            // the current reading from the input pin
int lastButtonState = LOW;  // the previous reading from the input pin

// the following variables are unsigned longs because the time, measured in
// milliseconds, will quickly become a bigger number than can be stored in an int.
unsigned long lastDebounceTime = 0;  // the last time the output pin was toggled
unsigned long debounceDelay = 50;    // the debounce time; increase if the output flickers

void setup() {
  pinMode(buttonPin, INPUT);
  pinMode(ledPin, OUTPUT);

  // set initial LED state
  digitalWrite(ledPin, ledState);
}

void loop() {
  // read the state of the switch into a local variable:
  int reading = digitalRead(buttonPin);

  // check to see if you just pressed the button
  // (i.e. the input went from LOW to HIGH), and you've waited long enough
  // since the last press to ignore any noise:

  // If the switch changed, due to noise or pressing:
  if (reading != lastButtonState) {
    // reset the debouncing timer
    lastDebounceTime = millis();
  }

  if ((millis() - lastDebounceTime) > debounceDelay) {
    // whatever the reading is at, it's been there for longer than the debounce
    // delay, so take it as the actual current state:

    // if the button state has changed:
    if (reading != buttonState) {
      buttonState = reading;

      // only toggle the LED if the new button state is HIGH
      if (buttonState == HIGH) {
        ledState = !ledState;
      }
    }
  }

  // set the LED:
  digitalWrite(ledPin, ledState);

  // save the reading. Next time through the loop, it'll be the lastButtonState:
  lastButtonState = reading;
}

Learn more

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

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

Last revision 2015/07/29 by SM
#30
AVR and Arduino / Re: Essentials
Last post by tha - November 17, 2023, 08:59:14 AM
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