Arduino servos (Spanish and English language)

Guys how can i action a servo with only one button.
The servo move 90° and after 1 second go back to the original state.
Srry if my english is bad, my original lenguaje is spanish.
//
Chicos como puedo accionar un servomotor con un pulsador o boton,el servomotor se mueve 90 grados y despues retrocede y vuelve al estado original.

(Could anyone send me a schematic that matches with this code)

#include <Servo.h> 

Servo myservo;

const int buttonPin = 2;
int val;
int buttonState = 0;

void setup() 
{ 
  myservo.attach(9);
  pinMode(buttonPin, INPUT);       
} 

void loop() 
{ 
  buttonState = digitalRead(buttonPin);
  if (buttonState == HIGH) {     
    val = 90;  
  } 
  else {
    val = 0;
  }
  myservo.write(val);
  delay(15);
}

Hello @nounlag - Tu codigo necesita algunos cambios para hacer el boton y hacer un temporizador. Aquí está el código con los cambios:

(I made a mistake before... here is the better sketch)

#include <Servo.h>
Servo myservo;
const int servoPin = 9;
const int buttonPin = 2;
int buttonState = 0;
int val;

unsigned long currentMillis; // "now" time
unsigned long previousMillis = 0; // "last" time
const long interval = 1000; // one second

void setup()
{
  myservo.attach(servoPin);
  pinMode(buttonPin, INPUT);
  myservo.write(0); // move servo to 0
}

void loop()
{
  buttonState = digitalRead(buttonPin); // read the button
  if (buttonState == HIGH) { // button was pushed
    myservo.write(90); // tell servo to move
    previousMillis = millis(); // set button press time
  }
  currentMillis = millis(); // now!
  if (currentMillis - previousMillis >= interval) { // difference of now and button press
    myservo.write(0); // move servo home
  }
}

And here is a simulation link. Press green "play" button to start, then red button to move servo.

Do you have a 10k pulldown resistor connected from pin 2 to GND?

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