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

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


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


#21
AVR and Arduino / Re: Essentials
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
#22
AVR and Arduino / Re: Essentials
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
#23
AVR and Arduino / Re: Essentials
November 16, 2023, 03:36:35 PM
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
#24
AVR and Arduino / Re: Essentials
November 16, 2023, 03:15:38 PM
Hardware Required

  •  Arduino Board
  •  LED
  •  220 ohm resistor

Circuit



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

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

Schematic



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