Here is the codes for the motor timer and location switch. I am using a motor adafruit motor shield and the limit switch on pin 2. I am just using a simple timer as a test at this stage.
/*
Limit swith and stepping motor code StepperTestrev4
*/
#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_PWMServoDriver.h"
// Create the motor shield object with the default I2C address
Adafruit_MotorShield AFMS = Adafruit_MotorShield();
// A stepper motor with 200 steps per revolution (1.8 degree)
// connected to motor port #2 (M3 and M4)
Adafruit_StepperMotor *myMotor = AFMS.getStepper(200, 2);
const int SWITCH=2; //The Button is connected to pin 2
boolean timerstate; //timer state variable
boolean check; //timer state variable
int SwitchState = 0;
int LastSwitchState = 0;
unsigned long StartTime;
void setup()
{
pinMode (SWITCH, INPUT); //Set button as input (not required)
Serial.begin(9600); // set up Serial library at 9600 bps
Serial.println("Stepper test!");
AFMS.begin(); // create with the default frequency 1.6KHz
//AFMS.begin(1000); // OR with a different frequency, say 1KHz
myMotor->setSpeed(10); // 10 rpm
}
void loop() {
timersub(); // check timer
while (timerstate == true) //could also be if loop
{
switchsub(); //check status of limit switch
}
}
int switchsub() {
SwitchState = digitalRead(SWITCH); // 0 is low (run) and 1 is high (stop) this line is not used
if (digitalRead(SWITCH) == LOW)
{
Serial.println(SwitchState);
myMotor->step(1, FORWARD, DOUBLE); //if switch is not triggered run motor
}
else
{
Serial.println("STOP");
Serial.println(SwitchState);
delay(500);
myMotor->release(); // release motor - stop if switch is triggered
timerstate=false; // reset timer
}
return timerstate;
}
int timersub() {
StartTime = millis()/1000; // simple timer test to start motor at two times - just placeholder
Serial.print("Time ");
Serial.println(StartTime);
if ((StartTime == 5) || (StartTime == 30)) {
timerstate = true;
}
return timerstate;
}