2

So I have tooled around a bit more with my stepper motor and my button sensor (I want to have the motor stop when the button is pressed; which I have for the most part figured out). However, in an effort to simplify my code I have managed to get my motor to stop moving altogether. In the code that follows the idea was to condense the main actions of the motor (to raise the platform up to the glass, take a picture, then to lower back down and repeat the process) by containing the majority of the actions within a single if statement so that the computer would have a better shot at reading it the way I intended.

#include <AccelStepper.h>
const int buttonPin=4;  //number of the pushbutton pin
const int opto_shoot=2;  // Pin that controls the shoot function
int maxDistance=-12000;  //intial distance for motor to move
int button_state=0;
int sensorPin=0;  //select input pin for the photocell
int sensorValue=0;  //variable to store the vaule coming from the photocell
int motorSpeed = 9600; //maximum steps per second (about 3rps / at 16 microsteps)
int motorAccel = 80000; //steps/second/second to accelerate
int motorDirPin = 8; //digital pin 8
int motorStepPin = 9; //digital pin 9


//set up the accelStepper intance
//the "1" tells it we are using a driver
AccelStepper stepper(1, motorStepPin, motorDirPin); 


void setup(){  
  pinMode(buttonPin,INPUT);  //set that the button is an input
  pinMode(opto_shoot,OUTPUT);  // set the pin that controls the shoot function
  stepper.setMaxSpeed(motorSpeed);
  stepper.setSpeed(motorSpeed);
  stepper.setAcceleration(motorAccel);

}

void loop(){
   stepper.moveTo(maxDistance); //move 2000 steps (gets close to the top)
   stepper.run();
   if (digitalRead(buttonPin) == HIGH){
       stepper.stop();
       stepper.runToPosition();
       digitalWrite(opto_shoot,HIGH);  //SHOOT
       delay(500);
       digitalWrite(opto_shoot,LOW);
       delay(1);
       goto Lower;  }
 //      for(int i=0;i<36;i++)
 //        Serial.read();
 //
      else{   
  if(stepper.distanceToGo() == 0){
    stepper.stop();
    stepper.runToPosition();
    stepper.moveTo(maxDistance);
  }
   }
   Lower:{
    maxDistance=-1*(maxDistance+500);
       stepper.moveTo(maxDistance);}
   //these must be called as often as possible to ensure smooth operation
  //any delay will cause jerky motion
  stepper.run();
}

Please help me to understand where I have gone wrong with this code.

1 Answer 1

2

One thing I would change is to use an interrupt for the digital pin. I don't have my Arduino set up right now but I believe the attachInterrupt method would work. This will let you run a block of code only when an certain event occurs (i.e. the button has been pressed).

int isPressed = 0;    // Init to off state
// Other init stuff goes here.

void setup(){
    // Other setup code...

    // Set up the interrupt so that when the button is pressed, myISR function 
    // runs. I opted to run it on the rising edge (i.e. low to high).
    attachInterrupt(buttonPin, myISR, RISING); 
}

void loop(){
    // Check the state of isPressed.
    if (isPressed == 0){
        // Do stuff, let the motor move.
    }
    else{
        // This runs when the button has been pressed.
        // Turn off the flag so this runs just once.
        isPressed = 0;

        // Then stop the motor until further notice.
        stepper.stop();
        stepper.runToPosition();

        // Do other stuff now that it's off.
    }
}

// ISR = interrupt service routine
void myISR() {
    // Set the state of isPressed to ON.
    isPressed = 1;
}

In addition, maybe you want to turn the motor back on? So just set up another ISR, maybe attachInterrupt(anotherButtonPin, anotherISR, RISING); which would call anotherISR when anotherButtonPin is held down (i.e. a pin different than the one to turn it off).

Here's a simple example which stops a motor and here are the interrupts docs.

Lastly, did you have something working after asking your other question? I suggest saving your broken code separately and returning back to what worked. Try to understand what is different. Another suggestion would be to create the simplest code which stops a moving motor, say after a certain amount of time passes. Then try to integrate the interrupt so that it controls the stopping of the motor instead. Good luck!


EDIT: I also noticed you are using goto Lower; rather than making Lower a function and calling it like Lower();. The goto command is usually considered bad practice. See GOTO still considered harmful? for more details.

3
  • 1
    and another +1 for the goto bashing! :-)
    – zmo
    Commented Jul 2, 2013 at 12:16
  • Yeah, I felt pretty bad about using it myself, but I was working with another programmer and he suggested it as a hack to accomplish what I was doing. Working in cahoots with another programmer friend of mine the code is currently based mostly around functions, and seems to basically do what I want to. Thanks for all your help peeps! I'll have to pay it forward and try to be more help to my fellow programmers in da future. Commented Jul 2, 2013 at 17:52
  • @MikePinnell You're welcome, hope it helped. Also, don't forget to click "Accept" on any answers that best answered your questions. (+2 rep for you and +15 for the answerer)
    – gary
    Commented Jul 5, 2013 at 12:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.