Servo motors control wireless

    



             CODE 

#include <Servo.h>


Servo servo1;  // create servo object for first servo

Servo servo2;  // create servo object for second servo


#define servoPin1 2 // pin for first servo

#define servoPin2 3 // pin for second servo

#define pushButtonPin1 4 // push button pin for first servo

#define pushButtonPin2 5 // push button pin for second servo


int angle1 = 90;    // initial angle for first servo

int angle2 = 90;    // initial angle for second servo

int angleStep = 5;  // angle step


const int minAngle = 0;

const int maxAngle = 180;


int button1Pushed = 0;

int button2Pushed = 0;


void setup() {

  Serial.begin(9600);          // setup serial

  servo1.attach(servoPin1);    // attaches the first servo to the servo object

  servo2.attach(servoPin2);    // attaches the second servo to the servo object

  pinMode(pushButtonPin1, INPUT_PULLUP); // set push button pin for first servo as input

  pinMode(pushButtonPin2, INPUT_PULLUP); // set push button pin for second servo as input

  Serial.println("Tech Technology Pk");

}


void loop() {

  // Check if push button for first servo is pressed

  if (digitalRead(pushButtonPin1) == LOW) {

    button1Pushed = 1;

  }

  

  // Check if push button for second servo is pressed

  if (digitalRead(pushButtonPin2) == LOW) {

    button2Pushed = 1;

  }


  // Handle first servo movement

  if (button1Pushed) {

    // change the angle for next time through the loop:

    angle1 += angleStep;


    // reverse the direction of the moving at the ends of the angle:

    if (angle1 <= minAngle || angle1 >= maxAngle) {

      angleStep = -angleStep;

      button1Pushed = 0;

    }


    servo1.write(angle1); // move the first servo to desired angle

    Serial.print("First servo moved to: ");

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

    Serial.println(" degree");

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

  }


  // Handle second servo movement

  if (button2Pushed) {

    // change the angle for next time through the loop:

    angle2 += angleStep;


    // reverse the direction of the moving at the ends of the angle:

    if (angle2 <= minAngle || angle2 >= maxAngle) {

      angleStep = -angleStep;

      button2Pushed = 0;

    }


    servo2.write(angle2); // move the second servo to desired angle

    Serial.print("Second servo moved to: ");

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

    Serial.println(" degree");

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

  }

}


Post a Comment

Previous Post Next Post

Contact Form