int DistanceS1 = 0; // Record the number of steps we've taken
int DistanceS2 = 0; // Record the number of steps we've taken
boolean upS1 = true;
boolean upS2 = true;
#define ENABLES1 12
#define ENABLES2 13
void setup() {
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
pinMode(11, OUTPUT);
pinMode(ENABLES1, OUTPUT);
pinMode(ENABLES2, OUTPUT);
digitalWrite(8, LOW);
digitalWrite(9, LOW);
digitalWrite(10, LOW);
digitalWrite(11, LOW);
Serial.begin(9600);
}
void doS1JobUp(){
if(upS1){
digitalWrite(8, HIGH);
digitalWrite(9, HIGH);
delayMicroseconds(100);
digitalWrite(9, LOW);
delayMicroseconds(100);
DistanceS1 = DistanceS1 + 1; // record this step
// Check to see if we are at the end of our move
if (DistanceS1 == 3000){
DistanceS1 = 0;
upS1=false;
// Now pause for half a second
delay(500);
motorS1Off();
return;
}else{
doS1JobUp();
}
}else{
return;
}
}
void doS1JobDown(){
if(!upS1){
digitalWrite(8, LOW);
digitalWrite(9, HIGH);
delayMicroseconds(100);
digitalWrite(9, LOW);
delayMicroseconds(100);
DistanceS1 = DistanceS1 + 1; // record this step
// Check to see if we are at the end of our move
if (DistanceS1 == 3000){
DistanceS1 = 0;
upS1=true;
// Now pause for half a second
delay(500);
motorS1Off();
return;
}else{
doS1JobDown();
}
}else{
return;
}
}
void doS2JobUp(){
if(upS2){
digitalWrite(10, HIGH);
digitalWrite(11, HIGH);
delayMicroseconds(100);
digitalWrite(11, LOW);
delayMicroseconds(100);
DistanceS2 = DistanceS2 + 1; // record this step
// Check to see if we are at the end of our move
if (DistanceS2 == 2000){
DistanceS2 = 0;
upS2=false;
// Now pause for half a second
delay(500);
motorS2Off();
return;
}else{
doS2JobUp();
}
}else{
return;
}
}
void doS2JobDown(){
if(!upS2){
digitalWrite(10, LOW);
digitalWrite(11, HIGH);
delayMicroseconds(100);
digitalWrite(11, LOW);
delayMicroseconds(100);
DistanceS2 = DistanceS2 + 1; // record this step
// Check to see if we are at the end of our move
if (DistanceS2 == 2000){
DistanceS2 = 0;
upS2=true;
// Now pause for half a second
delay(500);
motorS2Off();
return;
}else{
doS2JobDown();
}
}else{
return;
}
}
void motorS1Off() {
digitalWrite(ENABLES1, HIGH);
}
void motorS1On() {
digitalWrite(ENABLES1, LOW);
}
void motorS2Off() {
digitalWrite(ENABLES2, HIGH);
}
void motorS2On() {
digitalWrite(ENABLES2, LOW);
}
void loop() {
if (Serial.available() > 0) {
int inByte = Serial.read();
switch (inByte) {
case '1':
motorS1On();
doS1JobUp();
motorS2On();
doS2JobUp();
break;
case '2':
motorS1On();
doS1JobDown();
motorS2On();
doS2JobDown();
break;
case '3':
motorS1On();
doS1JobUp();
motorS2On();
doS2JobUp();
motorS1On();
doS1JobDown();
motorS2On();
doS2JobDown();
break;
}
}
}