hi,
here’s a bit from a project I’m working on, making a servo open and close a valve.
if (Val < 300) {myservo.write (5);}
else myservo.write (95);
delay(1000);
It works, but when the servo gets to its position, it doesn’t rest (it “sings”), because the valve is pushing back.
I tried this:
if (Val < 300) {myservo.write (5); // go to this position first
delay(10);
myservo.write (10); // then go to “rest” position (the servo stops “singing”)
}
else… same thing here
delay(1000);
but it doesn’t work because after the 1000ms delay the loop restarts and the servo goes back from (10) to (5), just to go back to (10) and back to (5) and so on.
I also tried to implement more than one condition:
if (Val < 300 && myservo > 10) {myservo.write (5);
but I didn’t manage to read out the position of the servo,so…
There must be a simple answer, but I don’t have enough programming experience (or my brain is too small)to know what to do.
There must be a simple answer, but I don’t have enough programming experience (or my brain is too small)to know what to do.
Store the value of Val (as oldVal) that caused the servo to be moved. Then, on each pass through loop, don’t move the servo unless Val != oldVal. You could even use a range, so that Val needed to change by a sizable amount:
if(Val > oldVal + SIZABLEAMOUNT || Val < oldVal - SIZABLEAMOUNT)
{
// Move the servo
// Relax the servo
oldVal = Val;
}
You get, of course, to #define what SIZABLEAMOUNT means.
I'm suspecting that the oldval voids at the start of every loop or something.
It must be something stupid like that.
@ erni, there is almost no pressure involved, the "step back to rest position" solution works, it's just my programming that sucks :-)
I've figured out "sort of" a lazymans-solution;
If I set the final delay at one minute, it's not so annoying any more :)
but nevertheless I'd still be very happy not having to do it like that.
I'm suspecting that the oldval voids at the start of every loop or something.
It must be something stupid like that.
No. Once oldval is assigned a value it remembers it until assigned a new value. If you add some Serial.print() statements, to print out Val, oldval, "I got here...", etc. (and auto-format (and post) your code), you should be able to figure out what the issue is.