搜尋程式碼

第88課:用兩個按鈕開關控制伺服馬達

第88課:用兩個按鈕開關控制伺服馬達

呢個項目示範咗點樣用Arduino同兩個按鈕嚟控制伺服馬達。唔使靠可變電阻或者串口指令,你可以用簡單嘅輕觸開關嚟手動將伺服馬達移向左或者右。呢個對於需要直接、實體控制機械結構嘅應用嚟講非常有用,例如整機械臂、調整相機雲台,或者整一個遙控嘅水平垂直轉動系統。按鈕可以讓你逐步調整伺服馬達嘅角度,仲可以按住按鈕令佢掃過成個活動範圍。

以下係幾個你可以用呢個設定嚟整嘅實用項目:

  • 為機械臂或者夾爪整一個手動控制面板。
  • 整一個遙控嘅相機滑軌或者水平垂直轉動雲台。
  • 為模型飛機或者船嘅舵整一個實體控制介面。
  • 整一個簡單嘅「教導器」嚟記錄CNC機床或者3D打印機嘅位置。

硬件同零件

呢個項目只需要幾個基本零件,係一個好好嘅起點嚟學習Arduino嘅輸入同輸出裝置。

  • Arduino板(Uno、Nano、Mega等)
  • 伺服馬達(例如SG90或者MG995)
  • 兩個輕觸按鈕
  • 杜邦線
  • 麵包板(可選但建議使用)

接線指南

喺影片入面,接線好簡單。伺服馬達嘅信號腳連接到Arduino上一個支援PWM嘅腳位,喺呢個例子入面係第9腳。伺服馬達嘅電源同地線分別連接到Arduino嘅5V同GND腳。兩個按鈕嘅接線方式相似。每個按鈕嘅其中一隻腳連接到地線,另一隻腳連接到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腳位。一定要用支援脈寬調變(PWM)嘅腳位,通常喺板上會用~符號標示。
  • angle:呢個設定Arduino啟動時伺服馬達嘅初始角度。預設值係90度,通常會令伺服馬達置中。
  • angleStep:呢個係每次按按鈕時伺服馬達角度改變嘅增量。數值係5嘅話,每次按壓會令伺服馬達移動5度。你可以加大呢個數值嚟加快移動,或者減細嚟做更精細嘅控制。
  • LEFTRIGHT:呢啲#define指令指定咗按鈕連接嘅數位腳位。如果你將按鈕接到其他腳位,就要改呢啲數值嚟配合你嘅接線。

loop()函數入面嘅核心邏輯會檢查按鈕係咪被撳(因為上拉配置,讀到LOW訊號就代表撳咗)。如果撳咗「右」按鈕,就會將angle變數減angleStep。如果撳咗「左」按鈕,就會將angleangleStep。程式碼包含邊界檢查,確保角度維持喺0到180度嘅範圍內。然後用myservo.write(angle)函數指令伺服馬達移動到新位置。

現場項目示範

喺影片示範入面,功能顯示得好清楚。按住「左」按鈕會令伺服馬達順暢咁向左掃,角度每次增加5度,直到達到180度就停。同樣,按住「右」按鈕會令伺服馬達向右掃返,角度遞減直到0度。當前角度會打印到序列監視器,令你可以即時睇到伺服馬達嘅確切位置。(喺影片05:20位置)

影片章節

  • [00:00] 介紹用按鈕控制伺服馬達
  • [00:57] 伺服馬達同按鈕嘅接線指南
  • [02:12] 程式碼解釋,第一部分:設定同配置
  • [03:25] 程式碼解釋,第二部分:主迴圈邏輯
  • [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
  }// 

  
}

文件📁

其他文件