1 // ST4 2 // 3 // Arduino ST4 is free software: you can redistribute it and/or modify 4 // it under the terms of the GNU Lesser General Public License as published by 5 // the Free Software Foundation, either version 3 of the License, or 6 // (at your option) any later version. 7 // 8 // Arduino ST4 is distributed in the hope that it will be useful, 9 // but WITHOUT ANY WARRANTY; without even the implied warranty of 10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 // GNU Lesser General Public License for more details. 12 // 13 // You should have received a copy of the GNU Lesser General Public License 14 // along with Arduino ST4. If not, see . 15 // 16 // Copyright Kevin Ferrare 2014 17 18 /* 19 * Led indicates whether the software requested connection, it is shut off when the software disconnects 20 */ 21 const int pinLED = 13; 22 23 /** 24 * An axis has a pin per direction. 25 * Both pins cannot be up at the same time. 26 */ 27 class Axis { 28 private: 29 int plusPin; 30 int minusPin; 31 public: 32 Axis(int plusPin, int minusPin) : 33 plusPin(plusPin), minusPin(minusPin) { 34 } 35 void setupPins(){ 36 pinMode(this->plusPin, OUTPUT); 37 pinMode(this->minusPin, OUTPUT); 38 } 39 void plus(){ 40 digitalWrite(this->minusPin, LOW); 41 digitalWrite(this->plusPin, HIGH); 42 } 43 void minus(){ 44 digitalWrite(this->plusPin, LOW); 45 digitalWrite(this->minusPin, HIGH); 46 } 47 void reset(){ 48 digitalWrite(this->minusPin, LOW); 49 digitalWrite(this->plusPin, LOW); 50 } 51 }; 52 53 class Axis rightAscension( 54 2,//RA+ pin 55 5);//RA- pin 56 class Axis declination( 57 3,//DEC+ pin 58 4);//DEC- pin 59 60 void setup() 61 { 62 rightAscension.setupPins(); 63 declination.setupPins(); 64 pinMode(pinLED, OUTPUT); 65 //57.6k, 8 data bits, no parity, one stop bit. 66 Serial.begin(57600, SERIAL_8N1); 67 //Wait for serial port to connect. Needed for Leonardo only 68 while (!Serial); 69 Serial.println("INITIALIZED#"); 70 } 71 72 void resetPins(){ 73 rightAscension.reset(); 74 declination.reset(); 75 } 76 77 void loop() 78 { 79 if (Serial.available() > 0) { 80 //Received something 81 String opcode = Serial.readStringUntil('#'); 82 boolean validOpcode=true; 83 //Parse opcode 84 if(opcode=="CONNECT"){ 85 digitalWrite(pinLED, HIGH); 86 resetPins(); 87 } 88 else if (opcode=="DISCONNECT"){ 89 digitalWrite(pinLED, LOW); 90 resetPins(); 91 } 92 else if(opcode=="RA0"){ 93 rightAscension.reset(); 94 } 95 else if(opcode=="RA+"){ 96 rightAscension.plus(); 97 } 98 else if(opcode=="RA-"){ 99 rightAscension.minus(); 100 } 101 else if(opcode=="DEC0"){ 102 declination.reset(); 103 } 104 else if(opcode=="DEC+"){ 105 declination.plus(); 106 } 107 else if(opcode=="DEC-"){ 108 declination.minus(); 109 } 110 else{ 111 validOpcode=false; 112 } 113 if(validOpcode){ 114 //Acknowledge valid command 115 Serial.println("OK#"); 116 } 117 } 118 }