Servo with 2 buttons

Good morning everyone, I've been struggling with a code i want to write.
Its about making 2 servo's turn with one button for as long as i press the same button again.
The other scenario is that button 2 has to place the servo a few steps to a direction with a repeatedly distance.
Also when power cuts, the servo has to start again from his last position, so not a begin position where it turns to every time.. For me its not necessary to know on what degree it is because it just has to sent a few pulses.

Can't wait to read the suggestions.
Kind regards,
Senna Nieuwland

Welcome to teh forum

Start by posting your best effort at writing the sketch, using code tags when you do

You can set the start position of a servo by writing the required value to it before attaching it

me too. what have you tried?

Hi,
A circuit diagram would be helpful too.
Welcome to the forum.

Please read the post at the start of any forum , entitled "How to use this Forum".

This will help with advice on how to present your code and problems.
Thanks.. Tom.. :grinning: :+1: :coffee: :australia:

Tinker, 2 buttons and servo

This is the link to my tinker.

I did write some codes but not worthy for this :see_no_evil:
I'm a beginner programmer and really want to understand how it all works. I do have a code in my tinker.

#include <Servo.h>

Servo servo_L;
Servo servo_R;

int buttonState1 = 0;
int buttonState2 = 0;
int pos;

int var1=0;
int var2=0;


void setup(){
  
  Serial.begin(9600);
  servo_L.attach(5);  //Left servo Pin3
  servo_R.attach(6);  //Right servo Pin3
  pinMode(2, INPUT);  //Button 1
  pinMode(4, INPUT);  //Button 2
  servo_L.write(pos); //left servo position storage
  servo_R.write(pos);  //Right servo position storage

}





void loop(){
  
 buttonState1 = digitalRead(2);
  buttonState2 = digitalRead(4);
  
  if (buttonState1 == HIGH) {
     for (pos = 0; pos <= 360; pos += 1) {                 // Sweep the servo from 0 to 360 degrees in steps
        servo_L.write(pos);                                 // tell servo's to go to position in variable 'pos'
        servo_R.write(pos);
        Serial.println("Button 1, turning");
      delay(100); 
     }
  }
    else if(buttonState1 == LOW){
      Serial.println("knop uit");
      var1 = 0;
   }
  
  if (buttonState2 == HIGH){
       Serial.println("Button 2 steps");
        var2=1;
    }
  }

Do you have physical UNO Board to run your sketch of Post-6?2.

Your obejectives (what you want the Servos should do in response to Buttons' action) should be stated more clearly like:
1. Intially, both Sevos are at postion 0 degree.
2. Press Button-1, the Servo_L keeps moving.
3. Press Button-2, the Servo_R keeps moving.
4. Press both Button-1 and Button-2 simultanesouly, ther Servos.....

Thanks for replying, with button 1 both servo's should turn at the same speed. With button 2 the servo's need to turn for a specific time.
Note: the servo's are continuous.
I'm using a physical Atmega.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.