Is this the code that you say you don't understand?
void loop()
{
// here is where you'd put code that needs to be running all the time.
// check to see if it's time to blink the LED; that is, if the
// difference between the current time and last time you blinked
// the LED is bigger than the interval at which you want to
// blink the LED.
unsigned long currentMillis = millis();
if(currentMillis - previousMillis > interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;
// if the LED is off turn it on and vice-versa:
if (ledState == LOW)
ledState = HIGH;
else
ledState = LOW;
// set the LED with the ledState of the variable:
digitalWrite(ledPin, ledState);
}
}
Anvay:
yes . i saw the code but couldnt understand
Save the millis() value at the time that you move the servo to the next position and increment the position variable. Then, each time through loop(), check whether the required wait period has elapsed by subtracting the start time from the millis() value now. If the period has elapsed then move the servo to the next position, update the position variable and save the start time again.
If the period has not elapsed then go round loop() again, perhaps taking other actions and/or reading inputs, but don't block the free running of loop().
Keep going until you reach the target servo position.
Anvay:
yes . i saw the [bwod] code but couldnt understand
What it does is this:
Initialise an interval; this is what in the old way was the delay value
Initialise a variable called previousMillis to 0. This will in future be the record of the last time we did the thing we're controlling. It hasn't been done at the start, so hence it's 0.
Each time through loop(), make a timestamp of the now time, currentMillis.
First time through loop(), check to see if it's time to do the thing. Do that by subtracting the time of the last time we did the thing (previuosMillis, which first time through is 0), from the time now (currentMillsi) and see if that difference is bigger than or equal to the interval.
If it IS greater than or equal, then it's time we did the thing. So do the thing, and record the time we did that thing, ie copy currentMillis into previuosMillis for next time.
If it's NOT greater than or equal, do nothing.
Each time through loop(), do exactly the same, except after we've done the thing once, previousMillis will not be 0, it will be the actual time at which we did the thing.
The ServoTimer2 library write() function takes microseconds in the range 0 to 2000 as its position paramater unlike the Servo library write function that takes degrees from 0 to 180
Hence the ServoTimer2 write() function is equivalent to the Servo library writeMicroseconds() function
UKHeliBob:
The ServoTimer2 library write() function takes microseconds in the range 0 to 2000 as its position paramater unlike the Servo library write function that takes degrees from 0 to 180
Hence the ServoTimer2 write() function is equivalent to the Servo library writeMicroseconds() function