0

I experimenting with the interrupt ISR using Arduino Uno. In my circuit, I am taking two switches which are connected with Uno's interrupt pins, pins 2 and 3, and also taking multiple LEDs which are connected with other pins.

My logic is when I press switch1 (which is connected to pin 3) LEDs should be turned on by the interrupt service routine and when I press switch2 (which is connected to pin 2) LEDs should be turned off by the interrupt service routine.

My problem is that when I pressed switch1 LEDs are turned on but when I pressed switch2 LEDs are not turned off.

Here's my code:

int switch1 = 3;
int switch2 = 2;
int led1 = 13;
int led2 = 12;
int led3 = 11;
int led4 = 10;
int led5 = 9;
int led6 = 8;
int led7 = 7;
volatile bool flag1 = false;
volatile bool flag2 = false;
//volatile byte state = LOW;

void setup()
{
  pinMode(switch1, INPUT_PULLUP);
  pinMode(switch2, INPUT_PULLUP);
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  pinMode(led4, OUTPUT);
  pinMode(led5, OUTPUT);
  pinMode(led6, OUTPUT);
  pinMode(led7, OUTPUT);
  attachInterrupt(digitalPinToInterrupt(switch1), &setFlag1, CHANGE);
  attachInterrupt(digitalPinToInterrupt(switch2), &setFlag2, CHANGE);
}

void loop()
{
  if (flag1)
  {
    off();
  }
  if (flag2)
  {
    blink1();
  }

}

void blink1()
{
  attachInterrupt(digitalPinToInterrupt(switch2), setFlag2, CHANGE);
  digitalWrite(led1, HIGH);
  blink2();
}

void blink2()
{
  digitalWrite(led2, HIGH);
  blink3();
}

void blink3()
{
  digitalWrite(led3, HIGH);
  blink4();
}

void blink4()
{
  digitalWrite(led4, HIGH);
  blink5();
}

void blink5()
{
  while (1)
  {
    digitalWrite(led5, HIGH);
    delay(10);
    digitalWrite(led5, LOW);
    delay(10);
  }
}

void off()
{
  digitalWrite(led1, LOW);
  digitalWrite(led2, LOW);
  digitalWrite(led3, LOW);
  digitalWrite(led4, LOW);
  digitalWrite(led5, LOW);
  attachInterrupt(digitalPinToInterrupt(switch1), setFlag1, CHANGE);
}

void setFlag1()
{
  detachInterrupt(digitalPinToInterrupt(switch1));
  flag1 = true;
}

void setFlag2()
{
  detachInterrupt(digitalPinToInterrupt(switch2));
  flag2 = true;
}
  • 1
    I don't think I've ever seen a call chain quite like that before. Also: 1) Do you know what while(1) does? 2) Why are you detaching and attaching interrupts all the time? – Majenko Jun 20 '17 at 12:48
  • 2
    Any reason you make this o convoluted? – Edgar Bonet Jun 20 '17 at 12:53
  • I don't see how you will ever leave the blink5 function. What is the purpose of an infinite loop in it? – Nick Gammon Jan 18 '18 at 10:00
2

You are being way way too convoluted with this. You're overthinking it.

The code should be as simple as (LED stuff omitted):

volatile bool running = false;
const uint8_t switch1 =3;
const uint8_t switch2 =2;

void setup() {
    pinMode(switch1, INPUT_PULLUP);
    pinMode(switch2, INPUT_PULLUP);
    attachInterrupt(digitalPinToInterrupt(switch1), turnOn, FALLING);
    attachInterrupt(digitalPinToInterrupt(switch2), turnOff, FALLING);
}

void loop() {
    if (running) {
        // Do something
    } else {
        // Do something else
    }
}

void turnOn() {
    running = true;
}

void turnOff() {
    running = false;
}

Note that what goes in loop() should be non-blocking, or at least have a reasonably short duration, so that it is able to pick up the changed running variable.

However, for something like this, interrupts are pretty pointless.

  • Thanks for reply. Actually this is not my solution but you're right according to my question. It's my fault, I asked this question in wrong way. I am sorry for it . Well Now I am letting you know what I am actually want. There are several functions which will perform respective tasks. Executions of those function may or may not in a sequence but here I was showing in a sequence. There are two interrupts and each interrupt has an associate function means whenever interrupt will occur firstly that associate function should execute after execution this particular function process may go (contd) – Prayuktibid Jun 21 '17 at 4:57
  • (contd) any other function depend on condition. Among those function there is a function which will execute for real time that's why I have taken while(1)loop. And second interrupt associate with only one function and its task is different not associate with other functions but here I was showing it wrong. So my intention is whenever an Interrupt occur process execution should go their respective functions and functions are may or may not associate with each other. – Prayuktibid Jun 21 '17 at 5:12
  • Well, that is an entirely different kettle of fish. A completely different question that requires a completely different answer (and possibly a completely different understanding of what interrupts actually do on your part). – Majenko Jun 21 '17 at 9:20
  • Now I am letting you know what I am actually want. - comments are not for completely rewording the question. You asked the question, you amend it to state what you actually want to do. – Nick Gammon Jan 18 '18 at 9:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.