Getting motor to alternate direction

Hello,

I recently built a motor controlled lathe using the sabertooth 2x25 and the packetized serial method. I just got a request from someone who is using the lathe tomorrow to have the lathe traverse back and forth automatically. I built the lathe with an overtravel switch on each end of the bed, so all of the hardware is in place. Basically all I need the lathe to do is go the entire length in one direction until switch is activated and then reverse course in the other direction until switch is activated and repeat. Would anyone be able to get me jumpstarted on the correct path to facilitate this request.

Rod M.

//*********** DEADMAN SWITCH MUST BE PRESSED IN, MOTORS WILL STOP IF RELEASED **********************
//ONLY THING YOU NEED IS DEAD MAN SWITCH TO DIGITAL PIN 9 (Ground TO PIN 9 WHEN SWITCH PRESSED)
//J. Dingley  March 1 2011 For Arduino 328 Duemalinove or similar with a 3.3V accessory power output
//i.e. the current standard Arduino board.
//Modified by R. Morgan 2/4/12

#include <SoftwareSerial.h>

//******************** IMPORTANT ***************************
//Set dip switches on the Sabertooth for simplified serial and 9600 Buadrate. 
//i.e. DIP switches 1, 3, 5 and 6 set to ON, the remainder set to OFF
//******************** IMPORTANT ***************************

#define SABER_TX_PIN  13  //Digital pin 13 is serial transmit pin to sabertooth
#define SABER_RX_PIN  12  //Not used but still initialised, Digital pin 12 is serial receive from Sabertooth
#define SABER_BAUDRATE  9600  //set baudrate to match sabertooth dip settings
#define SABER_MOTOR1_FULL_FORWARD 127  //simplifierd serial limits for each motor
#define SABER_MOTOR1_FULL_REVERSE 1
#define SABER_MOTOR2_FULL_FORWARD 255
#define SABER_MOTOR2_FULL_REVERSE 128
#define SABER_ALL_STOP  0  //motor level to send when issuing full stop command
SoftwareSerial SaberSerial = SoftwareSerial (SABER_RX_PIN, SABER_TX_PIN );  //initialise software to communicate with sabertooth

void initSabertooth (void)  
{
  pinMode ( SABER_TX_PIN, OUTPUT );
  SaberSerial.begin( SABER_BAUDRATE );
  delay (2000);  //2 sec delay to let it settle
  SaberSerial.print(0, BYTE);   //kill motors when first switched on
  delay (1000);
}

float level = 0;
int k4;
int deadmanbuttonPin = 9;  // deadman button is digital input pin 9
const int ledPin = 3;
int ledState = LOW;
long previousMillis = 0;
long interval = 250;
int rc_switch = 5;
int rc;
int sensorPin1 = A0;  //sets up A0 as the potentiometer input
int sensorPin2 = A1;  //sets up potentiometer on A1
int sensorValue_long = 0;
int sensorValue_lat = 0;
int right_traverse_limit = 12;
int right;
int left_traverse_limit = 11;
int left;
int a;
int b;
unsigned long transmitterchannel1=0;
unsigned long transmitterchannel2=0;
int ch1;
int ch2;



void setup()                    // run once, when the sketch starts
{
  initSabertooth( );  //runs sabertooth initialization 

  pinMode(deadmanbuttonPin, INPUT);  //set inputs
  pinMode (ledPin, OUTPUT);  //sets up pin 3 for indicator light
  pinMode (rc_switch, INPUT);  //sets up pin 5 for rc/hardwired selector
  pinMode(right_traverse_limit, INPUT);  //sets overtravel switch inputs
  pinMode(left_traverse_limit, INPUT);  //sets overtravel switch inputs
  pinMode (transmitterchannel1, INPUT);  //sets up pin for RC input
  pinMode (transmitterchannel2, INPUT);  //sets up pin for RC input
  digitalWrite(deadmanbuttonPin, 1); //activates internal resistors
  digitalWrite(right_traverse_limit, 1); //activates internal resistors
  digitalWrite(left_traverse_limit, 1);  //activates internal resistors
  digitalWrite(rc_switch, 1);  //activates internal resistors 
  Serial.begin(9600);      // HARD wired Serial feedback to PC for debugging in Wiring


}

void checkTransmitter()
{
  transmitterchannel1 = (pulseIn (7, HIGH, 100000))/10; //read RC channel, wait max of 0.1 seconds for pulse
  transmitterchannel2 = (pulseIn (8, HIGH, 100000))/10; //read RC channel, wait max of 0.1 seconds for pulse
  ch2 = map(transmitterchannel2, 106, 193, 1, 127);  //converts value to number used by controller
  ch1 = map(transmitterchannel1, 106, 193, 128, 255);  //converts value to number used by controller
  if (ch2 < 1 || ch2 > 127) ch2 = 0;
  if (ch1 < 128 || ch1 > 255) ch1 = 0;
}

void transmitterread()

{
  checkTransmitter();//check the data being received by the transmitter.
  Serial.print ("ch1 data: ");
  Serial.println (ch1);
  Serial.print ("ch2 data:");
  Serial.println (ch2);

}

void auto()  //this will be the loop that automatically changes direction of the motor depending on the traverse switches

{

void set_motor()   

{
  unsigned char cSpeedVal_Motor1 = 0;
  unsigned char cSpeedVal_Motor2 = 0;  //set motors using the simplified serial Sabertooth protocol  
  Serial.println ("#########################");
  cSpeedVal_Motor1 = sensorValue_long;
  cSpeedVal_Motor2 = sensorValue_lat;
  Serial.println("**********");
  Serial.println(cSpeedVal_Motor1);
  Serial.println(cSpeedVal_Motor2);
  Serial.println("**********");
  SaberSerial.print (cSpeedVal_Motor1, BYTE);
  SaberSerial.print (cSpeedVal_Motor2, BYTE);

}

void set_motor_rc()   

{
  unsigned char ch1 = 0;
  unsigned char ch2 = 0;  //set motors using the simplified serial Sabertooth protocol  
  unsigned char cSpeedVal_Motor1 = 0;
  unsigned char cSpeedVal_Motor2 = 0;
  Serial.println ("#########################");
  cSpeedVal_Motor1 = ch1;
  cSpeedVal_Motor2 = ch2;
  SaberSerial.print (cSpeedVal_Motor1, BYTE);
  SaberSerial.print (cSpeedVal_Motor2, BYTE);
  Serial.println("**********");
  Serial.println(cSpeedVal_Motor1);
  Serial.println(cSpeedVal_Motor2);
  Serial.println("**********");

}

void loop ()   

{

  unsigned char cSpeedVal_Motor1 = 0;
  unsigned char cSpeedVal_Motor2 = 0;

  int val = 0;
  for (int i=0; i< 16; i++) val += analogRead(sensorPin1);  //averages values 
  sensorValue_long = val/16;

  val = 0;
  for (int i=0; i< 16; i++) val += analogRead(sensorPin2); //averages values
  sensorValue_lat= val/16;

  int v = sensorValue_long - 512 ;  // expands center pot "dead" spot
  if (v < -50)     v += 50 ;
  else if (v > 50) v -= 50 ;
  else             v = 0 ;

  int w = sensorValue_lat - 512 ;  // expands center pot "dead" spot
  if (w < -50)     w += 50 ;
  else if (w > 50) w -= 50 ;
  else             w = 0 ;

  sensorValue_long = map(v, -462, 461, 1, 127);  //converts value to number used by controller
  sensorValue_lat = map(w, -462, 461, 128, 255);  //converts value to number used by controller

  digitalWrite (3, LOW);
  Serial.print("Long Data: ");
  Serial.println(sensorValue_long);
  Serial.print("Lat Data: ");
  Serial.println(sensorValue_lat);
  //delay (200);
  k4 = digitalRead(deadmanbuttonPin); 
  right = digitalRead(right_traverse_limit); 
  left = digitalRead(left_traverse_limit);  
  if (rc > 1)auto();
  while (k4 > 0) 
  { 
    cSpeedVal_Motor1 = 0;
    cSpeedVal_Motor2 = 0;
    SaberSerial.print (cSpeedVal_Motor1, BYTE);
    SaberSerial.print (cSpeedVal_Motor2, BYTE);
    Serial.println("Switch is OFF");
    k4 = digitalRead(deadmanbuttonPin);


    unsigned long currentMillis = millis();
    if(currentMillis - previousMillis > interval) {  // save the last time you blinked the LED 
      previousMillis = currentMillis;   // if the LED is off turn it on and vice-versa:
      if (ledState == LOW)
        ledState = HIGH;
      else
        ledState = LOW;
      digitalWrite(ledPin, ledState);  // set the LED with the ledState of the variable:
    }

  } 


  while  (right < 1)  //reverses motor for 750 ms after right traverse limit switch is engaged
  { 
    cSpeedVal_Motor1 = 0;
    SaberSerial.print (cSpeedVal_Motor1, BYTE);
    delay(500);
    cSpeedVal_Motor1 = 50;
    SaberSerial.print (cSpeedVal_Motor1, BYTE);
    Serial.println("Right limit reached");
    delay (750);
    right = digitalRead(right_traverse_limit);

  }


  while  (left < 1) 
  {

    cSpeedVal_Motor1 = 0;
    SaberSerial.print (cSpeedVal_Motor1, BYTE);
    delay(500);
    cSpeedVal_Motor1 = 84;
    SaberSerial.print (cSpeedVal_Motor1, BYTE);
    Serial.println("Left limit reached");
    delay (750);
    left = digitalRead(left_traverse_limit);
  }

  //if (rc > 1) { 
  // set_motor_analog();
  //}

  // rc = digitalRead (rc_switch);  //includes transmitterread() loop if switch is acitve  --saves processing--
  // if (rc < 1) {
  // transmitterread();
  //set_motor_rc();
  //Serial.println ("rc is active");

  //}

  unsigned long currentMillis = millis();
  if(currentMillis - previousMillis > interval) {  // save the last time you blinked the LED 
    previousMillis = currentMillis;   // if the LED is off turn it on and vice-versa:
    if (ledState == LOW)
      ledState = HIGH;
    else
      ledState = LOW;
    digitalWrite(ledPin, ledState);  // set the LED with the ledState of the variable:



  }

  set_motor();
}

Can you post your existing code and details of your wiring?

Simple limit switch setup you probably could use with your current setup.

Added the code up above to the original message. Is there a command that will register a state change in a button and if so how would it be worked into the program?

Is there a command that will register a state change in a button

The digitalRead() function will detect pressed or released.

 and if so how would it be worked into the program?

You're kidding, right?