Servo programming

hey

i have a servo connected to pin 13 in arduino and i have a button connected to pin 8 and i want to press the button and the servo rotates to one side and when reaches 0º rotates to 180 but when i unpress the button the servo stops rotating and when i press the button the servo starts rotating again in the same direction

this is the code i have so far , the problem is i can t control the speed and movement both ways

thanks

if(digitalRead(leftPin) == HIGH)

Where is the else statement telling the servo to move the other way?

    if ((pos == 0) || (pos < 0))
  {                              
     
    pos = 180;
    delay(5); // control speed to restart position
}

this is the code i have so far ,

For best forum response, post your code in a code box as explained in #7 below.

http://forum.arduino.cc/index.php/topic,148850.0.html

done

NOT1:
i want to press the button and the servo rotates to one side and when reaches 0º rotates to 180 but when i unpress the button the servo stops rotating and when i press the button the servo starts rotating again in the same direction

I can't tell how much of this is behaviour you want, and how much is a description of faulty behaviour. Can you clarify what behaviour you want, and what behaviour you currently have?

This part here is inside the IF statement and will only set the position back to 180 if your holding the button.

if ((pos == 0) || (pos < 0))
{
pos = 180;
delay(5); // control speed to restart position
}

What you need is to have a ELSE statement that tells the code what to do when you are not pressing the button or if you press then release the button.

Look into State machines.

This is the OP's original code with a bit of tidying up.

#include <Servo.h>

Servo myservo;

int leftPin = 8;
int servoPin = 13;

int pos = 90;  // servo initial position
int delayPeriod = 3;

void setup() {

  myservo.attach (13);
  pinMode(leftPin, INPUT);
  pinMode(servoPin, OUTPUT);
}  

void loop() {

  if(digitalRead(leftPin) == HIGH)  {
    if( pos > 0) {
      pos = pos - 1;
    }
    myservo.write(pos);
    delay(delayPeriod);

    if ((pos == 0) || (pos < 0)  {
     pos = 180;
     delay(5); // control speed to restart position
    }
  } 
}

And this is a substantially modified version that (I think) does what the OP wants. It seemed like an opportunity to illustrate how to compartmentalize the code into different functions - each with a single purpose - even if it may be overkill for this short project.

Note that I have changed the switch pin to INPUT_PULLUP so the other side of the switch needs to be connected to ground.

#include <Servo.h>

Servo myservo;

byte leftPin = 8;
byte servoPin = 13;

byte switchValue = LOW;
int servoStep = 1; // int because we need negative values

byte pos = 90;  // servo initial position
byte delayPeriod = 20;

void setup() {

  myservo.attach (13);
  pinMode(leftPin, INPUT_PULLUP);
  pinMode(servoPin, OUTPUT);
}  

void loop() {
  readSwitch();
  updatePosition();
  moveServo();
}

void readSwitch() {

  if(digitalRead(leftPin) == LOW) { // unpressed button will be HIGH
    switchValue = HIGH;
  }
  else {
    switchValue = LOW;
  }
}  
  
void updatePosition() {  

  if (switchValue == HIGH) {
    pos = pos + servoStep;
  }
  
  if( pos < 20) { // some servos can't go all the way from 0 to 180
                  //   adjust to suit your servo
      pos = 20;
      servoStep = 1;
  }

  if( pos > 160) {
      pos = 160;
      servoStep = -1;
  }
}

void moveServo() {
    myservo.write(pos);
    delay(delayPeriod);
}

...R

hey thanks the code that you posted does the opposite that i want in relation to button , in your code if the button is unpressed the servo does what i want , and when pressed it stops, and i wanted for the servo to move when pressed and to stop when unpressed but i think i can change the code to do what i want

after this works i want to add a second button that as the same behavior but changes the direction of the rotation of the servo

thanks :slight_smile:

#include <Servo.h>

Servo myservo;

byte leftPin = 8;
byte servoPin = 13;

byte switchValue = LOW;
int servoStep = 1; // int because we need negative values

byte pos = 90;  // servo initial position
byte delayPeriod = 20;

void setup() {

  myservo.attach (13);
  pinMode(leftPin, INPUT_PULLUP);
  pinMode(servoPin, OUTPUT);
}  

void loop() {
  readSwitch();
  updatePosition();
  moveServo();
}

void readSwitch() {

  if(digitalRead(leftPin) == HIGH) { // unpressed button will be HIGH
    switchValue = LOW;
  }
  else {
    switchValue = HIGH;
  }
}  
  
void updatePosition() {  

  if (switchValue == LOW) {
    pos = pos + servoStep;
  }
  
  if( pos < 20) { // some servos can't go all the way from 0 to 180
                  //   adjust to suit your servo
      pos = 20;
      servoStep = 1;
  }

  if( pos > 160) {
      pos = 160;
      servoStep = -1;
  }
}

void moveServo() {
    myservo.write(pos);
    delay(delayPeriod);
}

hey i changed it and it works thanks !!!

#include <Servo.h>

Servo myservo;

byte rightPin = 9;
byte leftPin = 8;
byte servoPin = 13;

byte switchValue = LOW;
int servoStep = 1; // int because we need negative values

byte pos = 90;  // servo initial position
byte delayPeriod = 20;

void setup() {

  myservo.attach (13);
  pinMode(rightPin, INPUT_PULLUP);
  pinMode(leftPin, INPUT_PULLUP);
  pinMode(servoPin, OUTPUT);
}  

void loop() {
  readSwitch();
  updatePosition();
  moveServo();
  readSwitch2();
  updatePosition2();
  moveServo2();
}

void readSwitch() {

  if(digitalRead(leftPin) == HIGH) { // unpressed button will be HIGH
    switchValue = LOW;
    
  }
  else {
    switchValue = HIGH;
  }
}  
  
void updatePosition() {  

  if (switchValue == LOW) {
    pos = pos + servoStep;
  }
  
  if( pos < 20) { // some servos can't go all the way from 0 to 180
                  //   adjust to suit your servo
      pos = 20;
      servoStep = 1;
  }

  if( pos > 160) {
      pos = 160;
      servoStep = -1;
  }
}

void moveServo() {
    myservo.write(pos);
    delay(delayPeriod);
}



void readSwitch2() {

  if(digitalRead(rightPin) == HIGH) { // unpressed button will be HIGH
    switchValue = LOW;
    
  }
  else {
    switchValue = HIGH;
  }
}  
  
void updatePosition2() {  

  if (switchValue == LOW) {
    pos = pos - servoStep;
  }
  
  if( pos < 20) { // some servos can't go all the way from 0 to 180
                  //   adjust to suit your servo
      pos = 20;
      servoStep = -1;
  }

  if( pos > 160) {
      pos = 160;
      servoStep = +1;
  }
}

void moveServo2() {
    myservo.write(pos);
    delay(delayPeriod);
}

the code with the 2 buttons

NOT1:
hey thanks the code that you posted does the opposite that i want in relation to button

I did explain that I had changed the button action, and how to wire your switch to take account of that.

...R

i did that (connect the other side of the button to ground) , did not work , maybe i did it wrong but the code works

thanks

#include <Servo.h>

Servo myservo;

byte rightPin = 9;
byte leftPin = 8;
byte servoPin = 13;

byte switchValue = LOW;
int servoStep = 5; // int because we need negative values change value to servo move faster

byte pos = 90;  // servo initial position
byte delayPeriod = 20;

void setup() {

  myservo.attach (13);
  pinMode(rightPin, INPUT_PULLUP); // other side of the button need to be connected to ground
  pinMode(leftPin, INPUT_PULLUP);  // other side of the button need to be connected to ground
  pinMode(servoPin, OUTPUT);
}  

void loop() {
  readSwitch();
  updatePosition();
  moveServo();
  readSwitch2();
  updatePosition2();
  moveServo2();
  restart();
}

void restart() {
  
  if (digitalRead(rightPin) == LOW && digitalRead(leftPin) == LOW)
  {
    pos = 90;  // restart position
  }  
}

void readSwitch() {

  if(digitalRead(leftPin) == HIGH) { // unpressed button will be HIGH
    switchValue = LOW;
    
  }
  else {
    switchValue = HIGH;
  }
}  
  
void updatePosition() {  

  if (switchValue == LOW) {
    pos = pos + servoStep;
  }
  
  if( pos < 20) { // some servos can't go all the way from 0 to 180
                  //   adjust to suit your servo
      pos = 20;
      servoStep = 5; // change value to servo move faster
  }

  if( pos > 160) {
      pos = 160;
      servoStep = -5;  // change value to servo move faster
  }
}

void moveServo() {
    myservo.write(pos);
    delay(delayPeriod);
}



void readSwitch2() {

  if(digitalRead(rightPin) == HIGH) { // unpressed button will be HIGH
    switchValue = LOW;
    
  }
  else {
    switchValue = HIGH;
  }
}  
  
void updatePosition2() {  

  if (switchValue == LOW) {
    pos = pos - servoStep;
  }
  
  if( pos < 20) { // some servos can't go all the way from 0 to 180
                  //   adjust to suit your servo
      pos = 20;
      servoStep = -5;  // change value to servo move faster
  }

  if( pos > 160) {
      pos = 160;
      servoStep = +5;  // change value to servo move faster
  }
}

void moveServo2() {
    myservo.write(pos);
    delay(delayPeriod);
}

i changed the code again and i added a void restart that when neither of the buttons is pressed the servo goes to position 90
but it does that with no delay, and if i add a delay in void restart it delays all the commands, any idea how i can fix that ?
and that if i press a button in the middle of the restart delay to stop the restart from happening?

thanks

In the Arduino world the standard answer to delay problems is not to use the delay() function and instead use the technique in the Blink Without Delay example sketch.

...R

okay thanks i am gonna try that