Hi Folks.
So i am still working on a project where i am using servos. I already programmed a nice eased random servo movement and i am upto programming a library to publish.
but never the less now i am stuck at one issue.
first my code so far (just for explenation...):
#include <Metro.h>
int duration = 3500; // duration of the interval in miliseconds
Metro metro_interval = Metro (duration);
// ArduinoServo library
// http://arduino.cc/en/Reference/Servo
#include <Servo.h>
Servo servo1; // create servo object #1
Servo servo2; // create servo object #2
Servo servo3; // create servo object #3
// servos minimum and maximum position
#define MIN_POS 0 // the minuimum pulse width for your servos
#define MAX_POS 360 // maximum pulse width for your servos
int pos[3]; // destination
int smoothed_pos[3]; // smoothed destination
// the filter to be aplied
// this value will be multiplied by "pos" and added to "smoothed_pos"
float filter = 0.03; // 0.01 to 1.0
int counter = 0;
void setup() {
// initialize serial comunication
Serial.begin(9600);
// set pin for servo #1
servo1.attach(10);
}
void loop() {
// check the time interval, if it reaches limit "duration" goes back to one
// and runs the code
if (metro_interval.check() == 1) {
pos[0] = random(MIN_POS, MAX_POS);
// calculate a new random position between max and min values
// resets interval with a new random duration
metro_interval.interval(random(500,2000));
}
smoothed_pos[0] = smoothed_pos[0] * (1.0-filter) + pos[0] * filter;
// assign new position to the servo
servo1.write(smoothed_pos[0]);
Serial.print(" // pos 0: ");
Serial.print(pos[0]);
Serial.print(" // smoothed 0: ");
Serial.print(smoothed_pos[0]);
Serial.println(" // ");
// delay to make the servo move
delay(25);
}
So well dont wonder there are arrays because i am alreading working with 3 servos. just cleaned the code for explenation of my problem.
As u see if the metro happens, a new position is generated and the servo moves towards this position with an eased value....
now i want to do this thing: servo moves … if he reaches the end of its movement "pos = 0" again and when "smoothed_pos = 0" a new position is generated.
problem is now: since "smoothed_pos" never reaches "pos" how i am able to compare these values. i cant just say if (smoothed_pos == pos) because e.g. if pos = 190, smoothed_pos is made to be just getting close to the pos value to realize the smoothed movement... how to i realize my idea?
anyone a good hint how i can reach my goal?
best regards.