Quick millis timer logic check please.

Please forgive me, but with a special needs child, I get only a couple hours of sleep at night, so my brain doesn't always work at 100%.

Simple arduino self power-down after a few seconds of the "busy" signal (digital read pin) being LOW.

Lets just assume that "pwrFet" and "busy" are properly declared and working.

If "busy" is LOW for > 5 seconds, it turns "pwrFet" to LOW.

If "busy" is HIGH, it updates "currentTime" to millis().

Code snippet:

void setup() {
  // put your setup code here, to run once:
unsigned long lastTime;
unsigned long currentTime;
lastTime=0;
}

void loop() {
  currentTime=millis();
  digitalRead(busy);
  if (busy==LOW ){
    if ((currentTime-lastTime) > 5000) {
      digitalWrite(pwrFet, LOW);
    }
  }
  if (busy==HIGH) {
    lastTime=millis();
  }
  

}

How does it look?

Thanks,

Lets just assume that "pwrFet" and "busy" are properly declared and working.

Let's not.

digitalRead(busy);
  if (busy==LOW ){

Ooops

void setup() {
  // put your setup code here, to run once:
unsigned long lastTime;
unsigned long currentTime;
lastTime=0;
}

Ooops times two..

...and that's why we ask you to post all your code.

OK, I'll be back in a few with some more of the full code.

Rat_Patrol:
OK, I’ll be back in a few with some more of the full code.

No.
All of the code.

unsigned long currentTime;
const byte pwrFet = 4;// pin connected to pwrFet
const byte busyPin = 5;// pin connected to signal

void setup() {
  // put your setup code here, to run once:
  pinMode(busyPin, INPUT_PULLUP);// Use internal pullup resistor to pull high when not busy.
  pinMode(pwrFet, OUTPUT);
  digitalWrite(pwrFet, HIGH);// not busy
}

void loop() {
  // put your main code here, to run repeatedly:
  int busy = digitalRead(busyPin);// we need a pin to read
  if (busy == LOW) {
    if (millis() - currentTime >= 5000) {
      digitalWrite(pwrFet, LOW);
    }
  }
  else currentTime = millis();// busy == HIGH
}

Perehama:

unsigned long currentTime;

const byte pwrFet = 4;// pin connected to pwrFet
const byte busyPin = 5;// pin connected to signal

void setup() {
  // put your setup code here, to run once:
  pinMode(busyPin, INPUT_PULLUP);// Use internal pullup resistor to pull high when not busy.
  pinMode(pwrFet, OUTPUT);
  digitalWrite(pwrFet, HIGH);// not busy
}

void loop() {
  // put your main code here, to run repeatedly:
  int busy = digitalRead(busyPin);// we need a pin to read
  if (busy == LOW) {
    if (millis() - currentTime >= 5000) {
      digitalWrite(pwrFet, LOW);
    }
  }
  else currentTime = millis();// busy == HIGH
}

That looks pretty much like what my full code will be.

I’ll get it all put together and post it up for review.

Thanks all.

“so my brain doesn't always work at 100%.”

Well, with what you have and what we have maybe we can get to 100% :wink:

OK, it works fine.

Thanks for the input everybody :smiley:

larryd:
“so my brain doesn't always work at 100%.”

Well, with what you have and what we have maybe we can get to 100% :wink:

Yeah, autism and insomnia are a PITA.

I actually do all this to make him custom "stuff" that helps him in various ways, in this case a custom music player tailored to his needs. I've learned a lot, but I have a long way to go.