Trying to turn off an output 1second after a switch is tripped


hi all.  so, have crude clock going.  i'd turn on motor @ top of hour , let run until runs on switch.  when hits switch, want wait ~1sec before turning off.  right it's not working.

what's happening after hour (5 seconds now) elapses, motor (led now) turns on turns off after 1 second though haven't hit switch.

here's got.  serial readouts commented.  any ideas it's going wrong?

code: [select]

unsigned long  time_old;
unsigned long  time_new;
const int ledpin = 13;
int hours = 0;
boolean timeout = false;
const int swpin = 2;
boolean latch = false;

void setup()
{
 pinmode(swpin, input_pullup);
 digitalwrite(swpin, high);
 time_old = millis();
 pinmode(ledpin, output);
 serial.begin(9600);
}

void loop(){
 int swstate = digitalread(swpin);
   time_new = millis();
   
    if(time_new - time_old >= (5ul * 1000)) // 1 hour has elapsed.  shortened debug  (60ul*60*1000)
    {
    hours++;
    time_old = time_new;
    serial.println(hours);  //printing out 1, 2, 3, .. 24 repeating (working)
     serial.println(swstate);  //printing out 1, 1, 1 ....
      serial.println(latch);  //printing out 0, 0, 0...
    digitalwrite(ledpin, high);  //turn on output
    }
          if(swstate == 0) //switch has been hit (pullup)
          {
          delay(50);  //debounce
          latch = true;
         }
if(latch = true)
{
delay(1000);
digitalwrite(ledpin, low);
latch = false;
}

    if(hours == 4)
    {
      //do later
    }
   
    if(hours>= 24)
    {
      time_old = time_new;
      hours = 0;
    }
 
}

wait, sorry, found it

if(latch = true)

should

if(latch == true)

now it's working


Arduino Forum > Using Arduino > Programming Questions > Trying to turn off an output 1second after a switch is tripped


arduino

Comments

Popular posts from this blog

CS5 Adobe Media Encoder: Encode failed because the source duration is nil.

cf_sql_integer vs cf_sql_bigint vs cf_sql_int??

localhost/joomla15/administrator doesnt work - Joomla! Forum - community, help and support