2

I have a program i am working on where a relay is turned on -> waits a delay -> then turns off after either one of two things happen: 1) A set timer is reached OR 2) someone pushes a push button to trigger it.

Right now 90% of my code is working and i am stuck on how i go about "locking out" the push button from triggering the relay, for a select-able amount of delay time.

The timer portion of the code, with select-able times, is working. Depending on what analog 1 see's, it will trigger the relay appropriately after the selected time.

I tried to implement this same approach for the lock out timer, using analog 2 , to determine how long of a delay. I used a variable i called LOK to either read the button if it is unlocked (LOK = 0) or if it is locked (LOK =1) to wait the allotted delay time before unlocking and allowing the button to be read again.

I've tried all sorta of things and it still isn't functioning right. Maybe there is a better or simpler way to do this but I've racked my brain, and key word searching isn't helping.

Code below - please forgive random comments. I've been chopping up and moving things around i haven't edited any comments , so some may be from other snippets of codes from other projects.

int DUMP = 2; // Pin connected to big dump relay
int BUTTON = 3; // Pin connected to manual dump push button.
int BUTTONlight = 4; // Pin connected to push button light.


int BUTstate; // Current reading from BUTTON pin
int lastBUTstate = LOW; // Previous reading from BUTTON pin

long lastDebounce = 0; // Last time output was changed
long Delay = 50; // Debounce Time

unsigned long DUMPTIME;
unsigned long LOT;

unsigned long prevTIME;

int a=0;
int d=0;

int LOK = 0;

void setup()
{ 
  pinMode(A1, INPUT);
  pinMode(A2, INPUT);

  pinMode (BUTTON, INPUT);
  pinMode (BUTTONlight, OUTPUT);
  pinMode (DUMP, OUTPUT);

  digitalWrite(DUMP, HIGH);
  delay(10000);
  digitalWrite(DUMP, LOW);
  digitalWrite(BUTTONlight, HIGH);
}

int readTselector(int pin)
// returns the button number pressed, or zero for none pressed 
// int pin is the analog pin number to read 
{
  int b,c = 1;
  c=analogRead(pin); // get the analog value  
  if (c>1000)
  {
    b=0; // buttons have not been pressed
  }   
  else
    if (c<1000 && c>600)
    {
      b=1; // button 1 pressed
    }     
    else
      if (c<520 && c>400)
      {
        b=2; // button 2 pressed
      }       
      else
        if (c>100 && c<400)
        {
          b=3; // button 3 pressed
        }         
        else
          if (c<100)
          {
            b=4; // button 4 pressed
          }           
  return b; 
  }


void loop()
{
  int DUMPTIME = DTselect();
  int LOT = LOselect();

  int reading = digitalRead(BUTTON);

  unsigned long currTIME = millis();

  if ((currTIME - prevTIME) >= DUMPTIME) // Count to 2 minutes
   {
     LOK = 1;
     digitalWrite(BUTTONlight, LOW);
     digitalWrite(DUMP, HIGH);
     delay(10000);
     digitalWrite(DUMP, LOW);
     prevTIME = currTIME;
   }

  if (reading != lastBUTstate)
  {
    lastDebounce = millis();
  }


  if (LOK == 1)
  {
    delay(LOT);
    digitalWrite(BUTTONlight, HIGH);
    LOK = 0;

  }
  if ((millis() - lastDebounce) > Delay && LOK == 0)
  {
    BUTstate = reading;
  }

  if (BUTstate == HIGH)
  {
    digitalWrite(BUTTONlight, LOW);
    digitalWrite(DUMP, HIGH);
    delay(10000);
    digitalWrite(DUMP, LOW);
    prevTIME = currTIME;
  }
  lastBUTstate = reading;
}

int DTselect()
{
  a=readTselector(1); 
  if (a==0) // no buttons pressed
  {
    DUMPTIME = 210000;
  }   
  else
    if (a==1) // someone pressed a button!
    {
      DUMPTIME = 180000;
    }
    else
     if (a==2) // someone pressed a button!
     {
       DUMPTIME = 150000;
     }
     else
      if (a==3) // someone pressed a button!
      {
        DUMPTIME = 120000;
      }
     else
       if (a==4) // someone pressed a button!
       {
         DUMPTIME = 90000;
       }
  return DUMPTIME; 
 }

int LOselect()
{
  d=readTselector(2); 
  if (d==0) // no buttons pressed
  {
    delay(20000);
  }   
  else
    if (d==1) // someone pressed a button!
    {
      delay(15000);
    }
    else
     if (d==2) // someone pressed a button!
     {
       delay(10000);
     }
     else
      if (d==3) // someone pressed a button!
      {
        delay(5000);
      }
      else
       if (d==4) // someone pressed a button!
       {
         delay(0);
       }
 }

migrated from electronics.stackexchange.com Jan 13 '17 at 0:43

This question came from our site for electronics and electrical engineering professionals, students, and enthusiasts.

  • what happened to the account of the OP? – crowie Jan 13 '17 at 3:20
1

I took your code and did a bit of an edit. You had some useless variables and you where redefining DUMPTIME and LOT in a wierd way. I also define some of your constants as #define's because thats the way you should always do it.

#define DUMP 2          // Pin connected to big dump relay
#define BUTTON 3        // Pin connected to manual dump push button.
#define BUTTONlight 4   // Pin connected to push button light.
#define DELAY 50        // Debounce Time

int BUTstate;           // Current reading from BUTTON pin
int lastBUTstate = LOW; // Previous reading from BUTTON pin

long lastDebounce = 0;  // Last time output was changed

unsigned long DUMPTIME;
unsigned long LOT;
unsigned long prevTIME;

int LOK = 0;

int readTselector(int pin)
// returns the button number pressed, or zero for none pressed 
// int pin is the analog pin number to read 
{
    int c = analogRead(pin); // get the analog value  
    if (c > 1000){
        return 0; // buttons have not been pressed
    }   
    else if (c<1000 && c>600){
        return 1; // button 1 pressed
    }
    else if (c<520 && c>400){
        return 2; // button 2 pressed
    }       
    else if (c>100 && c<400){
        return 3; // button 3 pressed
    }         
    else if (c<100){
        return 4; // button 4 pressed
    } 
    else return 0; // just in case
    }

long DTselect()
{
    switch(readTselector(1)){
        case 0: return 210000; // no buttons pressed
            break;
        case 1: return 180000; // someone pressed a button!
            break;
        case 2: return 150000;
            break;
        case 3: return 120000;
            break;
        case 4: return 90000;
            break;
        default: return 210000; // default
            break;
    }
 }

int LOselect()
{
  switch( readTselector(2) ){
        case 0:  return 20000; // no buttons pressed
            break;
        case 1:  return 15000; // someone pressed a button!
            break;
        case 2:  return 10000;
            break;
        case 3:  return 5000;
            break;
        case 4:  return 0;
            break;
        default: return 20000; // default
            break;
    }
 }

void setup()
{ 
    pinMode(A1, INPUT);
    pinMode(A2, INPUT);

    pinMode (BUTTON, INPUT);
    pinMode (BUTTONlight, OUTPUT);
    pinMode (DUMP, OUTPUT);

    digitalWrite(DUMP, HIGH);
    delay(10000);
    digitalWrite(DUMP, LOW);
    digitalWrite(BUTTONlight, HIGH);
}


void loop()
{
    DUMPTIME = DTselect();
    LOT = LOselect();

    int reading = digitalRead(BUTTON);

    unsigned long currTIME = millis();

    if ((currTIME - prevTIME) >= DUMPTIME){// Count to 2 minutes
        LOK = 1;
        digitalWrite(BUTTONlight, LOW);
        digitalWrite(DUMP, HIGH);
        delay(10000);
        digitalWrite(DUMP, LOW);
        prevTIME = currTIME;
    }

    if (reading != lastBUTstate){
        lastDebounce = millis();
    }

    if (LOK){
        delay(LOT);
        digitalWrite(BUTTONlight, HIGH);
        LOK = 0;
    }

    if ((millis() - lastDebounce) > DELAY && !LOK){
        BUTstate = reading;
    }

    if (BUTstate == HIGH){
        digitalWrite(BUTTONlight, LOW);
        digitalWrite(DUMP, HIGH);
        delay(10000);
        digitalWrite(DUMP, LOW);
        prevTIME = currTIME;
    }
    lastBUTstate = reading;
}

A different version with a BUTflag variable to keep track of the button press and release. I have not dealt with the DUMPTIME variable but hopefully this will give you an idea of how to use flags so the logic doesn't fail.

#define DUMP 2          // Pin connected to big dump relay
#define BUTTON 3        // Pin connected to manual dump push button.
#define BUTTONlight 4   // Pin connected to push button light.
#define DELAY 50        // Debounce Time

int BUTstate;           // Current reading from BUTTON pin
int BUTflag = 0; 
int go = 0;
long lastDebounce = 0;  // Last time output was changed

unsigned long DUMPTIME;
unsigned long LOT;
unsigned long prevTIME;

int LOK = 0;

int readTselector(int pin)
// returns the button number pressed, or zero for none pressed 
// int pin is the analog pin number to read 
{
    int c = analogRead(pin); // get the analog value  
    if (c > 1000){
        return 0; // buttons have not been pressed
    }   
    else if (c<1000 && c>600){
        return 1; // button 1 pressed
    }
    else if (c<520 && c>400){
        return 2; // button 2 pressed
    }       
    else if (c>100 && c<400){
        return 3; // button 3 pressed
    }         
    else if (c<100){
        return 4; // button 4 pressed
    } 
    else return 0; // just in case
    }

long DTselect()
{
    switch(readTselector(1)){
        case 0: return 210000; // no buttons pressed
            break;
        case 1: return 180000; // someone pressed a button!
            break;
        case 2: return 150000;
            break;
        case 3: return 120000;
            break;
        case 4: return 90000;
            break;
        default: return 210000; // default
            break;
    }
 }

int LOselect()
{
  switch( readTselector(2) ){
        case 0:  return 20000; // no buttons pressed
            break;
        case 1:  return 15000; // someone pressed a button!
            break;
        case 2:  return 10000;
            break;
        case 3:  return 5000;
            break;
        case 4:  return 0;
            break;
        default: return 20000; // default
            break;
    }
 }

void setup()
{ 
    pinMode(A1, INPUT);
    pinMode(A2, INPUT);

    pinMode (BUTTON, INPUT);
    pinMode (BUTTONlight, OUTPUT);
    pinMode (DUMP, OUTPUT);

    digitalWrite(DUMP, HIGH);
    delay(10000);
    digitalWrite(DUMP, LOW);
    digitalWrite(BUTTONlight, HIGH);
}


void loop()
{
    DUMPTIME = DTselect();
    LOT = LOselect();

    BUTstate = digitalRead(BUTTON);

    unsigned long currTIME = millis();
/*
    if ((currTIME - prevTIME) >= DUMPTIME && !LOK){
        LOK = 1;
        digitalWrite(BUTTONlight, LOW);
        digitalWrite(DUMP, HIGH);
        delay(10000);
        digitalWrite(DUMP, LOW);
        prevTIME = currTIME;
    }
    else */
    if (LOK){
        delay(LOT);
        digitalWrite(BUTTONlight, HIGH);
        LOK = 0;
    }
    else if(!LOK){
        if(!go && BUTstate && !BUTflag){
            lastDebounce = millis();
            BUTflag = 1;
        }
        else if (!go && BUTstate && BUTflag==1 && (millis() - lastDebounce) > DELAY){
            BUTflag = 2;
        }
        else if (!go && !BUTstate && BUTflag==2){
            BUTflag = 0;
            go = 1;
        }
        else if (go){
            digitalWrite(BUTTONlight, LOW);
            digitalWrite(DUMP, HIGH);
            delay(10000);
            digitalWrite(DUMP, LOW);
            prevTIME = currTIME;
            go = 0;
            LOK = 1;
        }
    }
}
  • Thanks, but it still isn't functioning right when any other case but 0 on A2 is selected. It seems the push button locks out completely from being triggered at all. – Adam Alexander Jan 13 '17 at 5:25
  • What do you mean by A2? I didnt change any of the logic though. Just tidied up the functions and varibles. The code should look a little more compact now. – crowie Jan 13 '17 at 9:54
  • Ah ok, and what i meant by A2 was analog 2 input. Now what's happening is if any other value than 0 is read from A2 it seems to cycle through the process twice after each button press. So with LOT being 5000 or 5 seconds, it cycles, waits 5 secs, and instead of resetting the LED and waiting for a new button press, it cycles a second time, waits 5 secs, then resets the LED. Also it will sometimes keep getting stuck in the cycle and resetting. – Adam Alexander Jan 13 '17 at 15:47
  • @AdamAlexander I have added a different version – crowie Jan 13 '17 at 18:04
  • I can't thank you enough! I need to logically think the flag through a bit more to grasp the concept, but the flags work great! I was able to just editted the DUMPTIME logic a bit to use flags and it is now also working in conjuction with the LOT delay time! Kudos! – Adam Alexander Jan 13 '17 at 23:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.