How to make linear Servo

 







           CODE 

#include <Servo.h>


Servo myservo;  // create servo object to control a servo

#define servoPin 3 //~

#define pushButtonPin 2 


int angle =90;    // initial angle  for servo

int angleStep =5;

const int minAngle = 0;

const int maxAngle = 180;


int buttonPushed =0;


void setup() {

  // Servo button demo by tech Technology pk 

  Serial.begin(9600);          //  setup serial

  myservo.attach(servoPin);  // attaches the servo on pin 3 to the servo object

  pinMode(pushButtonPin,INPUT_PULLUP);

   Serial.println("Tech Technology Pk");

}


void loop() {

  if(digitalRead(pushButtonPin) == LOW){

    buttonPushed = 1;

  }

   if( buttonPushed ){

  // 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 <= minAngle || angle >= maxAngle) {

      angleStep = -angleStep;

       buttonPushed = 0;

    }

    myservo.write(angle); // move the servo to desired angle

      Serial.print("Moved to: ");

      Serial.print(angle);   // print the angle

      Serial.println(" degree");    

  delay(50); // waits for the servo to get there

   }


  

}

 

Post a Comment

Previous Post Next Post

Contact Form