Lab_interaccio/2017/pendulo/pendulo.ino
2025-02-25 21:29:42 +01:00

76 lines
2.2 KiB
C++

/* Sweep
by BARRAGAN <http://barraganstudio.com>
This example code is in the public domain.
modified 8 Nov 2013
by Scott Fitzgerald
http://www.arduino.cc/en/Tutorial/Sweep
*/
#include <Servo.h>
//Define Variables we'll be connecting to
double Setpoint, Input, Output;
Servo myservo; // create servo object to control a servo
// twelve servo objects can be created on most boards
int pos = 0; // variable to store the servo position
int dif = 18;
#define MIN 90-dif
#define MAX 90+dif
void setup() {
Serial.begin(9600);
pinMode(A0, INPUT);
digitalWrite(A0, LOW);
myservo.attach(9); // attaches the servo on pin 9 to the servo object
myservo.write(90); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
for (pos = 90; pos <= MAX; 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(50); // waits 15ms for the servo to reach the position
}
}
boolean stop_bool = false;
unsigned long time_stop = 0;
void loop() {
stop_bool = false;
time_stop = millis();
while (!digitalRead(A0))
{
if ((millis()-time_stop)>=100)
{
stop_bool = true;
Serial.println("Hola");
break;
}
else stop_bool = false;
}
if (!stop_bool)
{
for (pos = MAX; pos >= MIN; pos -= 1) { // goes from 180 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(50); // waits 15ms for the servo to reach the position
}
for (pos = MIN; pos <= MAX; 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(50); // waits 15ms for the servo to reach the position
}
}
else
{
myservo.write(90); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
}