move a servo beyond the "val" value from analogRead

Dear all,

I need some help please,

I can control a servo using the knob code, the servo follows the position of the potmeter. but i need to move the servo forward or backward independent of the potmeter.

in other words, the potmeter is turned to position "x" (say 60 degrees). if i push button A the servo moves towards 0 degrees, if I push button B the servo moves to 180 degrees. If the button is released the servo must stay in that position. If the potmeter is adjusted to an other position the servo must move from the last known position set by the potmeter or the buttons.

How do i turn this question into code.

off course the knob code, but then ?

OK, so the knob code demonstrates that your servo is wired correctly and it's working. Get the buttons wired up and test them - use one of the other example sketches.

Can you see from the knob code that the servo always goes where you tell it? That indicates to me that you need to keep the servo position as a separate variable and allow A and B to increment or decrement that variable. Initially, don't worry about the knob at all.

So then you want to combine the knob and the buttons. Question: what happens when you turn the knob all the way to the right but you've used the buttons to go left. How do you go further right using the knob? One solution is to use an encoder type knob. Sparkfun has a good one. That can be turned indefinitely and it works in simple-to-program clicks.

In my sketch i have used min and max endpoints for the servo.

I also have the servo 2 buttons sketch, if i push one of the buttons the servo moves to that max position, even if i keep pressing the button.

if the potmeter (knob) is al the way to the left it must be possible to use the button(s) to move the servo all the way to the right. The max. working angles are 45 - 135 degrees. 90 degrees is the centre position.

In the functionality of the machine the servo will always return to the 90 degrees position. controlled by the 2 buttons.

  1. Show your code. Use [ code ] tags (the </> button on the full editor.)
  2. Tell us what you want it to do.
  3. Tell us what it actually does. Post any compiler error messages in [ code ] tags too.

Here is the code:

#include <VarSpeedServo.h>


VarSpeedServo stuur_truck;                           //servo om truck te laten sturen
VarSpeedServo stuur_trailer;                         //servo om trailer wielen te laten sturen


int potpin1 = 0;                             //leest waarde van de potmeter aangesloten op analog pin 0, stand van truck tov trailer
int val1;                                    //variabele om de waarde van de analoge pin te meten.

#define leftPin 22                            //stuur trailer: signaal op pin 22 voor links
#define rightPin 23                           //stuur trailer: signaal op pin 23 voor rechts
int pos1 = val1;                              //waarde van val1 is uitgangspunt om servo handmatig te bedienen
int delayPeriod1 = 30;                       // verhogen van deze waarde laat de servo langzamer bewegen




void setup()
{
  stuur_truck.attach(3);                      //plaats de servo op pin 3 deze servo laat de truck rond de kingpin draaien
  stuur_trailer.attach(4);                    //plaats de servo op pin 4 deze servo stuurt de trailer wielen


}

void loop()
{

  // programma om servos te laten bewegen:
  val1 = analogRead(potpin1);                 // leest de waarde van de potmeter (waarde tussen 0 en 1023)
  val1 = map(val1, 0, 1023, 0, 180);          // zet om in waarde voor servo (verstelweg tussen 45 en 120 graden)
  stuur_truck.slowmove(val1, 40);             // plaatst de servo naar de positie naarde de geschaalde waarde, at speed 40
  stuur_trailer.slowmove(val1, 40);             //laat de servo de trailer wielen sturen



  if (digitalRead(leftPin) == HIGH) {         // in stapjes van 1 graad bewegen
    if ( pos1 > 45) --pos1;                        // zolang de pin HOOG is laat de servo bewegen
    stuur_trailer.write(pos1);                   // laat de servo naar de variabele 'pos' gaan
    delay(delayPeriod1);                         // kijk naar de "delay" waarde = snelheid van de servo
  }
  if (digitalRead (rightPin) == HIGH) {        // in stapjes van 1 graad bewegen
    if ( pos1 < 135) ++pos1;                      // zolang de pin HOOG is laat de servo bewegen
    stuur_trailer.write(pos1);                   // laat de servo naar de variabele 'pos' gaan
    delay(delayPeriod1);                         // kijk naar de "delay" waarde = snelheid van de servo
  
  }
}

What it does is:
potmeter puts the servo in position “X” (can be any position whitin the range of the servo).
When i push one of the buttons the servo moves quicly to one side, when i release the button the servo goes back to its previous position set by the potmeter.

What i want it to do is:
set servo in position “X”
when i press button leftPin i want the servo to move to the left and stay in position when i releaese the button.
When i press button rightPin i want the servo to move to the right and stay in position when i release the button.

So i am looking for a way to alter the pulselength with to buttons, the value “val1” is the starting point.

So far this is beyond my capabilities…

I think this can simply be done by having your buttons change the potentiometer value before it hits the servo. I'm very new to programming so this could be a total fail, so forgive me if it is.

But down when you actually do your write, you can feed it a value that isn't just the pot, but pot + button Then make pot = pot + button so it remembers that position.

One button adds to the pot value before the servo sees it, other button subtracts. If you have the pot fully one way, your buttons can still adjust, but your pot will either be limited or in a dead zone until it comes back within useful range.

in my search to fix my problem i have found a solution, but implementing in the code is giving problems.

Somebody told me that i should add up or down the val+pos.

So the starting point is val, and if one of the buttons is pushed the value is added up or down to the value VAL.

But how to do this ?

the code has some faults…

#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
  delayPeriod();

if (digitalRead(leftPin) == HIGH)
{
  && (val+pos < 180)

   ++pos;
   myservo.write(val+pos);
if (digitalRead(rightPin HIGH)
{
  && (val+pos > 0)

   --pos;
   myservo.write(val+pos);
   delayPeriod();     
 }
}

the && is giving problems…
Help anyone ?

the && is giving problems...

Statements do not start with &&. The && operator is within the () of the if statement.

If that if() statement is merged back together [how did you get the { and newline in the middle of the logic?] it still has some problems. It checks if the proposed output value is going to go out of range when a button is pushed but what if a movement of the pot also pushes it out of range?

Take a step back from the keyboard and try to write down (on paper) the actions that the system should take under all possible input conditions and error conditions. What should happen when a button is pushed and the pot is moving and it hits an end stop? What if the movement of the pot and the button is opposite?

You may also need to look at https://www.arduino.cc/en/Tutorial/StateChangeDetection to identify the difference between a button-click event and the-button-is-currently-held-down.

Hi, I presume you are working with this project..

Tom... :)

Moderator edit: URL tag corrected

That is correct, but still the problem is there.
I used this code to move the servo with buttons with the val value, but it is not ok.

#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);
}
}

but all it does is to jitter the servo and not move at all, the other button does nothing…
I am new to coding so help is asked.

pinMode(leftPin, LOW);

That's not the correct parameter for pinMode.

You drive the servo to val then you wait for 50ms and drive it to val+pos but only if a button is actually held down at that instant. Which one did you actually want to send it to?

If the right button is pressed, then you add an extra 50ms delay? Why?