Search Code

Lesson 87: Controlling a Servo Motor Using a Push-Button Switch

Lesson 87: Controlling a Servo Motor Using a Push-Button Switch

In this lesson, we learn how to control a servo motor using a push button switch. When a push button is pressed, the servo starts moving to the right or left until it reaches 180 and then returns to 0 degrees. The code is fully explained and demonstrated. This code is for a single sensor.

440-Lesson 87: Controlling a Servo Motor using a Push Button Switch
Language: C++
/*
 * Lesson 87: Controlling Servo Motor using a push button switch
 * Full video details: https://youtu.be/qJebLpzLE24
 * This Arduino sketch to control with a push button
 * Controlling a servo with push button with Arduino
 when a push button is pressed, the servo starts moving to the right or left until
 reaches 180 and then returns to 0 degrees.

 * Written by Ahmad Shamshiri for Robojax.com 
 * on Jan 16, 2019 at 10:06 in Ajax, Ontario, Canada

 * 
 * This code is part of Arduino Step by Step Course which starts here:  https://youtu.be/-6qSrDUA5a8
 * 
 * for library of this code visit http://robojax.com/
 * 
If you found this tutorial helpful, please support me so I can continue creating 
content like this. Make a donation using PayPal by credit card https://bit.ly/donate-robojax 
 * 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 downloaded 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/>.
 */
#include <Servo.h>

Servo myservo;  // create servo object to control a servo
int servoPin = 9;// this pin must be of those with PWM ~
int pushButtonPin =2;// the pin where push button is connected
int angle =25;    // initial angle  for servo
int angleStep =5;

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(pushButtonPin,INPUT_PULLUP);
   Serial.println("Robojax Servo Button ");
   //Full video details: https://youtu.be/qJebLpzLE24
}

void loop() {
  while(digitalRead(pushButtonPin) == LOW){
  // change the angle for next time through the loop:
  angle = angle + angleStep;

    // reverse the direction of the moving at the ends of the angle:
    if (angle <= 0 || angle >= 180) {
      angleStep = -angleStep;
    }
    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

  //Full video details: https://youtu.be/qJebLpzLE24
}

Resources & references

No resources yet.

Files📁

No files available.