Motor drive triggering interrupt

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

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

}

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.

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 :slight_smile:

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

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

Hi Rob,

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.

analogWrite(13, 50);

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

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

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:

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.

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.

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! :slight_smile:

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

declare endposflag and homeposflag volatile.

I now have that in my code. Thanks.