โปรเจกต์ Arduino ง่ายๆ: ควบคุมเซอร์โวด้วยปุ่มกด 180 องศาไปที่ 0 และกลับมา
บทนำ
บทความนี้อธิบายโปรเจกต์ Arduino ง่ายๆ ที่ใช้ปุ่มกดเพื่อควบคุมมอเตอร์เซอร์โว มอเตอร์เซอร์โว คือมอเตอร์ที่มีความแม่นยำซึ่งสามารถหมุนไปยังตำแหน่งที่กำหนดได้ ทำให้เหมาะสำหรับการใช้งาน เช่น หุ่นยนต์และระบบอัตโนมัติ โปรเจกต์นี้จะทำให้เซอร์โวหมุนจาก 0 ถึง 180 องศาเมื่อกดปุ่ม และกลับไปที่ 0 เมื่อปล่อยปุ่ม
ส่วนประกอบและการเดินสายไฟของโปรเจกต์
คุณจะต้องใช้บอร์ด Arduino มอเตอร์เซอร์โว และปุ่มกด
คำแนะนำการเดินสายไฟ:
- มอเตอร์เซอร์โว: เชื่อมต่อ สายสัญญาณ (โดยปกติเป็นสีเหลือง/ส้ม) เข้ากับ พิน 3 ของ Arduino สายไฟ (สีแดง) เข้ากับ 5V และ สายกราวด์ (สีดำ/น้ำตาล) เข้ากับ GND
- ปุ่มกด: เชื่อมต่อขาหนึ่งเข้ากับ ดิจิทัลพิน 2 และอีกขาหนึ่งเข้ากับพิน GND เท่านั้นเอง! โดยการใช้ตัวต้านทานดึงขึ้นภายในของ Arduino คุณไม่จำเป็นต้องใช้ตัวต้านทานภายนอก
คำอธิบายโค้ด
โปรแกรมถูกแบ่งออกเป็นสามส่วนหลัก ได้แก่ การกำหนดพิน ฟังก์ชัน setup() และฟังก์ชัน loop()
การกำหนดพิน
ส่วนนี้จะตั้งค่าไลบรารีที่จำเป็นและกำหนดชื่อที่มีความหมายให้กับพินที่คุณใช้
#include <Servo.h>
const int servoPin = 3;
const int buttonPin = 2;
Servo myServo;
ฟังก์ชัน setup()
ฟังก์ชัน setup() มีโค้ดที่ทำงานเพียง ครั้งเดียว เมื่อบอร์ด Arduino ถูกเปิดเครื่องหรือรีเซ็ต ใช้สำหรับการกำหนดค่าเริ่มต้น
void setup() {
myServo.attach(servoPin);
pinMode(buttonPin, INPUT_PULLUP);
myServo.write(0);
}
การเปลี่ยนแปลงที่สำคัญคือ pinMode(buttonPin, INPUT_PULLUP); ซึ่งจะเปิดใช้งานตัวต้านทานภายในที่ดึงแรงดันไฟฟ้าของพินให้เป็น HIGH เมื่อคุณกดปุ่ม จะเชื่อมต่อพินเข้ากับกราวด์ และแรงดันไฟฟ้าจะลดลงเป็น LOW
ฟังก์ชัน loop()
ฟังก์ชัน loop() มีตรรกะหลักของโปรแกรมและทำงาน ซ้ำๆ ตลอดไปหลังจากฟังก์ชัน setup() เสร็จสิ้น
void loop() {
int buttonState = digitalRead(buttonPin);
if (buttonState == LOW) {
myServo.write(180);
} else {
myServo.write(0);
}
}
ตรรกะที่นี่ถูกกลับด้านแล้ว เนื่องจากการกดปุ่มส่งผลให้เกิดสัญญาณ LOW จึงใช้เงื่อนไข if (buttonState == LOW) เพื่อเคลื่อนเซอร์โวไปที่ 180 องศา
#include <Servo.h>
/*
This is sketch for controlling servo motor. when push button is pressed, the servo moves to 180 degreees and stays there. when the push button is released, the servo motor returns back to 0 degree.
*/
// Define the pin for the servo motor
const int servoPin = 3;
// Define the pin for the push button
const int buttonPin = 2;
// Create a Servo object
Servo myServo;
void setup() {
// Attach the Servo object to the servo pin
myServo.attach(servoPin);
// Set the button pin as an input
pinMode(buttonPin, INPUT);
// Start the servo at 0 degrees
myServo.write(0);
}
void loop() {
// Read the state of the push button
int buttonState = digitalRead(buttonPin);
// If the button is pressed (HIGH)
if (buttonState == HIGH) {
// Move the servo to 180 degrees
myServo.write(180);
} else {
// If the button is released (LOW), move the servo back to 0 degrees
myServo.write(0);
}
}
สิ่งที่คุณอาจต้องการ
-
อเมซอนServo motor on Amazonamzn.to
-
อาลีเอ็กซ์เพรสPurchase SG90 Servo motor 180 or 360 from AliExpresss.click.aliexpress.com
แหล่งข้อมูลและเอกสารอ้างอิง
ยังไม่มีทรัพยากรเลย
ไฟล์📁
ไม่มีไฟล์ที่พร้อมใช้งาน