Lab_interaccio/2012/Guimaraes/Guimaraes.ino

55 lines
1.6 KiB
Arduino
Raw Normal View History

2025-02-25 21:29:42 +01:00
// Sweep
// by BARRAGAN <http://barraganstudio.com>
// This example code is in the public domain.
#include <Servo.h>
Servo myservo; // create servo object to control a servo
// a maximum of eight servo objects can be created
int pos = 0; // variable to store the servo position
void setup()
{
Serial.begin(9600);
pinMode(4, INPUT);
myservo.attach(9); // attaches the servo on pin 9 to the servo object
myservo.write(10); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
void loop()
{
//Serial.println(digitalRead(4));
if (!digitalRead(4))
{
Serial.println(digitalRead(4));
myservo.attach(9);
for(pos = 120; pos>=10; pos-=1) // goes from 180 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
}
myservo.detach();
delay(1000);
while (!digitalRead(4));
}
else
{
Serial.println(digitalRead(4));
myservo.attach(9);
delay(100);
for(pos = 10; pos < 120; 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(15); // waits 15ms for the servo to reach the position
}
myservo.detach();
delay(5000);
while (digitalRead(4));
}
}