搜索代码

第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()函数中的核心逻辑检查按钮是否被按下(由于上拉配置,读取到低电平信号)。如果按下“右”按钮,则angle变量减少angleStep。如果按下“左”按钮,则angle增加。代码包含边界检查,确保角度保持在0到180度范围内。然后使用myservo.write(angle)函数命令舵机移动到新位置。

项目现场演示

在视频演示中,功能清晰可见。按住“左”按钮会使舵机平滑地向左转动,角度每次增加5度,直到达到180度并停止。同样,按住“右”按钮会使舵机向右回转,角度减小直到达到0度。当前角度会打印到串口监视器上,让您实时查看舵机的精确位置。(视频中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
  }// 

  
}

文件📁

其他文件