Hello,
I'm trying something simple (in my eyes), but did not get the expected result. My sketch is much longer, but I have separated needed code, for this problem.
My equipment:
Arduino MEGA 2560
Breadboard with some LEDs and Res'
My goal:
push button shortly: start procedure in the loop (here e.g. light up LED_Start).
push button longer than 2sec: --> interrupt, (var_stopp) --> stop procedure at a specific position of code and light up LED_Interrupt
My problem:
The LED_Interrupt lights up same time (more or less) as LED_Start. It seems if interrupt was set once, program does not care I'm checking the actual state of push button (see void STOPP () function).
Why?
I have tried to work with noInterrupts() and interrupts() --> . No effect at all. (see "Debug" lines)
Thanks for help in advance!
Here the sketch:
#include <SPI.h> // Arduino SPI library
#include <Wire.h> // Arduino I2C library
#include <SD.h>
#include "TimerOne.h"
#include <FlexiTimer2.h>
volatile int StartButton = 28; // PIN28 of Arduino MEGA
int LED_Start = 38;
int LED_Interrupt = 39;
int button = LOW;
int var_stopp = 0;
unsigned long timerStart;
void setup() {
pinMode (StartButton, INPUT);
pinMode (LED_Interrupt, OUTPUT);
pinMode (LED_Start, OUTPUT);
attachInterrupt(0, STOPP, HIGH); // external interrupt 0 (PIN2 of Arduino MEGA)
} // end void setup
void loop() {
if (button == LOW){
// do something
while(button == LOW) {
button = digitalRead(StartButton);
timerStart = millis();
}
digitalWrite(LED_Start, HIGH); // LED HIGH if push button was pressed
}
// interrupts(); // Debug !
if ((millis()-timerStart) > 1200000 || var_stopp==1) { // Stopp loop after 2min or if push button was pressed for longer then 2 sec
digitalWrite(LED_Interrupt, HIGH);
button = LOW;
var_stopp = 0;
//do something
}
} // end void loop
// -------------------------
void STOPP() {
delay(1000); // after Interrupt wait 1sekc to be sure, push button will be pushed longer than for usual start procedure
if (digitalRead(StartButton) == HIGH) {
delay(1000); // again be sure push button will be pushed for longer then 2sec
if (digitalRead(StartButton) == HIGH) {
var_stopp = 1; // now set variable var_stopp to 1 (as marker)
}
}
}