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