Voting resources, early voting, and poll worker information - VOTE. ... Adafruit is open and shipping.
0

Using counter to control stepper position.
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Using counter to control stepper position.

by Rmac on Thu Sep 01, 2011 11:00 pm

I have a sketch that uses the Arduino UNO and Adafruit motor shield to control a stepper motor.
I have managed to get the motor to home to a positioning switch. Now I want to be able to move the stepper to three different positions using pushbuttons. I don't want to go back to home each time I move to the new location. Can I use a counter function and if so how do I set it up? The stepper will HOME in the Park position and I want to step to three positions . Rev - 40 degrees from home Ntrk - 80 degrees from home and Dr - 120 degrees from home.
Attached is my current sketch. I have the three buttons programed to step at this time but no count function.
Rmac

Code: Select all | TOGGLE FULL SIZE
#include <buttons.h>
#include <Bounce.h>
#include <AccelStepper.h>
#include <AFMotor.h>

/*
   Four switch stepper motor Program
 
 //Turns on and off digital pins connected to pins 9,10,11,13, as output when  pressing  pushbuttons
 //(Park,Rev,Ntrl & Dr.) attached to digital pins 14,15,16 & 17.
 // The Home switch is attached to digital pin 18.
 
 The circuit:
 * Using AdaFruit shield with motor output on M3 and M4
 * pushbuttons  are attached to digital pins 14,15,16,17,and switch 18 +5V supply for switches.
 // analog pins A0,A1,A2,A3, and A4 are used as digital pins 14 through 18:
 * 10K resistors attached to digital pins from ground
 
 
 */

// constants won't change. They're used here to
// set pin numbers:

const int buttonPinPark = 14;  //Setting Park button to Pin 14
const int buttonPinRev = 15;  //Setting Reverse button to Pin 15
const int buttonPinNtrl = 16;  //Setting Neutral button to Pin 16
const int buttonPinDr = 17; //Setting Drive button to Pin 17
const int buttonPinHome = 18; //Used as input for the home position on pin 18

int buttonStatePark = 0;       //Setting Park button state to off
int buttonStateRev = 0;       //Setting Reverse button state to off
int buttonStateNtrl = 0;       //Setting Neutral button state to off
int buttonStateDr = 0;       //Setting Drive button state to off
int buttonStateHome = 0;       //Setting Drive button state to off
//Setting Val to off 

const int Pin9 =  9;      // PIN OUTPUT NUMBER
const int Pin10 =  10;      // PIN OUTPUT NUMBER
const int Pin11 =  11;      // PIN OUTPUT NUMBER
const int Pin13 =  13;     // PIN OUTPUT NUMBER


AF_Stepper motor(200, 2);  // Names motor, sets steps to 200 per revolution and assigns shield motor output to Stepper motor on M3 and M4 connectors.

void setup() {

  // open the serial port at 9600 bps:
  Serial.begin(9600);


  // initialize the pin as an output:
  pinMode (Pin9,OUTPUT);   
  pinMode (Pin10,OUTPUT);
  pinMode (Pin11,OUTPUT);   
  pinMode (Pin13,OUTPUT);



  // initialize the pushbutton pin as an input:
  pinMode(buttonPinPark, INPUT); 
  pinMode(buttonPinRev, INPUT);
  pinMode(buttonPinNtrl, INPUT); 
  pinMode(buttonPinDr, INPUT);
  pinMode(buttonPinHome, INPUT);

  motor.setSpeed(10);  // 10 rpm 
}

void loop()

{
 

  // check if the Park pushbutton is pressed.
  // if it is, the buttonStatePark is HIGH:

  if (buttonStatePark == HIGH)
  {
    // Have output on pin 9 (5 volts:)
    digitalWrite (Pin9, HIGH);
    int counter = 0 ;
    int val = 0;

    val = digitalRead(buttonPinHome);   // read the input pin


    // counter limits the number of steps taken before forced exit from loop.

    while ((val != HIGH) && ( counter < 300 )) // set counter to appropriate value.

    {

      // step until the home limit switch is detected       

      motor.step(1, FORWARD, SINGLE); // move forward one step.

      val = digitalRead(buttonPinHome);   // read the input pin again



      // May not need this, but it’s a good safety net.

      if ( val == HIGH) // if the pin is high

        break ;  // then break out of while loop


      counter++ ; // increment the counter, are we at 300 yet?

   
      //  digitalWrite(buttonStateHome, val);    // sets the Home Pin to the button's value

    } // end while

  }
  else
  {
    // Set Pin9 Off:
    digitalWrite(Pin9, LOW);
  }

  {
   

    // check if the Rev pushbutton is pressed.
    // if it is, the buttonStateRev is HIGH:

    if (buttonStateRev == HIGH)
    {
      // Have output on pin 10 (5 volts:
      digitalWrite (Pin10, HIGH);

      motor.step(40, FORWARD, SINGLE);

    }
    else
    {
      // Set Pin10 Off:
      digitalWrite(Pin10, LOW);
    }
    {
     
      // check if the Ntrl pushbutton is pressed.
      // if it is, the buttonStateNtrl is HIGH:

      if (buttonStateNtrl == HIGH)
      {
        // Have output on pin 11 (5 volts:
        digitalWrite (Pin11, HIGH);

        motor.step(80, FORWARD, SINGLE);

      }
      else
      {
        // Set Pin11 Off:
        digitalWrite(Pin11, LOW);
      }

      {
       

        // check if the Dr pushbutton is pressed.
        // if it is, the buttonStateDr is HIGH:

        if (buttonStateDr == HIGH)
        {
          // Have output on pin 13 (5 volts:
          digitalWrite (Pin13, HIGH);

          motor.step(120, FORWARD, SINGLE);

        }
        else
        {
          // Set Pin13 Off:
          digitalWrite(Pin13, LOW);

        }
      }
    }
  }
}
Rmac
 
Posts: 31
Joined: Mon Jul 25, 2011 11:32 am

Re: Using counter to control stepper position.

by adafruit_support_bill on Fri Sep 02, 2011 6:08 am

You need to keep track of your current position. When you reach Home, set your current position to 0. If you step 80 steps out, you set your current position to 80. If you need to get to position 40, just do the math: 40 - 80 = -40, so you need to go 40 steps in reverse.

adafruit_support_bill
 
Posts: 79031
Joined: Sat Feb 07, 2009 10:11 am

Re: Using counter to control stepper position.

by Rmac on Fri Sep 02, 2011 9:12 am

Thanks
Rmac
 
Posts: 31
Joined: Mon Jul 25, 2011 11:32 am

Please be positive and constructive with your questions and comments.