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.
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);
}
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.