Author Topic: (arduino) move a servo with potmeter and buttons  (Read 9677 times)

0 Members and 1 Guest are viewing this topic.

Offline john33Topic starter

  • Beginner
  • *
  • Posts: 1
  • Helpful? 0
(arduino) move a servo with potmeter and buttons
« on: August 04, 2016, 10:33:26 AM »
Hello,

for a project i have something which is getting me stuck.... so far.
I want to move a servo using a potentiometer, but also i would like to move the servo from the set  position by the potmeter with 2 buttons, button A turn left, button B turn right.

In my search for this problem i came to the solution to add up the value pos to val.
But so far this is not working.
This is the test sketch so far:

#include <Servo.h>


Servo myservo;
int potpin = 0; // analog pin used to connect the potentiometer
int val; // variable to read the value from the analog pin

#define leftPin 2
#define rightPin 3
int pos = 90;
int delayPeriod = 50; // increasing this slows down the servo movement



void setup()
{
myservo.attach(9); // attaches the servo on pin 9 to the servo object
myservo.write(pos); // center the servo
pinMode(leftPin, LOW);
pinMode(rightPin, LOW);
}


void loop()
{
val = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023)
val = map(val, 0, 1023, 0, 180); // scale it to use it with the servo (value between 0 and 180)
myservo.write(val); // sets the servo position according to the scaled value
delay(delayPeriod);

if ((digitalRead(leftPin) == HIGH)&& (val+pos < 180))
{
++pos;
myservo.write(val+pos);
}
else if ((digitalRead(rightPin) == HIGH) && (val+pos > 0))
{
--pos;
myservo.write(val+pos);
delay(delayPeriod);
}
}

What am i doing wrong ?