検索コード

レッスン88:2つの押しボタンスイッチを使用してサーボモーターを制御する

レッスン88:2つの押しボタンスイッチを使用してサーボモーターを制御する

このプロジェクトでは、Arduinoと2つの押しボタンを使ってサーボモーターを制御する方法を紹介します。ポテンショメータやシリアルコマンドに頼る代わりに、シンプルなタクタイルスイッチを使ってサーボを手動で左右に動かすことができます。これは、ロボットアームの構築、カメラジンバルの調整、リモコン式パン・チルトシステムの作成など、メカニズムを直接物理的に制御する必要があるアプリケーションで非常に役立ちます。ボタンを使用するとサーボの角度を段階的に調整でき、ボタンを押し続けると全可動域をスイープさせることができます。

このセットアップで構築できる実用的なプロジェクトをいくつか紹介します。

  • ロボットアームやグリッパー用の手動コントロールパネルの構築。
  • リモコン式カメラスライダーやパン・チルトヘッドの作成。
  • 模型飛行機やボートのラダー用の物理的制御インターフェースの開発。
  • CNCマシンや3Dプリンターの位置を記録するためのシンプルな「教示ペンダント」の製作。

ハードウェアと部品

このプロジェクトに必要な部品はわずかで、Arduinoでの入力デバイスと出力デバイスの両方について学ぶのに最適な出発点です。

  • Arduinoボード(Uno、Nano、Megaなど)
  • サーボモーター(例:SG90またはMG995)
  • モーメンタリ押しボタン×2
  • ジャンパーワイヤー
  • ブレッドボード(任意ですが推奨)

配線ガイド

ビデオでは、配線は簡単です。サーボモーターの信号ピンはArduinoのPWM対応ピンに接続されており、この例ではピン9です。サーボの電源とグランドはArduinoの5VピンとGNDピンに接続されています。2つの押しボタンは互いに同様の方法で配線されています。各ボタンの一方の端子はグランドに接続され、もう一方の端子は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度動きます。より速い動きが必要な場合はこの値を増やし、より細かい制御が必要な場合は減らすことができます。
  • LEFTおよびRIGHT: これらの#defineディレクティブは、押しボタンが接続されているデジタルピンを割り当てます。ボタンを異なるピンに配線する場合は、セットアップに合わせてこれらの値を変更する必要があります。

loop()関数のコアロジックは、ボタンが押されているかどうかをチェックします(プルアップ構成によりLOW信号を読み取ります)。「右」ボタンが押されると、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
  }// 

  
}

ファイル📁

他のファイル