#include <Servo.h>
int buttonpin = 7;
int last_state = HIGH;
int relaypin = 8;
Servo myservo; //create servo object to control a servo
//twelve servo objects can be created on most boards
int pos = 0;
void setup()
{
// put your setup code here, to run once:
Serial.begin(115200);
pinMode(buttonpin, INPUT_PULLUP);
pinMode(relaypin, OUTPUT);
myservo.attach(9); //attaches the servo on pin 9 to the servo
}
void loop()
{
int value = digitalRead(buttonpin);
if (last_state != value)
{
last_state=value;
if (value == HIGH)
{
digitalWrite(relaypin,LOW);
Serial.println("released");
for (pos = 0; pos <= 100; pos += 1)
{
// goes from 0 degrees to 100 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
for (pos = 100; pos >= 80; pos -= 1)
{
// goes from 100 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
}
else
{
digitalWrite(relaypin,HIGH);
Serial.println("pressed");
}
}
}