Help programing - Turn on/off based on input

I’m glad the code worked!

Reworking the code for the new changes - I broke out just the section of code that would change:

...
        timeToTurnOff=relay_on_time+60000; 
    }
    if ( val == LOW ) 
    {
      digitalWrite (relayPin, LOW);
      timeToTurnOff=0; // << change: turn off our test
    }
    if ( timeToTurnOff>=relay_on_time )  
    {
      digitalWrite (relayPin, LOW); // turn off
      // lock up until reset
      while (1)
      {
// turn on led somehow
        delay(500);
// turn off led somehow
        delay(500);
      }
    }
    loopTime = relay_on_time;
  }
}
  • I changed the 30000 to 60000

  • Since you had a different direction to go with the deadman switch, I separated it from the original LOW test.

  • For the deadman, it will stay in that loop until your reset - I’d recommend turning on the Arduino’s LED as a visual indicator you’re all locked up, but it’s whatever you wish.

Hope this solves it.

David, thanks for sticking with me :-)

Is this correct? I have the LED hookup to relayPin, and with the new "if statement", nothing happen to relayPin

....
void loop()
{
  relay_on_time = millis();
  
  if (relay_on_time >= (loopTime + 10000))
  { 
    val = digitalRead(inPin);
    
    if (val == HIGH)
    {
      digitalWrite(relayPin, HIGH);
      if ( 0 == TimeToTurnOff)
      {
        TimeToTurnOff = relay_on_time + 60000;
      }
    }
    
    if (val == LOW)
    {
      digitalWrite (relayPin, LOW);
      TimeToTurnOff = 0;
    }
    
    if (TimeToTurnOff >= relay_on_time)
    {
       digitalWrite(relayPin, LOW);
       while(1)
       {}
    }
    loopTime = relay_on_time;
  }
  
}

It looks right - but with nothing happening in the while(1) {} loop, you won't have an indication it's locked. You could always wire another led to another pin.

I was expecting the LED to come on after 10 seconds and after 60 seconds the LED go off and remain off until manual reset.

But the LED doesn’t even come on after 10 seconds.

I don't think it's the code - take a moment (and a new sketch) to turn on/off a LED exactly how you want to, and then use that code back in this main sketch. Debugging two things at once is usually more complicated than doing each separately.

I am not sure if this relevant but if changed

...
    if (shutoff <= relay_on_time)  // changed relay_on_time >= shutoff relayPin go into HIGH but never get inside infinite loop to shutoff
    {
       digitalWrite(relayPin, LOW);
       while(1)
       {}
    }
    loopTime = relay_on_time;
  } 
}

Xenia2:

    if (shutoff <= relay_on_time)  // changed relay_on_time >= shutoff relayPin go into HIGH but never get inside infinite loop to shutoff

{
       digitalWrite(relayPin, LOW);
       while(1)
       {}
    }
    loopTime = relay_on_time;
  }
}

Did you rename relay_on_time to shutoff, or is it a new variable?

I’m thinking you’ll need to post the whole code again - the changes are a bit confusing…

Also, how exactly are you testing this - what is connected, and how can you confirm something works/doesn’t? The original code seems to do what you wish, so perhaps I’m missing something in the original specs - perhaps you could list step by step what you want to do again, and we’ll make the code fit that final spec.

Sorry, they are the same variable.

Ok - well maybe we should step back a bit:

  • can you list the whole current code sketch you're using?

  • how are you testing? that is, when you say it doesn't work, how are you determining that?

  • what exactly do you want to do with the project? not the previous requirements throughout this posting, but the current/latest requirements. don't worry about using detailed step by step - I'm pretty fuzzy headed today, so slow and methodical will be fine.

By the way, if this is too much detail for a public forum, you can post me details (I think - I haven't tried it on this forum yet). On the other hand, others may weigh in with something I'm simply not seeing if you post here.

I have a water level sensor (HIGH in air, LOW in water), is connected to inPin 7 and relayPin output to pin 13 (Arduino Uno LED indicator - this is how I tell if the code working or not)

Part 1- Below is my original code, every 10 seconds it check for inPin value. if it is HIGH, set relayPin to HIGH, after 10 seconds its check for inPin value again (water should fill up and set sensor to LOW) set relayPin to LOW.

Part 2 - Now let say after 60 seconds inPin is still HIGH, that means water have not fill up (empty water in reservoir). Set relayPin to LOW and go into infinite loop until manual reset. This part apply only if inPin read HIGH after 60 seconds.

So unless part 2 executed, part 1 need to work as is.

unsigned long relay_on_time;
unsigned long loopTime;

int relayPin = 13;  // relay connected to digital pin 13
int inPin = 7;      // sensor input source connected to digital pin 7
int val = 0;        // variable to store the read value

void setup()
{
  relay_on_time = millis();
  loopTime = relay_on_time;  
  
  
  
  pinMode(relayPin, OUTPUT);        // sets the digtial pin 13 as output
  pinMode(inPin, INPUT);            // sets the digital pin 7 as input
}

void loop()
{
  relay_on_time = millis();
  
  if (relay_on_time >= (loopTime + 10000))
  { 
    val = digitalRead(inPin);
    
    if (val == HIGH)
    {
      digitalWrite(relayPin, HIGH);
    }
    
    if (val == LOW)
    {
      digitalWrite (relayPin, LOW);
    }
    loopTime = relay_on_time;
  }
}

Darn it - I swapped the test between <= and >= again!

Not only that, but the test was missing a test for zero, which is now in there.

Thanks for the complete code, I went through it and added the changes, marking them:

unsigned long relay_on_time;
unsigned long loopTime;
unsigned long shutDownTime=0;                 // <<< changed

int relayPin = 13;  // relay connected to digital pin 13
int inPin = 7;      // sensor input source connected to digital pin 7
int val = 0;        // variable to store the read value

void setup()
{
  relay_on_time = millis();
  loopTime = relay_on_time;  
  pinMode(relayPin, OUTPUT);        // sets the digtial pin 13 as output
  pinMode(inPin, INPUT);            // sets the digital pin 7 as input
}

void loop()
{
  relay_on_time = millis();
  if (relay_on_time >= (loopTime + 10000))
  { 
    val = digitalRead(inPin);
    if (val == HIGH)
    {
      digitalWrite(relayPin, HIGH);
      if ( 0 == shutDownTime)                 // <<< changed
        shutDownTime = relay_on_time + 60000; // <<< changed
    }
    if (val == LOW)
    {
      digitalWrite (relayPin, LOW);
      shutDownTime = 0;                       // <<< changed
    }
    if ( shutDownTime && shutDownTime <= relay_on_time )      // <<< changed
    {                                         // <<< changed
       digitalWrite(relayPin, LOW);           // <<< changed
       while(1)                               // <<< changed
       {                                      // <<< changed
// wait for reset                             // <<< changed
       }                                      // <<< changed
    }                                         // <<< changed
    loopTime = relay_on_time;
  }
}

The code is ridiculously simple, despite the fact my head didn’t seem to wrap around it:

  • We use shutDownTime to keep track of the time to abort. We also use a zero value to ‘turn off’ timing.

  • When we set high, we set the current time into shutDownTime, plus 60 seconds. However, if it’s already set, we skip it since that means the countdown is already running.

  • When we go low, we turn it off by setting shutDownTime to 0 - no more timing.

  • Finally, we check if the time has run out (the time to end is now less than the current time). However, we have to make sure a zero is ignored, since that’s our ‘code’ that the timer is not in use.

Sorry for the mixup - that one line error meant the timeout would never be reached. One of the joys of debugging remotely…

I am happy to say, it is now working :D. Will do more testing and add LEDs for each stage as indicator.

Thank you so much for your helps David.

Xenia2: I am happy to say, it is now working :D. Will do more testing and add LEDs for each stage as indicator. Thank you so much for your helps David.

My pleasure - sorry the path to completion was not smooth...