Hello World,
I am currently building a stepper driven camera dolly and the electronics work great so far. The logic is an Arduino Micro and the stepper driver is a Pololu A4988. However, I still have troubles with the programming especially interrupts.
There are two buttons triggering an interrupt. One on each side of the dolly so the Arduino knows when the sled has hit the end of the rail. Now what the code below is supposed to do is peddle free 3000 steps (that’s half a rotation for the geared stepper) if one button is HIGH on start. This works. Then attach the interrupts and go right until it hits a button again. Then go left in iterations of 1000 until it hits the other button.
Well the peddling free part works then a small delay and then the motor starts turning counter clockwise and never stops. When I press the interrupt buttons I can feel a small jolt in the stepper, so there is definitely something happening, but the motor won’t stop and change direction.
My best guess is that I have misunderstood the logic behind interrupt CHANGE or it is something really stupid and simple as so often when I try to program.
const int buttonStopLeft = 2;
const int buttonStopRight = 3;
const int stepperPin = 7;
const int directionPin = 8;
const boolean left = true;
const boolean right = false;
int motorPhaseDelay = 400;
boolean contactLeft = false;
boolean contactRight = false;
void setup(){
pinMode(buttonStopLeft, INPUT);
pinMode(buttonStopRight, INPUT);
pinMode(directionPin, OUTPUT);
pinMode(stepperPin, OUTPUT);
if(digitalRead(buttonStopRight) == HIGH){
photoStep(left,3000);
}
if(digitalRead(buttonStopLeft) == HIGH){
photoStep(right,3000);
}
delay(3000);
attachInterrupt(1, contactToggleLeft, CHANGE);
attachInterrupt(0, contactToggleRight, CHANGE);
videoStep(right); //Go Right
delay(3000);
while(contactLeft == false){ //Go left and count steps
photoStep(left,1000);
}
delay(3000);
}
void contactToggleLeft(){
static unsigned long lastInterruptTime = 0;//Debounce
unsigned long interruptTime = millis();
if (interruptTime - lastInterruptTime > 100){
contactLeft != contactLeft;
}
lastInterruptTime = interruptTime;
}
void contactToggleRight(){
static unsigned long lastInterruptTime = 0;//Debounce
unsigned long interruptTime = millis();
if (interruptTime - lastInterruptTime > 100){
contactRight != contactRight;
}
lastInterruptTime = interruptTime;
}
void photoStep(boolean dir,int stepAmount){
digitalWrite(directionPin,dir);
int i = 0;
if(dir == true){
while(i<stepAmount && contactLeft == false){
steps();
i++;
}
}
else{
while(i < stepAmount && contactRight == false){
steps();
i++;
}
}
}
void videoStep(boolean dir){
digitalWrite(directionPin,dir);
if(dir == true){
while(contactLeft == false){
steps();
}
}
else{
while(contactRight == false){
steps();
}
}
}
void steps(){
digitalWrite(stepperPin, HIGH);
delayMicroseconds(motorPhaseDelay);
digitalWrite(stepperPin, LOW);
delayMicroseconds(motorPhaseDelay);
}
void loop(){
}
Thanks in advance
MooJuice