Help , servo motor 2 button

Hi , i have a little problem , i want to press bouton to turn the motor to 0 and press the buton2 to turn the motor to 180 but idk how to i did this:

#include <Servo.h>                    
Servo monservo;                       
                                      

int bouton = 8;                       
int etatbouton = 0;                   
int bouton2 = 10;
int etatbouton2 = 0;
int position = 0;
void setup() {
   pinMode (bouton, INPUT) ;             
  pinMode (bouton2, INPUT);
  monservo.attach(9);                   
  Serial.begin(0);
}

void loop() {
  etatbouton = digitalRead (bouton);  
  etatbouton2 = digitalRead (bouton2);

  if (etatbouton==HIGH) {            
    monservo.write (position);
    position = 180;
    delay(150); 
}  
  if (etatbouton2==HIGH) {
    monservo.write (position);
    position = 0;
    delay(150);
 }
Serial.print("position du servo : "); Serial.println(position);
}

Welcome to the forum

What happens when you run your code now ?

Have you got resistors in place keeping the input pins in a known state at all times even when the buttons are not pressed ?

How are the buttons wired ? I am guessing that pressing a button takes its pin HIGH. Change that to take the pin LOW when the button is pressed and change the pinMode()s to INPUT_PULLUP instead of INPUT to turn on the built in pullup resistors so that the pins are always in a known state


okay i changed to

#include <Servo.h>                    
Servo monservo;                       
                                      

int bouton = 8;                       
int etatbouton = 0;                   
int bouton2 = 10;
int etatbouton2 = 0;
int position = 0;
void setup() {
   pinMode (bouton, INPUT_PULLUP) ;             
  pinMode (bouton2, INPUT_PULLUP);
  monservo.attach(9);                   
  Serial.begin(0);
}

void loop() {
  etatbouton = digitalRead (bouton);  
  etatbouton2 = digitalRead (bouton2);

  if (etatbouton==LOW) {            
    monservo.write (position);
    position = 180;
    delay(150); 
}  
  if (etatbouton2==LOW) {
    monservo.write (position);
    position = 0;
    delay(150);
 }
Serial.print("position du servo : "); Serial.println(position);
}

If you use INPUT_PULLUP you do not need external pulldown resistors. Also make sure that you have used 2 switch contacts that are actually switched by pressing the button. Diagonally opposite pins are a good choice

I can't make much sense of your wiring. Simply use INPUT_PULLUP and connect the switches so that the pin goes to GND when the button is pressed. You seem to have far too many wires and don't need the resistors

Is this deliberate?

Looks like you're powering the servo from the Arduino- that's never a good idea, so you need to get an additional source of power for that.

:skull: difficult

?
what deliberate

Then make it simpler and don't use the buttons for now

Use INPUT_PULLUP in pinMode()
Test for inputs being LOW in the sketch
Connect a jumper wire from GND to one of your input pins. If the servo moves then disconnect the wire from that input pin and connect it to the other one

What happens ?

sorry i have to go , i come back to test this in 30-40m maybe

Try 115200 instead of 0.

It would be easier to understand the code if you set position before using it instead of after.

  if (etatbouton==LOW) {            
    position = 0;
    monservo.write (position);
    delay(150); 
}  
  if (etatbouton2==LOW) {
    position = 180;
    monservo.write (position);
    delay(150);
 }

Setting the position variable after the move?

Edit: as per reply #12 (thanks @johnwasser)

The following diagram may help OP to connect his buttons using internal pull-up resistors. The diagram also shows how to power the Servo from separate 5V supply.
sw2Servo

pinMode(8, INPUT_PULLUP);
pinMode(10, INPUT_PULLUP);
if(digitalRead(8) == LOW)   //button is closed
{
       monservo.write(0);
}

if(digitalRead(10) == LOW)   //button2 is closed
{
       monservo.write(170);
}

why 115200?

woah a little difficult forme but i'll test that

this code ?

#include <Servo.h>                    
Servo monservo;                       
                                      

int bouton = 8;                       
int etatbouton = 0;                   
int bouton2 = 10;
int etatbouton2 = 0;
int position = 0;
void setup() {
   pinMode (bouton, INPUT_PULLUP) ;             
  pinMode (bouton2, INPUT_PULLUP);
  monservo.attach(9);                   
  Serial.begin(115200);
}

void loop() {
pinMode(8, INPUT_PULLUP);
pinMode(10, INPUT_PULLUP);
if(digitalRead(8) == LOW)   //button is closed
{
       monservo.write(0);
}

if(digitalRead(10) == LOW)   //button2 is closed
{
       monservo.write(170);
}
}

And I don't have external power :confused:
and i tried to remake ur circuit :

When you set the Serial.begin(115200), be sure to also set it in the Serial Monitor, bottom right corner.

As for your wiring, it's best to wire to opposite corners of the button - "diagonal", as this ensures you haven't connected two pins that are internally connected, which will give you a continuous connection, and won't work as a button. You may have chosen the right pins now, but you wouldn't be the first to get it wrong by connecting to the same side, either. Are both green wires which go to the buttons connected to +5? If you can't draw a schematic, and therefore insist on photographs, at least arrange the wiring so we can see, without a doubt, where each wire is going. Many posters on this forum simply walk away from threads where the poster produces pictures like this.

BTW - Could you explain why 115200 is a "little hard for you"? This is puzzling.
C

okay it works now ! the code is the same as before and the circuit too, i just changed the 115200 , i didnt know what it was.

After leaving out some lines you don't use anymore and giving names to pin numbers:

#include <Servo.h>
Servo MonServo;

const int BoutonPin = 8;
const int Bouton2Pin = 10;
const int ServoPin = 9;

void setup()
{
  pinMode (BoutonPin, INPUT_PULLUP);
  pinMode (Bouton2Pin, INPUT_PULLUP);
  MonServo.attach(ServoPin);
}

void loop()
{
  if (digitalRead(BoutonPin) == LOW)  //button is closed
  {
    MonServo.write(0);
  }

  if (digitalRead(Bouton2Pin) == LOW)  //button2 is closed
  {
    MonServo.write(170);
  }
}
1 Like

yes thanks ! last question , if i wanted to light on a green led when the servomotor is at 170 , how to do this?