attachinterrupt() for running motor using multiple interrupts

Hiii…
I am beginner…

please tell me whats the mistake in my code?
I want to use multiple interrupts several times… on pin 2,3, 19 (may be more later)
cant I use attach interrupts inside void loop ()?
have I done reattaching interrupts correctly?

I am dettaching the interrupt as it is coming to monitor the next pins as I have to run motor parallelly with the LED dot matrix glowing (not included in the code here…)

Please help…

#include <Servo.h> 
Servo myservo; 
const int ledPin =  13; 
const int Pin0 =  2; // interrupt pins- 2,3,19 on arduino mega
const int Pin1 =  3;
const int Pin2 =  19;
int State0 = 0; // state of interrupts
int State1 = 0;
int State2 = 0;


volatile int attached_pin = 0;
// Next pin that needs to be monitored.
volatile int next_pin = 0;

void run_motor()
{
  for(pos=0; pos>=90; pos-=1)
  {
    myservo.write(pos);
    delay(50);
  }
  for(pos=90; pos<=0; pos+=1)
  {
    myservo.write(pos);
    delay(50);
  }
}

void read_val0()
{
  State0 = digitalRead(Pin0);
  next_pin = Pin1;
}

void read_val1()
{
  State1 = digitalRead(Pin1);
  next_pin = EMGPin2;
}

void read_val2()
{
  State2 = digitalRead(Pin2);
  next_pin = 0;
}

void setup()
{
  Serial.begin(9600); 
  pinMode(2, INPUT); 
   pinMode(3, INPUT);
    pinMode(19, INPUT);
  pinMode(13, OUTPUT); 
  myservo.attach(11);
}

void loop()

  {
  myservo.write(0);
  State0 = 0;
  State1 = 0;
  State2 = 0;
  attached_pin = Pin0;
  next_pin = 0;
  attachInterrupt(Pin0, read_val0, RISING);
  {
    if(next_pin != 0)
    {
      detachInterrupt(attached_pin);
      attached_pin = next_pin;
      next_pin = 0;
            if(attached_pin == Pin1)
            {
            attachInterrupt(Pin1, read_val1, RISING);
            } 
            else if(attached_pin = Pin2) 
            {
            attachInterrupt(Pin2, read_val2, RISING);
            }
    }
    if(Pin0 == 1 && Pin1 == 1 && Pin2 == 1)
    {
        run_motor();
    }
  }
}

Did you mean main() or loop()? The answer is the same, you can attach an interrupt anywhere you want in your code. Whether or not that makes much sense is a completely different story. Since you are using three different interrupts it seems like this could be refactored to leave all three on all the time.

Please tell me wats wrong with this code.
Interrupts are not working…

#include <Servo.h> 
Servo myservo; 
const int ledPin =  13; 
const int EMGPin0 =  2; 
int EMGState0 = 0;

void setup()
{
  Serial.begin(9600); 
  pinMode(2, INPUT); 
    pinMode(3, INPUT); 
  pinMode(13, OUTPUT); 
  myservo.attach(11);
  attachInterrupt(2, read_val0, RISING);
}

void loop()
  {
}

void read_val0()
{
  EMGState0 = digitalRead(EMGPin0);
  Serial.println(" 1st interrupt done ");
    digitalWrite(13, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(5000);              // wait for a second
  digitalWrite(13, LOW);    // turn the LED off by making the voltage LOW
  delay(5000);
}

Serial code in an ISR has a nasty habit of locking things up.

The delays in your ISR will definitely lock up the program. delay relies on the timer0 interrupt to work and its turned off during your interrupt function. Interrupts should be fast fast. Anything that can wait around for 10 seconds shouldn't be using an interrupt.

What type of Arduino are you using?

You can’t print inside an interrupt routine. Please review the attachInterrupt page.

Normally, you would just do a little work, like set a flag or increment a counter.

Cheers,
/dev

thanks all..

@dev.. can I do it by adding dettach interrupts just after getting the interrupt high..? attach interrupt - increment counter- dettach Interrupt - Print?? @Delta_G .. I m using mega.. I had run a motor for 5 seconds whenever I get a interrupt high.. how should I do it? can It be like attach interrupt - glow LEDs for 1 second- dettach Interrupt - run the motor? Thanks

When you get an interrupt set a boolean variable to true and do nothing else in the ISR. Then in your main loop which is empty now test that variable and when it is true run your motor and set it back to false. Don't forget to make that variable volatile since it will change in the ISR.

You seem to think that attachInterrupt() blocks until something happens. It does not. Your code makes no sense.

nehasingh: I am beginner... ...SNIP.... I want to use multiple interrupts several times... on pin 2,3, 19 (may be more later)

WHY ?

I have a simple philosophy. Don't use interrupts if you don't know how to use them (apart from in a program intended only to learn about interrupts).

Debugging problems caused by interrupts is difficult even when you are experienced.

Please tell us in English (not code) what you want to achieve.

Nick Gammon has a good tutorial on interrupts if you want to learn about them.

Unless you have a very particular requirement it would be normal to attach interrupts in setup().

...R

nehasingh:
I had run a motor for 5 seconds whenever I get a interrupt high…

If you are measuring time in seconds (rather than microseconds) perhaps there is no need to use an interrupt?

…R

Duplicate post!