Auto-aiming system whith timer

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.

what do you think currentMillis - previousMillis will be when you do this?

otherwise Don't post snippets (Snippets R Us!)

Целый код просто большой
но если нужно больше информации, то вот необходимая часть
unsigned long previousMillis = 0;

const long interval = 10000;

?? use English in the general categories in the forum. There are also specific categories for other languages

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;

const long interval = 10000;

OK I guess my first answer is enough to explain then.

for the rest, as we say, the crystal ball is still in the dishwasher

1 Like

Your topic has been moved; you have been asked before NOT to post in "Uncategorized".

What is the task of the sketch in real life?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.