ค้นหาโค้ด

บทเรียนที่ 88: การควบคุมมอเตอร์เซอร์โวโดยใช้สวิตช์ปุ่มกดสองตัว

บทเรียนที่ 88: การควบคุมมอเตอร์เซอร์โวโดยใช้สวิตช์ปุ่มกดสองตัว

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

นี่คือโปรเจกต์เชิงปฏิบัติบางส่วนที่คุณสามารถสร้างได้ด้วยการตั้งค่านี้:

  • สร้างแผงควบคุมด้วยมือสำหรับแขนหุ่นยนต์หรือกริปเปอร์
  • สร้างสไลเดอร์กล้องหรือหัวแพน-ทีลต์ที่ควบคุมระยะไกล
  • พัฒนาอินเทอร์เฟซควบคุมทางกายภาพสำหรับหางเสือเครื่องบินจำลองหรือเรือ
  • สร้าง "เพนแดนท์สอน" ง่ายๆ เพื่อบันทึกตำแหน่งสำหรับเครื่อง CNC หรือเครื่องพิมพ์ 3 มิติ

ฮาร์ดแวร์และส่วนประกอบ

โปรเจกต์นี้ต้องใช้ส่วนประกอบพื้นฐานเพียงไม่กี่ชิ้น ทำให้เป็นจุดเริ่มต้นที่ดีเยี่ยมสำหรับการเรียนรู้เกี่ยวกับทั้งอุปกรณ์อินพุตและเอาต์พุตกับ Arduino

  • บอร์ด Arduino (Uno, Nano, Mega ฯลฯ)
  • มอเตอร์เซอร์โว (เช่น SG90 หรือ MG995)
  • ปุ่มกดชั่วขณะสองปุ่ม
  • สายจัมเปอร์
  • เบรดบอร์ด (ไม่บังคับแต่แนะนำ)

คู่มือการเดินสายไฟ

ในวิดีโอ การเดินสายไฟทำได้ตรงไปตรงมา ขาสัญญาณของมอเตอร์เซอร์โวเชื่อมต่อกับพินที่รองรับ PWM บน Arduino ซึ่งในตัวอย่างนี้คือพิน 9 สายไฟและกราวด์ของเซอร์โวเชื่อมต่อกับพิน 5V และ GND ของ Arduino ปุ่มกดสองปุ่มเดินสายในลักษณะคล้ายกัน ปุ่มแต่ละปุ่มมีขาข้างหนึ่งเชื่อมต่อกับกราวด์ และอีกข้างเชื่อมต่อกับพินอินพุตดิจิทัลบน Arduino (พิน 2 สำหรับปุ่ม "ซ้าย" และพิน 3 สำหรับปุ่ม "ขวา") โค้ดใช้ตัวต้านทานดึงขึ้นภายใน ดังนั้นจึงไม่จำเป็นต้องใช้ตัวต้านทานภายนอกสำหรับปุ่มต่างๆ (ในวิดีโอที่ 00:57)

Arduino wriing for Sevo motor with 2 push buttons
Arduino wriing for Sevo motor with 2 push buttons

คำอธิบายโค้ด

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

#include <Servo.h>

Servo myservo;  // สร้างออบเจกต์เซอร์โวเพื่อควบคุมเซอร์โว
int servoPin = 9;// พินนี้ต้องเป็นพินที่มี ~ (PWM)
int angle =90;    // มุมเริ่มต้นสำหรับเซอร์โว
int angleStep =5;

#define LEFT 2   // พิน 2 เชื่อมต่อกับปุ่มซ้าย
#define RIGHT  3  // พิน 3 เชื่อมต่อกับปุ่มขวา
  • servoPin: ตัวแปรนี้กำหนดพิน Arduino ที่เชื่อมต่อกับสายสัญญาณของเซอร์โว จำเป็นต้องใช้พินที่รองรับ Pulse Width Modulation (PWM) ซึ่งโดยทั่วไปจะมีสัญลักษณ์ ~ บนบอร์ด
  • angle: ค่านี้กำหนดมุมเริ่มต้นของเซอร์โวเมื่อ Arduino เริ่มทำงาน ค่าเริ่มต้นคือ 90 องศา ซึ่งโดยทั่วไปจะทำให้เซอร์โวอยู่ตรงกลาง
  • angleStep: ค่านี้คือจำนวนที่มุมเซอร์โวเปลี่ยนแปลงในแต่ละครั้งที่กดปุ่ม ค่า 5 หมายความว่าแต่ละครั้งที่กดจะเลื่อนเซอร์โว 5 องศา คุณสามารถเพิ่มค่านี้เพื่อการเคลื่อนที่ที่เร็วขึ้นหรือลดลงเพื่อการควบคุมที่ละเอียดขึ้น
  • LEFT และ RIGHT: คำสั่ง #define เหล่านี้กำหนดพินดิจิทัลที่เชื่อมต่อกับปุ่มกด หากคุณเดินสายปุ่มเข้ากับพินอื่น คุณต้องเปลี่ยนค่าเหล่านี้ให้ตรงกับการตั้งค่าของคุณ

ตรรกะหลักในฟังก์ชัน loop() จะตรวจสอบว่ามีการกดปุ่มหรือไม่ (โดยการอ่านสัญญาณ LOW เนื่องจากการกำหนดค่าดึงขึ้น) หากกดปุ่ม "ขวา" จะลดค่าตัวแปร angle ลงตาม angleStep หากกดปุ่ม "ซ้าย" จะเพิ่มค่า angle โค้ดมีการตรวจสอบขอบเขตเพื่อให้แน่ใจว่ามุมอยู่ในช่วง 0 ถึง 180 องศา จากนั้นใช้ฟังก์ชัน myservo.write(angle) เพื่อสั่งให้เซอร์โวเคลื่อนที่ไปยังตำแหน่งใหม่

การสาธิตโปรเจกต์สด

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

บทในวิดีโอ

  • [00:00] แนะนำการควบคุมเซอร์โวด้วยปุ่มกด
  • [00:57] คู่มือการเดินสายไฟสำหรับเซอร์โวและปุ่มกด
  • [02:12] คำอธิบายโค้ด ตอนที่ 1: การตั้งค่าและการกำหนดค่า
  • [03:25] คำอธิบายโค้ด ตอนที่ 2: ตรรกะลูปหลัก
  • [05:20] การสาธิตโปรเจกต์สด
  • [06:23] บทสรุปและการประยุกต์ใช้งานที่เป็นไปได้

ภาพ

2 pin tactile push button switch
2 pin tactile push button switch
SG90 Mini Servo Motor
SG90 Mini Servo Motor
SG90_servo_motor-1
SG90_servo_motor-1
SG90_servo_motor-0
SG90_servo_motor-0
Arduino wriing for Sevo motor with 2 push buttons
Arduino wriing for Sevo motor with 2 push buttons
891-Lesson 88: Servo control with 2 push buttons
ภาษา: C++
/*
 * S05-05 Servo control with 2 push buttons
 * Download and resource page https://robojax.com/RJT764
 Watch video on YouTube https://www.youtube.com/watch?v=J_kbyAY1rLM
 * 
 * Written by Ahmad Shamshiri for Robojax.com 
 * on Jan 15, 2019 at 19:15 in Ajax, Ontario, Canada

  
 * This code is "AS IS" without warranty or liability. Free to be used as long as you keep this note intact.* 
 * This code has been download from Robojax.com
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */
/*
 Controlling a servo with two Push buttons with Arduino
 when Left  push button is pressed, the servo start moving to the left until it reaches 180( or zero) degrees
 when Right  push button is pressed, the servo start moving to the right until it reaches 180( or zero) degrees
At any instance if the button is released, servo stops

*/

#include <Servo.h>

Servo myservo;  // create servo object to control a servo
int servoPin = 9;// this pin must be of those with PWM ~
int angle =90;    // initial angle  for servo
int angleStep =5;

#define LEFT 2   // pin 2 is connected to left button
#define RIGHT  3  // pin 3 is connected to right button

void setup() {
  // Servo button demo by Robojax.com
  Serial.begin(9600);          //  setup serial
  myservo.attach(servoPin);  // attaches the servo on pin 9 to the servo object
  pinMode(LEFT,INPUT_PULLUP); // assign pin 12 ass input for Left button
  pinMode(RIGHT,INPUT_PULLUP);// assing pin 2 as input for right button
  myservo.write(angle);// send servo to the middle at 90 degrees
 Serial.println("Robojax Servo 2 Buttons ");
}

void loop() {
  // Servo button demo by Robojax.com
  while(digitalRead(RIGHT) == LOW){

    if (angle > 0 && angle <= 180) {
      angle = angle - angleStep;
       if(angle < 0){
        angle = 0;
       }else{
      myservo.write(angle); // move the servo to desired angle
      Serial.print("Moved to: ");
      Serial.print(angle);   // print the angle
      Serial.println(" degree");
       }
    }
    
  delay(100); // waits for the servo to get there
  }// while
 // Servo button demo by Robojax.com

  while(digitalRead(LEFT) == LOW){

    // Servo button demo by Robojax.com
    if (angle >= 0 && angle <= 180) {
      angle = angle + angleStep;
      if(angle >180){
        angle =180;
       }else{
      myservo.write(angle); // move the servo to desired angle
      Serial.print("Moved to: ");
      Serial.print(angle);   // print the angle
      Serial.println(" degree");
       }
    }
    
  delay(100); // waits for the servo to get there
  }// 

  
}

สิ่งที่คุณอาจต้องการ

แหล่งข้อมูลและเอกสารอ้างอิง

ไฟล์📁

ไฟล์อื่น ๆ