12 Mayıs 2015 Salı

Setting servo speed with using push buttons

In this modification, servo speed is changed via 2 push buttons. One button increases the speed, other one decreases it.
Circuit is shown in the below :

Servo and two push buttons





CODES :


#include <Servo.h> 
const byte buttonPin[2]={2,3};
const byte servoPin = 9;
int servoSpeed = 100;
const byte minLimit = 0;
int servoPosition = 0;
boolean servoIsHigh = false;
// Create a servo "object", called servo1. Each servo object
// controls one servo (you can have a maximum of 12):

Servo servo1;
void setup(){
  Serial.begin(9600);
  servo1.attach(servoPin);
  servoPosition = servo1.read();
}
void loop(){
  
  if (!digitalRead(buttonPin[0])){
    servoSpeed = servoSpeed + 10;
    delay(20);
  }
  if(!digitalRead(buttonPin[1])){
    servoSpeed = servoSpeed - 10;
    if(servoSpeed < minLimit){
      servoSpeed = minLimit;
    }
  }
    Serial.print("Speed Servo");
    Serial.println(servoSpeed);
    Serial.print("Servo position");
    Serial.println(servoPosition);
    delay(20);
    servo1.write(servoPosition);
    if(servoPosition > 180){
      servoIsHigh=true;
    }
    if(servoPosition < 0){
      servoIsHigh=false;
    }
    if(servoIsHigh){
          servoPosition-=10;

    }else{
          servoPosition+=10;

    }
    delay(servoSpeed);
  
  

}

Hiç yorum yok:

Yorum Gönder