/* Michael Kontopoulos 2009 Pacing Track - Reciever */ #include NewSoftSerial mySerial = NewSoftSerial(4, 5); int ledPin = 13; //h-bridge stuff int motor1Pin = 6; int motor2Pin = 7; int switchPin1 = 8; int switchPin2 = 9; int reading1, reading2; // the current reading from the input pin int previous1 = LOW; int previous2 = LOW; // the previous reading from the input pin long time1 = 0; // the last time the output pin was toggled long time2 = 0; long debounce = 200; // the debounce time, increase if the output flickers boolean motorRunning; int stepCounter1, stepCounter2; long motorStartTime=0; long thresh; long twelveFeet = 10000;//76800; long sixFeet = 4000;//38400; int lastButtonPressed = 0; void setup() { pinMode(switchPin1, INPUT); pinMode(switchPin2, INPUT); pinMode(motor1Pin, OUTPUT); pinMode(motor2Pin, OUTPUT); motorRunning=false; stepCounter1 = 0; stepCounter2 = 0; thresh = 0; Serial.begin(9600); mySerial.begin(9600); } void loop() { char val = (char)mySerial.read(); if(val == '8') { reading1 = HIGH; Serial.print("left!\t"); Serial.print(reading1); Serial.print("\t"); Serial.println(reading2); } else if(val == '9'){ reading2 = HIGH; Serial.print("right!\t"); Serial.print(reading1); Serial.print("\t"); Serial.println(reading2); } else{ reading1 = LOW; reading2 = LOW; } serialHandler(); //Motor check if(!motorRunning && stepCounter1 > 0) { startMotor(); stepCounter1--; } if(!motorRunning && stepCounter2 > 0) { startMotor(); stepCounter2--; } long diff = millis() - motorStartTime; if (motorRunning && diff > thresh) { stopMotor(); } //Register button pressing //Includes Debouncing if (reading1 == HIGH && previous1 == LOW && millis() - time1 > debounce) { if(lastButtonPressed == 2) { thresh = twelveFeet; } else if(lastButtonPressed == 1) { thresh = sixFeet; } else thresh = sixFeet; stepCounter1++; time1 = millis(); lastButtonPressed = 1; // reading1=LOW; } previous1 = reading1; if (reading2 == HIGH && previous2 == LOW && millis() - time2 > debounce) { if(lastButtonPressed == 1) { thresh = twelveFeet; } else if(lastButtonPressed == 2) { thresh = sixFeet; } else thresh = sixFeet; stepCounter2++; time2 = millis(); lastButtonPressed = 2; } previous2 = reading2; } // end main void startMotor() { determineMotorDirection(digitalRead(switchPin1), digitalRead(switchPin2)); digitalWrite(ledPin, HIGH); motorRunning=true; motorStartTime = millis(); } void stopMotor() { determineMotorDirection(LOW, LOW); digitalWrite(ledPin, LOW); motorRunning=false; } void serialHandler() { if (mySerial.available()) { Serial.print((char)mySerial.read()); } if (Serial.available()) { mySerial.print((char)Serial.read()); } delay(100); } void determineMotorDirection(int pinOne, int pinTwo) { // if the switch is high, motor will turn on one direction: if ( pinOne == HIGH && pinTwo== LOW){ digitalWrite(motor1Pin, LOW); // set leg 1 of the H-bridge low digitalWrite(motor2Pin, HIGH); // set leg 2 of the H-bridge high } // if the switch is low, motor will turn in the other direction: else if ( pinOne == LOW && pinTwo == HIGH ){ digitalWrite(motor1Pin, HIGH); // set leg 1 of the H-bridge high digitalWrite(motor2Pin, LOW); // set leg 2 of the H-bridge low } else { digitalWrite(motor1Pin, LOW); // set leg 1 of the H-bridge high digitalWrite(motor2Pin, LOW); // set leg 2 of the H-bridge low } }