Go Down

Topic: Motor drive triggering interrupt (Read 703 times) previous topic - next topic

Pokey

Hello,

For some reason, when I drive my motor using the following test code it triggers interrupt 0 even though that interrupt is connected to a separate sensor.

Every time the motor stops (see my test code in setup), LED 13 turns on.

I would appreciate any thoughts. TIA
Code: [Select]
int const shpwm = 12; //PWM shuttle drive
int const shdir = 22; //shuttle direction
int ledPin = 13;
int endposflag = 0;
int homeposflag = 0;
int const stp = 0;
int const fwd = 1;
int const rev = 2;

void setup() {               
  // initialize the digital pin as an output.
  // Pin 13 has an LED connected on most Arduino boards:
        pinMode(13, OUTPUT);
digitalWrite(13, LOW);
        pinMode(shpwm, OUTPUT);
        analogWrite(shpwm, 0);
pinMode(shdir, OUTPUT);
        digitalWrite(shdir, LOW);
        pinMode(2, INPUT); //interrupt 0
        pinMode(3, INPUT); //interrupt 1
        digitalWrite(2, HIGH);
        digitalWrite(3, HIGH);
        attachInterrupt(0, homepos, FALLING);
        attachInterrupt(1, endpos, FALLING);
        endposflag = 0;
        homeposflag = 0;
        drive_shuttle(rev);
        delay(250);
        drive_shuttle(stp);
}

void loop() {
  if(endposflag){ endposflag=0; analogWrite(13, 50); }//drive_shuttle(rev);  }
  if(homeposflag){ homeposflag=0; analogWrite(13, 255); }//drive_shuttle(fwd);  }
}



void endpos(){ endposflag=1; }

void homepos(){ homeposflag=1; }



void drive_shuttle(int dir){
int speed = 175;
// forwards with soft start
if(dir==fwd){
analogWrite(shpwm, 0);
                        delay(100);
digitalWrite(shdir, HIGH);
while(speed < 255){
analogWrite(shpwm, speed);   
speed = speed + 20;
delay(20);                           
}
}
// backwards with soft start
if(dir==rev){
analogWrite(shpwm, 0);
                        delay(100);
                        digitalWrite(shdir, LOW);
while(speed < 255)  {
analogWrite(shpwm, speed);   
speed = speed + 20;
delay(20);                           
}
}
if(dir==stp){
analogWrite(shpwm, 0);   
}

}


Pokey

I should also note that:

a) I am driving the motor through an H-bridge circuit (Acroname 3A Back-EMF).

b) the interrupts are triggered when their pins (2 and 3) are pulled low by slot sensors.

Graynomad

#2
Mar 15, 2011, 02:45 am Last Edit: Mar 15, 2011, 02:50 am by Graynomad Reason: 1
Pins 2 and 3 are the serial connection, not a great choice really. If you were doing some Serial.prints() that might explain it but I don't see any. Still any activity on the serial port would cause an interrupt.

You say "pins 2 and 3" but int 0 and 1 are on pins 4 and 5. Do you have the wires on the right pins?

Or are you using "Arduino pins", jees I hate this bloody pin mapping :)

If you're using Arduino digital pins then forget the above.

Quote
Every time the motor stops (see my test code in setup), LED 13 turns on.

What pin does this write to?

analogWrite(13, 50);

a digital write would turn the LED on, not sure what analogWrite() does.

______
Rob

Rob Gray aka the GRAYnomad www.robgray.com

Pokey

Hi Rob,

Quote
You say "pins 2 and 3" but int 0 and 1 are on pins 4 and 5. Do you have the wires on the right pins?


I'm using an Arduino Mega (older version). It has ints 0 and 1 on digital pins 2 & 3. The interrupts work correctly when I manually trigger the slot sensors.

Quote
analogWrite(13, 50);

That makes LED 13 glow dimly so I can tell interrupt 1 was triggered.

Graynomad

#4
Mar 15, 2011, 09:35 am Last Edit: Mar 15, 2011, 09:37 am by Graynomad Reason: 1
So in setup you do a

drive_shuttle(rev);

drive_shuttle() appears to ramp the shuttle speed from 175 to 255 then return

What if the shuttle has reached the end by then, or by the end of the delay(250) so endposflag has already been set before you do the drive_shuttle (stp).

In this case the test in loop will be true.

______
Rob

Rob Gray aka the GRAYnomad www.robgray.com

Pokey

Quote
What if the shuttle has reached the end by then...


The shuttle is on a rail that is 5 feet long. I have it in the middle of its run. In the time that the test drive takes to complete the shuttle only moves a few inches. It never gets anywhere near the sensors.

I know this sounds crazy but if I set my loop to:
Code: [Select]
void loop() {
  if(endposflag){ endposflag=0;  drive_shuttle(rev);  }
  if(homeposflag){ homeposflag=0; drive_shuttle(fwd);  }
}


The shuttle will jerk back and forth moving within only a few inches as the interrupts trigger forcing it change direction. Again, the sensors are easily 15 inches away from the motor.

I am completely stumped...thank you for the feedback though.

jraskell

declare endposflag and homeposflag volatile.  You should declare any variables that will be used in your main code and interrupts this way.  This prevents the compiler from potentially optimizing out code it shouldn't.

Pokey

Don't ask me how, but the idea occurred to me that the LEDs in the slot sensors may be flickering when the Arduino and the H-bridge Vcc suddenly draw power. So I isolated the power supply for the LED's and the problem went away...the interrupts trigger when the shuttle hits the sensors! :)

Well...that was certainly a learning experience. :~

Quote
declare endposflag and homeposflag volatile.


I now have that in my code. Thanks.

Go Up