I am setting up the structure for a new application and trying to use the hardware interrupt, but I must be misunderstanding the use case or something else. Basically, after a welcome message the device will detect the sensor filed size and start reading the sensor continuously which it does. I want to use a button to interrupt this and go to a Menu function that may offer choices like calibrate etc.
I can do this with a While statement as shown and it works but this means it polls the button every loop.
If I hit the Interrupt button, everything stops and it just hangs.
I have read that there can be no Print or delays in the interrupt which I removed.
Right now the fMenu() function only calls another function just for testing.
Any help appreciated.
int sensorReading;
int sensorLevel;
void fMenu()
{
// Serial.println("This is the Menu Function");
//delay (200);
//Serial.println("waiting");
// while (digitalRead (5)== HIGH);
//delay (1000);
fFieldChoice();
}
//===========================================================================
void setup() {
// setup serial - diagnostics - port
Serial.begin(9600);
analogReference(INTERNAL);
pinMode(5, INPUT);
pinMode (6, INPUT);
// measure sensor
for (int i=0; i<10; i++) {
sensorReading = analogRead(A0); // Range : 0..1024
}
// Button on pin 2, Int0
attachInterrupt(0, fMenu, RISING);
//welcome message
Serial.println("Hello");
//Set up Switch Statement in fFieldChoice.
if(sensorReading < 299) {sensorLevel = 1;}
else
{sensorLevel = 2;}
Serial.println("Press FUNCTION key to start");
Serial.println("waiting");
while (digitalRead (5)== LOW);
delay (1000);
fFieldChoice();
}
//==============================================================================
void loop()
{
while (digitalRead (6)== LOW){
sensorReading = analogRead(A0); // Range : 0..1024
Serial.println(sensorReading);
delay(100);
}
fMenu();
}
//===============================================================================
void fCalibrate()
{
Serial.println("This is the Calibrate Function");
delay (200);
}
//=================================================================================
void MultiMap()
{
Serial.println("This is the MultiMap Function");
delay(200);
}
//=================================================================================
//=================================================================================
void fFieldChoice()
{
Serial.println("This is the FieldChoice Function");
delay (200);
switch (sensorLevel) {
case 1:
Serial.println("sensorlevel = 1");
Serial.println("Big field detected");
delay (2000);
break;
case 2:
Serial.println("sensorlevel = 2");
Serial.println("Small field deteted");
delay (2000);
break;
}
}
//==================================================================================