Go Down

Topic: AttachInterrupt? issues (Read 329 times) previous topic - next topic

Lsnyman

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.
Code: [Select]
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;
   }
}
//==================================================================================

johnwasser


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.

But it calls a function that does both Serial.print() AND delay().

Interrupts are not a magical way to make your code responsive.  You can't do much more than set a flag or increment a value.  The way to make your code responsive is the get rid of delay() calls and loops that take a long time.
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Lsnyman

Thank you John.
Thats what I suspected. So to confirm, the only way to return to a Menu type function that will need to display options and accept inputs will be like what I have done in the main loop() ?
minus the delays of course, those are mainly just for testing the structure.

Nick Gammon


I want to use a button to interrupt this and go to a Menu function that may offer choices like calibrate etc.


Interrupts aren't for that though. They are for briefly dealing with something like an incoming byte from a serial port.

This sort of "interrupt" is more correctly just a different logic path. Your main loop can do something like a state machine, where you are in the state of doing Menu X, or Menu Y.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

UKHeliBob

Quote
I can do this with a While statement as shown and it works but this means it polls the button every loop.

There is nothing wrong with that as long as the code in loop() is non blocking.  I think that you are taking the word interrupt too literally.
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Go Up