Hoi mies,
Hier dan met drukken op één knop: drukken - heen, drukken - terug, weer drukken - heen, enz.
#include <Servo.h>
Servo myservo; // create servo object to control a servo
// twelve servo objects can be created on most boards
#define servopin 9 // pin for servo
#define buttonpin 8 // pin for button
#define slowdown 15 // delay (ms) between each degree in servo turn: the higher the number, the slower the servo turns.
int pos = 0; // variable to store the servo position
void setup() {
myservo.attach(servopin); // attaches the servo on pin 9 to the servo object
pinMode (buttonpin, OUTPUT); // button pin on input
digitalWrite (buttonpin, HIGH); // activate pullup resistor on button pin
}
void loop() {
while (digitalRead (buttonpin) == HIGH ) {
// do nothing, but wait untill button pin gets low
}
for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(slowdown); // waits for the servo to reach the position
}
while (digitalRead (buttonpin) == HIGH ) {
// do nothing, but wait untill button pin gets low
}
for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(slowdown); // waits for the servo to reach the position
}
}
Ik heb er ook een definitie in gezet om snelheid waarmee de servo draait in te kunnen stellen. Hoe groter het getal, des te langzamer de servo draait:
#define slowdown 15 // delay (ms) between each degree in servo turn: the higher the number, the slower the servo turns.
groet,
Rob