1 Reply Latest reply on Feb 22, 2016 9:31 AM by DiegoV_Intel

    External interrupt has no effect

    danush

      I'm a beginner to arduino, using intel edison to experiment arduino programs. While i was enjoying interrupt programs, i ran into a problem. I'm not sure what is the reason behind this problem.

      Here is my code:

      //Pin numbers
      int red = 7;
      int yellow = 8;
      int green = 9;
      //For giving delay
      int stop = 6;
      int yield = 2;
      int go = 6;
      
      
      void setup()
      {
        Serial.begin(9600);
        pinMode(red, OUTPUT);
        pinMode(yellow, OUTPUT);
        pinMode(green, OUTPUT);
        pinMode(13, OUTPUT);
        digitalWrite(red, LOW);
        digitalWrite(yellow, LOW);
        digitalWrite(green, LOW);
        digitalWrite(13, LOW);
        attachInterrupt(0,changeTime,FALLING);
      }
      
      
      void loop()
      {
        stopLight(stop);
        yieldLight(yield);
        goLight(go);
      }
      
      
      void stopLight(int time)
      {
        digitalWrite(red, HIGH);
        digitalWrite(yellow, LOW);
        digitalWrite(green, LOW);
        Serial.println("Light mode: Stop");
        delay(time * 1000);
      }
      
      
      void yieldLight(int time)
      {
        digitalWrite(red, LOW);
        digitalWrite(yellow, HIGH);
        digitalWrite(green, LOW);
        Serial.println("Light mode: Yield");
        delay(time * 1000);
      }
      
      
      void goLight(int time)
      {
        digitalWrite(red, LOW);
        digitalWrite(yellow, LOW);
        digitalWrite(green, HIGH);
        Serial.print("Light mode: Go - ");
        Serial.println(time);
        delay(time * 1000);
      }
      
      
      void changeTime()
      {
        if(go == 6)
        {
          go = 10;
          digitalWrite(13,HIGH);
        }
        else
        {
          go = 6;
          digitalWrite(13,LOW);
        }
      }
      
      

      When i give an interrupt the value of "go" doesn't change and the led on pin 13 keeps glowing except at the instance when i give the interrupt.

       

      Is there any problem with my code or does the external interrupt work in a different way?

        • 1. Re: External interrupt has no effect
          DiegoV_Intel

          Hi danush,

           

          I tested your code and I didn't have problems in turning ON/OFF the LED. I recommend you to try the following example code to check if the attachInterrupt() method works as you expect it to.

           

          int pin = 13;
          volatile int state = LOW;
          
          void setup()
          {
            pinMode(pin, OUTPUT);
            // Use the modes CHANGE, RISING or FALLING
            attachInterrupt(2, blink, CHANGE);
          }
          
          void loop()
          {
            digitalWrite(pin, state);
          }
          
          void blink()
          {
            state = !state;
            // After the ISR ends, the code in the loop will be executed as usual
          }
          

           

           

          Regards,

          Diego