Lab_interaccio/2017/pendulo_pid/pendulo_pid.ino

42 lines
1.2 KiB
Arduino
Raw Permalink Normal View History

2025-02-25 21:29:42 +01:00
#include <Servo.h>
Servo myservo;
float pos;
int angleInit;
int angleMax;
int angleMin;
int dif = 60;
void setup()
{
Serial.begin(9600);
myservo.attach(9); // attaches the servo on pin 9 to the servo object
angleInit = 90;
angleMax = angleInit+dif;
angleMin = angleInit-dif;
myservo.write(angleInit);
}
int angle = angleInit;
float inc = 1;
void loop() {
inc = 1;
for (pos = angleMin; pos <= angleMax -1; pos += inc) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
inc = 4*(angleMax- pos)/(angleMax- angleMin);
// Serial.print("UP: ");
// Serial.println(pos);
myservo.write((int)pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
inc = 1;
for (pos = angleMax; pos >= angleMin + 1; pos -= inc) { // goes from 180 degrees to 0 degrees
inc = 4*(pos - angleMin)/(angleMax- angleMin);
// Serial.print("DOWN: ");
// Serial.println(pos);
myservo.write((int)pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
}