Why aren't my timers working?

I am a newbie, I've been working on this for 3 days and can't figure out why my timers are not working. Everything comes on but does not shut off. Please tell me where I went wrong. Thanks

Garage_light_3.ino.ino (5.68 KB)

int LDR = A1;     //analog pin to which LDR is connected, here we set it to 0 so it means A0
int LDRValue = 0;      //that's a variable to store LDR values
int light_sensitivity = 650;    //This is the approx value of light surrounding your LDR

const byte lightLed = 8; //light on led BLACK
const byte lightRelay = 9; //turn on light
const int fanLed = 10;// fan on led YELLOW
const int fanRelay = 11; //turn on fan
const byte lightOverRideRelay = 12; //turn on light
const byte lightOverRideLed = 17;
const byte thermostatOverRideLed = 13;//thermostst override on ledRED
const byte thermostatOverRideRelay = 18; //short thermostat output

const int pin_lightFanRetime = 3;
const int pin_tripSensed = 4; //laser sensor input
const int pin_fanOff = 5;//button to turn off fan YELLOW
const int pin_theromOverRide = 6; //button to short thermostat output RED
const int pin_lightOverRide = 7;//if held closed light will not time out BLACK
const int pin_lightOff = 19;//button to turn off light

bool pushed_lightFanRetime = false;
bool pushed_tripSensed = false;
bool pushed_fanOff = false;
bool pushed_theromOverRide = false;
bool pushed_lightOverRide = false;
bool pushed_lightOff = false;

int flagLight_Fan = 0;
int flagThermostat = 0;
unsigned long previousMillis = 0;//used by fan and light
unsigned long previousMillis2 = 0;//used by thermostat override


void setup ()//(void)
{
  Serial.begin(9600);

  pinMode (lightLed, OUTPUT);
  pinMode (lightRelay, OUTPUT);
  pinMode (fanLed, OUTPUT);
  pinMode (fanRelay, OUTPUT);
  pinMode (lightOverRideRelay, OUTPUT);
  pinMode (lightOverRideLed, OUTPUT);
  pinMode (thermostatOverRideLed, OUTPUT);
  pinMode (thermostatOverRideRelay, OUTPUT);

  pinMode (pin_lightFanRetime, INPUT_PULLUP);
  pinMode (pin_tripSensed, INPUT_PULLUP);
  pinMode (pin_fanOff, INPUT_PULLUP);
  pinMode (pin_theromOverRide, INPUT_PULLUP);
  pinMode (pin_lightOverRide, INPUT_PULLUP);
  pinMode (pin_lightOff, INPUT_PULLUP);
}

void loop ()
{
  bool prev_lightFanRetime = pushed_lightFanRetime;
  bool prev_tripSensed = pushed_tripSensed;
  bool prev_fanOff = pushed_fanOff;
  bool prev_theromOverRide = pushed_theromOverRide;
  bool prev_lightOverRide = pushed_lightOverRide;
  bool prev_lightOff = pushed_lightOff;


  pushed_lightFanRetime = digitalRead (pin_lightFanRetime) == LOW;
  pushed_theromOverRide = digitalRead (pin_theromOverRide) == LOW;
  pushed_tripSensed = digitalRead (pin_tripSensed) == LOW;
  pushed_fanOff = digitalRead (pin_fanOff) == LOW;
  pushed_lightOverRide = digitalRead (pin_lightOverRide) == LOW;
  pushed_lightOff = digitalRead (pin_lightOff) == LOW;

  LDRValue = analogRead(LDR);
  Serial.println (LDRValue);

 
  if (prev_tripSensed != pushed_tripSensed) //Laser tripped
  {
  }
  if (prev_lightFanRetime != pushed_lightFanRetime) //Laser tripped
  {
  }
  if ((LDRValue < light_sensitivity)  && (pushed_tripSensed) && (flagLight_Fan == 0) || (pushed_lightFanRetime))
    // low light and LASER tripped or retime pushed
  {
    previousMillis = millis();//set time that light is on
    flagLight_Fan = 1; //used to stop the beam being read again

    digitalWrite(lightRelay, HIGH);//turn on the relay
    digitalWrite(lightLed, HIGH); // turn on light led
    digitalWrite(fanRelay, HIGH);//turn on the relay
    digitalWrite(fanLed, HIGH);//turn on the led
  }
  //nothing was required so remove flag
  else {
    flagLight_Fan = 0;
  }


  if (prev_theromOverRide != pushed_theromOverRide) //thermostat override pushed
  {
  }
  if ((pushed_theromOverRide) && (flagThermostat == 0))// thermostat override pushed
  {
    previousMillis2 = millis();//set time that thermostat is overridden
    flagThermostat = 1; //used to stop the beam being read again

    digitalWrite(thermostatOverRideRelay, HIGH);//turn thermostat override relay on
    digitalWrite(thermostatOverRideLed, HIGH);//turn thermostat override relay led on
  }
  //nothing was required so remove flag
  else {
    flagThermostat = 0;


    if (prev_lightOff != pushed_lightOff) //thermostat override pushed
    {
    }
    if (pushed_lightOff) {//push button to shut down fan
      digitalWrite(lightRelay, LOW);//turn relay off
      digitalWrite(lightLed, LOW);
      flagLight_Fan = 0;
      //nothing will turn it back on until timer is done
    }


    if (prev_fanOff != pushed_fanOff) //thermostat override pushed
    {
    }
    if (pushed_fanOff) {//push button to shut down fan
      digitalWrite(fanRelay, LOW);//turn relay off
      digitalWrite(fanLed, LOW);
      flagLight_Fan = 0;
      //nothing will turn it back on until timer is done
    }


 if (prev_lightOverRide != pushed_lightOverRide) //fan override pushed
  {
  }
  if (pushed_lightOverRide) //fan override pushed (detent)
  {
    digitalWrite(lightOverRideRelay, HIGH); //turn on lightOverRide relay
    digitalWrite(lightOverRideLed, HIGH); //turn on light2 led
  }  else
  {
    digitalWrite(lightOverRideRelay, LOW); //turn on light2 relay
    digitalWrite(lightOverRideLed, LOW); //turn on light2 led
    flagLight_Fan = 0;
  }



    if (flagLight_Fan == 1)
    { //fan or light is on
      if (millis() - previousMillis >= 600) {
        //check time only run this part of code if 10mins has passed
        digitalWrite(lightRelay, LOW);//turn relay off
        digitalWrite(lightLed, LOW); // turn led off
        digitalWrite(fanRelay, LOW);//turn relay off
        digitalWrite(fanLed, LOW);//turn led off
        flagLight_Fan = 0;//reset the flag
      }
    }
    if (flagThermostat == 1)
      //thermostat override on
      if (millis() - previousMillis2 >= 600) {
        digitalWrite(thermostatOverRideRelay, LOW); //turn on thermostat override
        digitalWrite(thermostatOverRideLed, LOW); //tturn on thermostat override led
        flagThermostat = 0;//reset the flag



      }
  }
}

Next time use code tags, please, so we don't have to download the file.

What part of that mess sketch isn't working? What is it supposed to do, and what does it do instead?

what do you mean "code tags"? Everything else works but timers are not shutting off anything. Nothing after line 152 works.
I don't see any changes in DrAzzy's code if there are it still does not work.

I think you are labouring under the false impression that these statements do something. In fact, they do absolutely nothing. I'm not sure what you think that they do.

if (prev_tripSensed != pushed_tripSensed) //Laser tripped
  {
  }
  if (prev_lightFanRetime != pushed_lightFanRetime) //Laser tripped
  {
  }

I don't mean to sound unkind but your code has so many things wrong with it. The main problem is that it does not seem to have any timer code in it.

Have a look at how millis() is used to manage timing in several things at a time and in planning and implementing a program.

...R

int LDR = A1; //analog pin to which LDR is connected, here we set it to 0 so it means A0

Which pin is your LDR really connected to?

Please go through your code line by line and make sure your comments match what you have actually programmed.
Very confusing.

I have attached your code with line numbers for future reference.

Garage_light_3.ino.ino.txt (7.38 KB)

LDR is connected to Analog input 1= A1

OK I appreciate the help, I'm a 65 year old disabled vet who has never attempted programming before. I have checked my labels and taken out the code that is simple io. here is what is left.

This is what I am trying to do:

  1. If low light and laser tripped and timer is reset OR light fan retime is pushed
    Turn on light, light LED, fan and fan LED for xxx mins
    once timer has expired turn them off and reset timer

  2. If thermostat override is pushed and timer is reset
    turn on thermostat override and thermostat override LED for xxx mins
    once timer has expired turn off thermostat override and thermostat override LED and reset timet

sketch_aug04b.ino (3.89 KB)

memaier:
This is what I am trying to do:

  1. If low light and laser tripped and timer is reset OR light fan retime is pushed
    Turn on light, light LED, fan and fan LED for xxx mins
    once timer has expired turn them off and reset timer

  2. If thermostat override is pushed and timer is reset
    turn on thermostat override and thermostat override LED for xxx mins
    once timer has expired turn off thermostat override and thermostat override LED and reset timet

The conditions you have listed here are quite complex. Does this pseudo code reflect what you mean in part 1

onCondition = false;
read lowLight;
read laser;
check timer;
if (lowLight == on and laser == on and timer == reset) {
   onCondition = true;
}
if (reTime = on) {
   onCondition = true;
}
if (onCondition == true && started = false) {
   startTime = millis()
  started = true;
  // start LEDs and fans
}

if (millis() - startTime >= interval) {
    // stop stuff
   started = false;
   onCondition = false;
}

...R

Quite a can of worms there, memaire. Could you answer a few questions?

Is the LDR detecting indoor or outdoor light and does a value below 650 mean its getting dark?

Is the "Laser" a photoswitch that detects an object breaking a beam? If so is it powered at all
times or only when light is low and what breaks the beam? A person walking through it, a car or ??

Is tripSensed HIGH or LOW when the beam is blocked?

What kind of relays are connected to the Arduino pins? Do they have a separate power supply for
their coils? Do the arduino pins pull HIGH or LOW to energize a relay?

What is "light fan retime"?

What is the purpose of this? "flagThermostat = 1; //used to stop the beam being read again"

Can you draw a new schematic of your project as it is now?

Hi,

I think you need to read,

Code tags.
They are made with the </> icon in the reply Menu.
See section 7 http://forum.arduino.cc/index.php/topic,148850.0.html

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

We need to see how you have configured your inputs and output.

The code tags place your sketch in its own scrolling window that makes it easy to read and copy to IDE.
Post #1 shows this.

Hope to help... Tom... :slight_smile:

if (prev_lightOverRide != pushed_lightOverRide) //fan override pushed
  {
  }

Your code has a considerable amount of stuff like that in it.

In essence it is saying:

if (something happens)
  {
  do nothing
  }

Since you are doing nothing, who cares what the thing is you are testing?

I'm presuming that is not your intention, so we are having to guess a bit as to what you actually mean.