/* Ted Ullrich tullrich@gmail.com tedullrich.com Georgia Intitute of Technology College of Architecture Ñ Industrial Design Program Final Project for Masters of Industrial Design Spring 2009 Listens to the USB port for alpha letters sent from the email.rb (Ruby) script. Program to drive a stepper motor in two directions. Send 'a' thru 't' to send the tube up or down. a is worth 5%, b is worth 10%, c is worth 15% ... t is worth 100% send 'u' in front of the letter to flip direction of travel. */ // Fading LED values int value = 0; // variable to keep the actual value int ledpin = 9; // light connected to digital pin 9 //motor values int motorPin1 = 2; int motorPin2 = 3; int motorPin3 = 4; int motorPin4 = 5; int delayTime = 10; // milliseconds between each step of the motor - don't change int val = 0; // variable to store the data from the serial port - don't change int count = 1; int totalrevs = 1600; //number of turns of the motor axle to go from 0 to 100. int resolution = 20; //the number of finite positions available in the hose. int timer = (totalrevs/resolution); // length of one knotch in the hose extension, right now set to 5% int loopqty = 1; //variable to be changed by the arduino int godirection = 0; //variable to be changed by the arduino void setup() { pinMode(motorPin1, OUTPUT); pinMode(motorPin2, OUTPUT); pinMode(motorPin3, OUTPUT); pinMode(motorPin4, OUTPUT); Serial.begin(9600); } void loop () { val = Serial.read(); // read the serial port if (val == 97) /*value is a*/ { loopqty = 1 ;} if (val == 98) /*value is b*/ { loopqty = 2 ;} if (val == 99) /*value is c*/ { loopqty = 3 ;} if (val == 100) /*value is d*/ { loopqty = 4 ;} if (val == 101) /*value is e*/ { loopqty = 5 ;} if (val == 102) /*value is f*/ { loopqty = 6 ;} if (val == 103) /*value is g*/ { loopqty = 7 ;} if (val == 104) /*value is h*/ { loopqty = 8 ;} if (val == 105) /*value is i*/ { loopqty = 9 ;} if (val == 106) /*value is j*/ { loopqty = 10 ;} if (val == 107) /*value is k*/ { loopqty = 11;} if (val == 108) /*value is l*/ { loopqty = 12 ;} if (val == 109) /*value is m*/ { loopqty = 13 ;} if (val == 110) /*value is n*/ { loopqty = 14 ;} if (val == 111) /*value is o*/ { loopqty = 15 ;} if (val == 112) /*value is p*/ { loopqty = 16 ;} if (val == 113) /*value is q*/ { loopqty = 17 ;} if (val == 114) /*value is r*/ { loopqty = 18 ;} if (val == 115) /*value is s*/ { loopqty = 19 ;} if (val == 116) /*value is t*/ { loopqty = 20 ;} if (val == 117) /* u has been received, flip directions just for that move*/ { godirection = 1; } // begin the loop if ( val <= 116 && val >= 97 ) { /*if a thru t is received*/ /*move up*/ if (godirection == 1) { count = 1; while (count <= (loopqty*timer)) { digitalWrite(motorPin1, HIGH); digitalWrite(motorPin2, HIGH); digitalWrite(motorPin3, LOW); digitalWrite(motorPin4, LOW); delay(delayTime); digitalWrite(motorPin1, HIGH); digitalWrite(motorPin2, LOW); digitalWrite(motorPin3, LOW); digitalWrite(motorPin4, HIGH); delay(delayTime); digitalWrite(motorPin1, LOW); digitalWrite(motorPin2, LOW); digitalWrite(motorPin3, HIGH); digitalWrite(motorPin4, HIGH); delay(delayTime); digitalWrite(motorPin1, LOW); digitalWrite(motorPin2, HIGH); digitalWrite(motorPin3, HIGH); digitalWrite(motorPin4, LOW); delay(delayTime); count += 1; // Notice this statement } } /*move down, turn on light*/ if (godirection == 0) { for(value = 0 ; value <= 255; value+=5) // fade in (from min to max) { analogWrite(ledpin, value); // sets the value (range from 0 to 255) delay(30); // waits for 30 milli seconds to see the dimming effect } count = 1; while (count <= (loopqty*timer)) { digitalWrite(motorPin1, HIGH); digitalWrite(motorPin2, HIGH); digitalWrite(motorPin3, LOW); digitalWrite(motorPin4, LOW); delay(delayTime); digitalWrite(motorPin1, LOW); digitalWrite(motorPin2, HIGH); digitalWrite(motorPin3, HIGH); digitalWrite(motorPin4, LOW); delay(delayTime); digitalWrite(motorPin1, LOW); digitalWrite(motorPin2, LOW); digitalWrite(motorPin3, HIGH); digitalWrite(motorPin4, HIGH); delay(delayTime); digitalWrite(motorPin1, HIGH); digitalWrite(motorPin2, LOW); digitalWrite(motorPin3, LOW); digitalWrite(motorPin4, HIGH); delay(delayTime); count += 1; } for(value = 255; value >=0; value-=5) // fade out (from max to min) { analogWrite(ledpin, value); delay(30); } } godirection = 0; //reset go direction } // close if a thru t is received Serial.println(val); delay(1000); } // close the loop