1. We will be in Read Only mode (no new threads, replies, registration) for several hours as we migrate the forums to upgraded software.

using interrupt to switch between modes in arduino

Discussion in 'The Projects Forum' started by justtrying, Apr 29, 2012.

  1. justtrying

    Thread Starter Senior Member

    Mar 9, 2011
    Back to try and find a better solution for my previous problem...

    We got the switch to work when the device is being turned on, but we want to configure it so that the modes can be switched during operation. Because there are while loops and infinite loops, we found it difficult to come up with a way to monitor the switch and now I am trying to use interrupts. I got a code, which seems to be limping along, but I have to close and reopen serial port to see the interrupt kick in. I am not sure what is wrong.

    Code ( (Unknown Language)):
    1.  void setup() {
    3.   Serial.begin(9600);          // start serial communication
    4.   pinMode(2, INPUT);          // make pin 2 an input pin
    6.   // attach routine Learn() to ext interrupt 0, and
    7.   // generate it when the associated pin (7) falls from
    8.   // HIGH to LOW. Alternatives are LOW, CHANGE, RISING.
    9.   attachInterrupt(0, Learn, CHANGE);
    10. }
    13. void loop() {
    14.   Trigger();
    15.   delay(200);
    16. }
    So the way I see it is that it will run in trigger mode until interrupt is executed.

    One thing I saw is that when interrupt happens, everything freezes, both Trigger and Learn functions uses millis to determine sampling rate, so maybe I cannot even use this approach? Both are large functions that call other functions, so I am not posting the bloated thing here.

    Any suggestions are appreciated. What we want to do is to allow the user to switch from Trigger mode into Learn, the way it is now, you have to turn the device off first. The problem seems to be due to the fact that Trigger mode is a while(true) loop to make it constantly gather data.