trying to modify my auto aiming system for fan that will turn off after 10 seconds passed, i tried this code, but servo continue running instead of rotating on 90 degrees and stop. What did I do wrong?
if (b == 3) {
previousMillis = currentMillis;
digitalWrite(6, HIGH);
delayMicroseconds(10);
digitalWrite(6, LOW);
duration = pulseIn(5, HIGH);
distance = duration * 0.034 / 2;
if (distance > 100 && currentMillis - previousMillis <= interval) {
if (q > 0 && w == 0) {
q-=5;
myservo.write(q);
delay(10);
if (q == 0) {
w = 1;
}
} else if (q < 180 && w == 1) {
q+=5;
myservo.write(q);
delay(10);
if (q == 180) {
w = 0;
}
} else if(distance >100 && currentMillis - previousMillis >= interval){
digitalWrite(3, LOW);
myservo.write(90);
}
}
}
PS: system works that way: if see obstacle, than do nothing, if doesn't see, then for 10 seconds it seeking, rotating servo right-left direction and if after 10 seconds it haven't found obstacle, then servo on 90 degrees, and fan off.
robot0889
5mpost #4
The whole code is just too big
If you need more information, then this will be enough for understanding
unsigned long previousMillis = 0;