INTERRUPTS?? Two timers, two relays, 10 minutes...

Got a couple of loads controlled by relays connected to arduino..

The idea...
to control two motors wireless by RF with 10 minute timers, EACH ONE SEPARATELY.

  1. Arduino receives a pulse on a digital input
  2. starts a timer of 10 minutes
  3. activates the relay.
  4. Keeps checking time till 10 minutes.
  5. When 10 minutes end... Shuts downs relay.

but if another pulse is received while counting...

  1. Shut down relay.
  2. Reset timer.

I'm kind of new to all these but is fun to learn...
Some questions:

-Wondering if I should use interrupts?? :astonished:
-Heard about timer.h because if I use Delay function I will be stuck with 10 minutes with no stop option. :stuck_out_tongue:

Thanks for any advice!!!

Don't use interrupts, but don't use delay() either.

Use the approach demonstrated in the blink without delay sketch to turn the relays on and off as required. Meanwhile your sketch is free to monitor other inputs and handle any events received on them, for example ending the current timed activity and starting another one.

Thanks for the orientation!!! Any other comments welcomed! XD

Too simple. Just use blink without delay style of program. I must type this example up every day.

When does the 2nd relay kick in?

Both relays work independently. Each one controls a motor.

When a pulse is received in a digital input it runs the motor for 10 minutes.
Two separate inputs.

However if the button is pressed while the timer is running it just stops and resets.

Yes it's simple but I'm new hahaha

Thanks for answering.

// So here is my 01 Relay 10 Minute timer… tomorrow I’ll do the code for the second one! Can’t beleive it worked! XD
Thanks!

/*

CONTROLLING A RELAY USING AN INPUT SIGNAL
WITH A 10 MINUTE TIMER (START, STOP/RESET)

Based on:

*/

// constants won’t change. Used here to
// set pin numbers:
const int pumpPin = 13; // the number of the PUMP pin
const int pulseInput = 11; // the number of the PULSE pin

// Variables will change:
int pumpState = LOW; // pumpState used to set the PUMP
long startMillis = 0; // will store last time PUMP was updated

// the follow variables is a long because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long timer = 10006010; // timer of 10 minutes

void setup() {
// set the digital pin as output:
pinMode(pumpPin, OUTPUT);
digitalWrite(pumpPin, LOW);
pinMode(pulseInput, INPUT);
}

void loop()
{

// Checking for the pulse
if (digitalRead(pulseInput) == HIGH){
if (pumpState == LOW)
pumpState = HIGH;
else
pumpState = LOW;
}

// Update, update, update…
unsigned long currentMillis = millis();

// Load startMillis
if (pumpState == HIGH & startMillis == 0)
startMillis = currentMillis;

// If PumpState HIGH and Timer less than 10 minutes
if (pumpState == HIGH & currentMillis - startMillis < timer){
// Turn ON pump
digitalWrite(pumpPin, HIGH);

}
// Else turn OFF pump, reset startMillis and pumpState
else {
digitalWrite(pumpPin, LOW);
startMillis = 0;
pumpState = LOW;

}

} // End of Loop

This is getting to be my "hobby horse", but interrupts are a nasty distraction to "newbie" programmers. Interrupts can be - and should almost always be - completely ignored and left to the ambit of certain library routines such as those for keeping time.

They are basically never needed for a "HID" interface and in particular should never be used for pushbuttons where there is a need to de-bounce. Interrupts are for things that need a fast response, meaning computer-fast, that is, microseconds.

In this case (as so often happens), you have been confused by the idea that an interrupt will "interrupt" the task that you are presently doing. In fact and to the contrary, it is a critical point of design that your main program as such should never know that an interrupt has occurred at all because the interrupt has left the operating state of the main routine absolutely unchanged. Interrupts are intended only for events that occur in "computer time"; that need to be attended to within microseconds. By the same token, the have to be dealt with and completed within fractions of a millisecond - if only because other such important events may require attention.

Thanks I'll keep that in mind... only for tasks that are ultra precise.