//Created August 23 2006 //Heather Dewey-Hagborg //http://www.arduino.cc #include #define bit9600Delay 100 #define halfBit9600Delay 50 #define bit4800Delay 188 #define halfBit4800Delay 94 byte tx =7; byte rx =8; byte SWval; volatile boolean moveFlag=false; void setup() {// Setup harware intterupt on pin2 attachInterrupt(0, translatorISR, RISING); Serial.begin(9600); pinMode(rx,INPUT); pinMode(tx,OUTPUT); digitalWrite(tx,HIGH); //digitalWrite(13,HIGH); //turn on debugging LED //SWprint('h'); //debugging hello //SWprint('i'); //SWprint(10); //carriage return } void SWprint(int data) { byte mask; Serial.println(data); //startbit digitalWrite(tx,LOW); delayMicroseconds(bit9600Delay); for (mask = 0x01; mask>0; mask <<= 1) { if (data & mask){ // choose bit digitalWrite(tx,HIGH); // send 1 } else{ digitalWrite(tx,LOW); // send 0 } delayMicroseconds(bit9600Delay); } //stop bit digitalWrite(tx, HIGH); delayMicroseconds(bit9600Delay); } int SWprintarray(int *array,int asize) { byte mask; int i; int data; for (i=0;i0; mask <<= 1) { if (data & mask){ // choose bit digitalWrite(tx,HIGH); // send 1 } else{ digitalWrite(tx,LOW); // send 0 } delayMicroseconds(bit9600Delay); } //stop bit digitalWrite(tx, HIGH); delayMicroseconds(bit9600Delay); } } int SWread() { byte val = 0; while (digitalRead(rx)); //wait for start bit if (digitalRead(rx) == LOW) { delayMicroseconds(halfBit9600Delay); for (int offset = 0; offset < 8; offset++) { delayMicroseconds(bit9600Delay); val |= digitalRead(rx) << offset; } //wait for stop bit + extra delayMicroseconds(bit9600Delay); delayMicroseconds(bit9600Delay); return val; } } void loop() { //SWval = SWread(); //SWprint(toupper(SWval)); // Serial.(SWval); char incomingByte; int command[64]; ////////////////////////////////////////////////// // The next part is the part that moves the stage ////////////////////////////////////////////////// if (moveFlag){ //command[0]='H'; // command[1]=':'; // command[2]='1'; // command[3]='+'; command[0]='M'; command[1]=':'; command[2]='1'; command[3]='+'; command[4]='P'; command[5]='9'; command[6]='0'; command[7]='0'; command[8]='0'; command[9]=13; command[10]=10; command[11]='G'; command[12]=13; command[13]=10; SWprintarray(command,14); delay(6000); command[0]='H'; command[1]=':'; command[2]='1'; command[3]='+'; command[4]=13; command[5]=10; SWprintarray(command,6); moveFlag=false; } ///////////////////////////////////////////////// // End of movement stage ///////////////////////////////////////////////// if (Serial.available() > 0) { // read the incoming byte: incomingByte = Serial.read(); // say what you got: Serial.print("I received: "); Serial.println(incomingByte, DEC); if(incomingByte==107){ // SWprint('H'); // SWprint(':'); // SWprint('1'); // SWprint('+'); // SWprint(13); // SWprint(10); // SWprint('M'); // SWprint(':'); // SWprint('1'); // SWprint('+'); // SWprint('P'); // SWprint('9'); // SWprint('0'); // SWprint('0'); // SWprint('0'); // SWprint(13); // SWprint(10); // SWprint('G'); // SWprint(13); // SWprint(10); command[0]='H'; command[1]=':'; command[2]='1'; command[3]='+'; command[4]=13; command[5]=10; SWprintarray(command,6); //SWprint(13); //SWprint(10); } } } void translatorISR(){ moveFlag = true; }