multitasking timer

Hi friends. I have a Problem with my sketch. it's not working because I don't know how to using multi tasking.
in this arduino sketch I want if reedSwitch closed then turning on the MOSFET pin and for 5 second check the VoltageSensor pin if it's HIGH changing the MOSFET pin to LOW or if Voltage Sensor pin is LOW set the flasherSwitch to HIGH for 2seconds.

the sketch:

#include <avr/sleep.h>
#include <avr/interrupt.h>

const byte voltageSensor = 0;
const byte flasherSwitch = 1;
const byte reedSwitch = 3;
const byte MOSFET = 4;

void setup()
{
  pinMode( reedSwitch, INPUT );
  pinMode( voltageSensor, INPUT );
  pinMode( MOSFET, OUTPUT );
  pinMode( flasherSwitch, OUTPUT );
  sleep();
}

void loop()
{
  if (digitalRead(reedSwitch) == HIGH ) {
    digitalWrite(MOSFET, HIGH);
    delay(5000);
    if (digitalRead(voltageSensor) == HIGH ) {
      digitalWrite(MOSFET, LOW);
      sleep();
    }
    else
    {
      if (digitalRead(voltageSensor) == LOW ) {
        digitalWrite(flasherSwitch, HIGH);
        delay(2000);
        digitalWrite(flasherSwitch, LOW);
      }
    }
  }
}

void sleep() {
  GIMSK |= _BV(PCIE);
  PCMSK |= _BV(PCINT3);
  ADCSRA &= ~_BV(ADEN);
  set_sleep_mode(SLEEP_MODE_PWR_DOWN);
  sleep_enable();
  sei();
  sleep_cpu();
  cli();
  PCMSK &= ~_BV(PCINT3);
  sleep_disable();
  ADCSRA |= _BV(ADEN);
  sei();
}
ISR(PCINT0_vect) {
}

You don't need to use any interrupt library here. Instead you could use Arduino Pin Change interrupt.

Connect Voltage Sensor Pin to Pin # 2 of Arduino and then place your time code inside pin_ISR function.

const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPin =  13;      // the number of the LED pin

// variables will change:
volatile int buttonState = 0;         // variable for reading the pushbutton status

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);
  // Attach an interrupt to the ISR vector
  attachInterrupt(0, pin_ISR, CHANGE);
}

void loop() {
  // Nothing here!
}

void pin_ISR() {
  buttonState = digitalRead(buttonPin);
  digitalWrite(ledPin, buttonState);
}

attachinterrupt has CHANGE as a parameter, you can change it to HIGH, LOW, RISING or FALLING.

As long as you use delay(...), you will continue to have problems (including having to use difficult-to-debug interrupts). There is a tutorial at the top of this forum that will teach you to use millis(). Study it ! Learn it !

jackthom41:
You don't need to use any interrupt library here. Instead you could use Arduino Pin Change interrupt.

Connect Voltage Sensor Pin to Pin # 2 of Arduino and then place your time code inside pin_ISR function.

const int buttonPin = 2;     // the number of the pushbutton pin

const int ledPin =  13;      // the number of the LED pin

// variables will change:
volatile int buttonState = 0;        // variable for reading the pushbutton status

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);
  // Attach an interrupt to the ISR vector
  attachInterrupt(0, pin_ISR, CHANGE);
}

void loop() {
  // Nothing here!
}

void pin_ISR() {
  buttonState = digitalRead(buttonPin);
  digitalWrite(ledPin, buttonState);
}




attachinterrupt has CHANGE as a parameter, you can change it to HIGH, LOW, RISING or FALLING.

but I need sleep mode. and need the reedswitch too

The functions delay() and delayMicroseconds() block the Arduino until they complete.
Have a look at how millis() is used to manage timing without blocking in Several Things at a Time.

And see Using millis() for timing. A beginners guide if you need more explanation.

...R