Pages: [1]   Go Down
Author Topic: Motor drive triggering interrupt  (Read 603 times)
0 Members and 1 Guest are viewing this topic.
California
Offline Offline
Full Member
***
Karma: 0
Posts: 144
One man with the truth is a majority.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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);   
}

}
Logged

California
Offline Offline
Full Member
***
Karma: 0
Posts: 144
One man with the truth is a majority.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

nr Bundaberg, Australia
Offline Offline
Tesla Member
***
Karma: 126
Posts: 8471
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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 smiley

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

« Last Edit: March 14, 2011, 08:50:13 pm by Graynomad » Logged

Rob Gray aka the GRAYnomad www.robgray.com

California
Offline Offline
Full Member
***
Karma: 0
Posts: 144
One man with the truth is a majority.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

nr Bundaberg, Australia
Offline Offline
Tesla Member
***
Karma: 126
Posts: 8471
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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
 
« Last Edit: March 15, 2011, 03:37:21 am by Graynomad » Logged

Rob Gray aka the GRAYnomad www.robgray.com

California
Offline Offline
Full Member
***
Karma: 0
Posts: 144
One man with the truth is a majority.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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.
Logged

New Hampshire
Offline Offline
God Member
*****
Karma: 17
Posts: 781
There are 10 kinds of people, those who know binary, and those who don't.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged


California
Offline Offline
Full Member
***
Karma: 0
Posts: 144
One man with the truth is a majority.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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! smiley

Well...that was certainly a learning experience. smiley-confuse

Quote
declare endposflag and homeposflag volatile.

I now have that in my code. Thanks.
Logged

Pages: [1]   Go Up
Jump to: