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

Setting a Home position for a stepper motor
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Re: Setting a Home position for a stepper motor

by adafruit_support_bill on Thu Aug 18, 2011 5:47 pm

How do you have the switch wired?

Code: Select all | TOGGLE FULL SIZE
        if (buttonStateHome == HIGH)
        // read the input pin
        digitalWrite(buttonStateHome,HIGH ); 


And what is this part for? Is buttonStateHome an input or an output? As is, this statement enables the internal pullup resistor.

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

Re: Setting a Home position for a stepper motor

by Rmac on Fri Aug 19, 2011 10:39 am

I am wondering if my naming of the loop is a problem. I have named it void loop() for no other reason than I see it in several examples I have studied while putting the program together.
The Microsoft C book I have states that void has three types.
The one I'm using
Void func () It indicates that a function does not return a value.
Does that mean it doesn't read while in the while loop?

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 pin 9,10,11,13,2 as output when  pressing  pushbuttons
 (Park,Rev,Ntrl, Dr and Home ) attached to digital pins 14,15,16,17and 18
 
 The circuit:
 * Using AfaFruit shield with motor output on M3 and M4
 * pushbuttons  are attached to digital pins 14,15,16,17,qnd 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 2 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

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
const int Pin2 =  2;     // PIN OUTPUT NUMBER

AF_Stepper motor(200, 2);

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);
  pinMode (Pin2,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()

{
  // delay 10 milliseconds before the next reading:
  delay(10);

  // read the state of the Park pushbutton value:
  buttonStatePark = digitalRead(buttonPinPark);    //code for Park button
  Serial.print("Park = "); 
  Serial.println(buttonStatePark, DEC);  // print as an ASCII-encoded decimal:

 
     // delay 10 milliseconds before the next reading:
  delay(10);

  // read the state of the Home pushbutton value:
  buttonStateHome = digitalRead(buttonPinHome);    //code for Park button
  Serial.print("Home = "); 
  Serial.println(buttonStateHome, DEC);  // print as an ASCII-encoded decimal:


  // 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);
   
    while (buttonStateHome != HIGH)
    // step until the home limit switch is detected
   
    motor.step(20, FORWARD, SINGLE);
   
    // read the input pin
    digitalRead(buttonStateHome);   

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

  {
    // delay 10 milliseconds before the next reading:
    delay(10);

    // read the state of the Rev pushbutton value: 
    buttonStateRev = digitalRead(buttonPinRev);    //code for Rev button
    Serial.print("Rev = "); 
    Serial.println(buttonStateRev, DEC);  // print as an ASCII-encoded decimal:

    // 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, BACKWARD, SINGLE);

    }
    else
    {
      // Set Pin10 Off:
      digitalWrite(Pin10, LOW);
    }
    {
      // delay 10 milliseconds before the next reading:
      delay(10);

      // read the state of the Ntrl pushbutton value:
      buttonStateNtrl = digitalRead(buttonPinNtrl);    //code for Ntrl button
      Serial.print("Ntrl = "); 
      Serial.println(buttonStateNtrl, DEC);  // print as an ASCII-encoded decimal:
      // 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(60, FORWARD, SINGLE);

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

      {
        // delay 10 milliseconds before the next reading:
        delay(10);

        // read the state of the Rev pushbutton value: 
        buttonStateDr = digitalRead(buttonPinDr);    //code for Dr button
        Serial.print("Dr = "); 
        Serial.println(buttonStateDr, DEC);  // print as an ASCII-encoded decimal:

        // 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(80, FORWARD, SINGLE);

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

        }
      }
    }
  }
}

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

Re: Setting a Home position for a stepper motor

by adafruit_support_bill on Fri Aug 19, 2011 10:52 am

the "void loop()" function is required for Arduino sketches. It is called over and over as long as long as the board is running.

Your problem is likely due to wiring, confusing inputs with outputs or both.

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

Re: Setting a Home position for a stepper motor

by Rmac on Fri Aug 19, 2011 11:54 am

Thanks
When I use the monitor it shows the read out for the last button pushed until the command is completed so it never reads the Home switch
when it is contacted. Is it possible I need a bounce statement or a different button statement so it will start the read again before it completes the last command?
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 pin 9,10,11,13,2 as output when  pressing  pushbuttons
 (Park,Rev,Ntrl, Dr and Home ) attached to digital pins 14,15,16,17and 18
 
 The circuit:
 * Using AfaFruit shield with motor output on M3 and M4
 * pushbuttons  are attached to digital pins 14,15,16,17,qnd 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 2 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

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
const int Pin2 =  2;     // PIN OUTPUT NUMBER

AF_Stepper motor(200, 2);

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);
  pinMode (Pin2,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()

{
  // delay 10 milliseconds before the next reading:
  delay(10);

  // read the state of the Park pushbutton value:
  buttonStatePark = digitalRead(buttonPinPark);    //code for Park button
  Serial.print("Park = "); 
  Serial.println(buttonStatePark, DEC);  // print as an ASCII-encoded decimal:

 
     // delay 10 milliseconds before the next reading:
  delay(10);

  // read the state of the Home pushbutton value:
  buttonStateHome = digitalRead(buttonPinHome);    //code for Park button
  Serial.print("Home = "); 
  Serial.println(buttonStateHome, DEC);  // print as an ASCII-encoded decimal:


  // 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);
   
    while (buttonStateHome != HIGH)
    // step until the home limit switch is detected
   
    motor.step(20, FORWARD, SINGLE);
   
    // read the input pin
    digitalRead(buttonStateHome);   

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

  {
    // delay 10 milliseconds before the next reading:
    delay(10);

    // read the state of the Rev pushbutton value: 
    buttonStateRev = digitalRead(buttonPinRev);    //code for Rev button
    Serial.print("Rev = "); 
    Serial.println(buttonStateRev, DEC);  // print as an ASCII-encoded decimal:

    // 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, BACKWARD, SINGLE);

    }
    else
    {
      // Set Pin10 Off:
      digitalWrite(Pin10, LOW);
    }
    {
      // delay 10 milliseconds before the next reading:
      delay(10);

      // read the state of the Ntrl pushbutton value:
      buttonStateNtrl = digitalRead(buttonPinNtrl);    //code for Ntrl button
      Serial.print("Ntrl = "); 
      Serial.println(buttonStateNtrl, DEC);  // print as an ASCII-encoded decimal:
      // 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(60, FORWARD, SINGLE);

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

      {
        // delay 10 milliseconds before the next reading:
        delay(10);

        // read the state of the Rev pushbutton value: 
        buttonStateDr = digitalRead(buttonPinDr);    //code for Dr button
        Serial.print("Dr = "); 
        Serial.println(buttonStateDr, DEC);  // print as an ASCII-encoded decimal:

        // 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(80, FORWARD, SINGLE);

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

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

Re: Setting a Home position for a stepper motor

by adafruit_support_bill on Fri Aug 19, 2011 12:09 pm

Generally a limit-switch is held for as long as the mechanism is at the limit. So, if your motor is "home" the home switch should stay pressed.

But in your latest code you are still not reading the "home" button inside the loop.
Code: Select all | TOGGLE FULL SIZE
        while (buttonStateHome != HIGH)
        // step until the home limit switch is detected
       
        motor.step(20, FORWARD, SINGLE);
       
        // read the input pin
        digitalRead(buttonStateHome);   


You need some braces to establish the scope of your while loop like this:

Code: Select all | TOGGLE FULL SIZE
        while (buttonStateHome != HIGH)
       {
             // step until the home limit switch is detected
       
             motor.step(20, FORWARD, SINGLE);
       
             // read the input pin
             digitalRead(buttonStateHome); 
       }
 

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

Re: Setting a Home position for a stepper motor

by Rmac on Fri Aug 19, 2011 1:11 pm

I inserted the brackets as suggested but still get the same results. Motor run continuously. As soon as I push the Park button the loop stops. I get this on the monitor. It never reads the Home switch when it gets made.

Rev=0
Ntrl=0
Dr=0
Park=1
Home=0
The momitor stream stops here and does not start again so I assume no other commands or reads do either. I have to reset to get it going again. Program with changes below.
( I acks like you are holding the Park switch in.)

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 pin 9,10,11,13,2 as output when  pressing  pushbuttons
 (Park,Rev,Ntrl, Dr and Home ) attached to digital pins 14,15,16,17and 18
 
 The circuit:
 * Using AfaFruit shield with motor output on M3 and M4
 * pushbuttons  are attached to digital pins 14,15,16,17,qnd 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 2 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

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
const int Pin2 =  2;     // PIN OUTPUT NUMBER

AF_Stepper motor(200, 2);

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);
  pinMode (Pin2,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()

{
  // delay 10 milliseconds before the next reading:
  delay(10);

  // read the state of the Park pushbutton value:
  buttonStatePark = digitalRead(buttonPinPark);    //code for Park button
  Serial.print("Park = "); 
  Serial.println(buttonStatePark, DEC);  // print as an ASCII-encoded decimal:

 
     // delay 10 milliseconds before the next reading:
  delay(10);

  // read the state of the Home pushbutton value:
  buttonStateHome = digitalRead(buttonPinHome);    //code for Park button
  Serial.print("Home = "); 
  Serial.println(buttonStateHome, DEC);  // print as an ASCII-encoded decimal:


  // 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);
   
    while (buttonStateHome != HIGH)
    // step until the home limit switch is detected
    {
    motor.step(20, FORWARD, SINGLE);
   
    // read the input pin
    digitalRead(buttonStateHome);   
    }
  }
  else
  {
    // Set Pin9 Off:
    digitalWrite(Pin9, LOW);
  }

  {
    // delay 10 milliseconds before the next reading:
    delay(10);

    // read the state of the Rev pushbutton value: 
    buttonStateRev = digitalRead(buttonPinRev);    //code for Rev button
    Serial.print("Rev = "); 
    Serial.println(buttonStateRev, DEC);  // print as an ASCII-encoded decimal:

    // 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, BACKWARD, SINGLE);

    }
    else
    {
      // Set Pin10 Off:
      digitalWrite(Pin10, LOW);
    }
    {
      // delay 10 milliseconds before the next reading:
      delay(10);

      // read the state of the Ntrl pushbutton value:
      buttonStateNtrl = digitalRead(buttonPinNtrl);    //code for Ntrl button
      Serial.print("Ntrl = "); 
      Serial.println(buttonStateNtrl, DEC);  // print as an ASCII-encoded decimal:
      // 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(60, FORWARD, SINGLE);

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

      {
        // delay 10 milliseconds before the next reading:
        delay(10);

        // read the state of the Rev pushbutton value: 
        buttonStateDr = digitalRead(buttonPinDr);    //code for Dr button
        Serial.print("Dr = "); 
        Serial.println(buttonStateDr, DEC);  // print as an ASCII-encoded decimal:

        // 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(80, FORWARD, SINGLE);

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

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

Re: Setting a Home position for a stepper motor

by adafruit_support_bill on Fri Aug 19, 2011 1:49 pm

Then you probably still have a wiring problem.

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

Please be positive and constructive with your questions and comments.