//PHONE CONTROLS OBJECT //Asterisk Connects to Arduino via Xport //based on code from Kate Hartman: http://itp.nyu.edu/~kh928/phonesandobjects.html /* Connections: Xport TX --> Arduino RX Xport RX --> Arduino TX Xport Reset --> Arduino pin 7 */ #define disconnected 0 #define connected 1 // I/O pins #define connectedLED 2 //indicates when there's a TCP connection #define gotdataLED 3 //indicates that data has come in #define oneLED 5 #define twoLED 6 #define deviceResetPin 7 //resets Xport #define debug 13 // SERVOS-------------------------------------- #define servoPin 9 // Control pin for servo motor int minPulse = 500; // Minimum servo position int maxPulse = 2500; // Maximum servo position int pulse = 0; // Amount to pulse the servo long lastPulse = 0; // the time in milliseconds of the last pulse int refreshTime = 20; // the time needed in between pulses //variables: char inByte; //incoming byte from serial RX int status = 0; //Xport connection status void setup() { //set all status LED pins and Xport reset pin pinMode(debug, OUTPUT); pinMode(connectedLED, OUTPUT); pinMode(gotdataLED,OUTPUT); pinMode(deviceResetPin,OUTPUT); pinMode(oneLED,OUTPUT); pinMode(twoLED,OUTPUT); pinMode(servoPin,OUTPUT); pulse = minPulse; //set servo position to minimum digitalWrite(connectedLED,LOW); Serial.begin(9600); //start serial port 9600 8-N-1 digitalWrite(twoLED,LOW); resetDevice(); //reset Xport } void loop() { digitalWrite(debug,LOW); stateCheck(); digitalWrite(debug,HIGH); if (millis() - lastPulse >= refreshTime) // pulse the servo again if the refresh time (20 ms) have passed { digitalWrite(servoPin, HIGH); // Turn the motor on delayMicroseconds(pulse); // Length of the pulse sets the motor position digitalWrite(servoPin, LOW); // Turn the motor off lastPulse = millis(); // Save the time of the last pulse } } //take the Xport's reset pin low to reset it void resetDevice() { digitalWrite(deviceResetPin,LOW); delay(50); digitalWrite(deviceResetPin,HIGH); delay(2000); //pause to let Xport boot up } void stateCheck() { switch (status) { case disconnected: digitalWrite(connectedLED,LOW); if(Serial.available()) { inByte = Serial.read(); Serial.print(inByte); if(inByte == 'I') { status = connected; digitalWrite(connectedLED,HIGH); } } break; case connected: if(Serial.available()) { inByte = Serial.read(); Serial.print((char)inByte); if(inByte == '1') { digitalWrite(oneLED,HIGH); digitalWrite(twoLED,LOW); pulse = minPulse; } else if(inByte == '2') { digitalWrite(twoLED,HIGH); digitalWrite(oneLED,LOW); pulse = maxPulse; } /* else if(inByte > 65) { digitalWrite(oneLED,HIGH); digitalWrite(twoLED,HIGH); } */ else { digitalWrite(oneLED,LOW); digitalWrite(twoLED,LOW); } } break; } }