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

#9
AVR and Arduino / Re: Essentials
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


#10
AVR and Arduino / Re: Essentials
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
#11
AVR and Arduino / Re: Essentials
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

#12
AVR and Arduino / Re: Essentials
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
#13
AVR and Arduino / Re: Essentials
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
#14
AVR and Arduino / Re: Essentials
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
#15
AVR and Arduino / Re: Essentials
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
#16
AVR and Arduino / Re: Essentials
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