/* V.Chan for interface for Sky Safari * Free to modify and share for non-commercial use * encoder0 is azimuth * encoder1 is altitude * communication is "simple encoder" * 9600 baud * Aug 16th, 2011 * Jul 9th, 2012 modified to use interrupts * * encoder library from PJRC electronic projects * www.pjrc.com/teensy/td_libs_Encoder.html */ /* * Arduino based Digital Setting Circle * Michael Fulbright 2010-06-06 * * Feel free to modify and share - please let me know if you make any changes so I can keep my version up to date * 2010-06-06: Initial relase - supports 2X encoding */ //load in the encoder library /library/Encoder/ #define ENCODER_OPTIMIZE_INTERRUPTS #include //set the encoder pins Encoder encoder0(2, 4); Encoder encoder1(3, 5); int LED = 13; int inByte; long encoder0Pos = 0; long encoder1Pos = 0; long newencoder0, newencoder1; void setup() { //setup for builtin LED pinMode (13, OUTPUT); //setup for serial to sky safari interface Serial.begin (9600); } void loop() { //read encoders newencoder0 = encoder0.read(); newencoder1 = encoder1.read(); if (newencoder0 != encoder0Pos || newencoder1 != encoder1Pos) { encoder0Pos = newencoder0; encoder1Pos = newencoder1; } //communicate with sky safari // if we get a valid byte, read analog ins: if (Serial.available() > 0) { // get incoming byte: inByte = Serial.read(); if (inByte == 81) //ascii for "Q" { digitalWrite(13, HIGH); // set the LED on //send out the encoder positions printEncoderValue(encoder0Pos); Serial.print("\t"); printEncoderValue(encoder1Pos); Serial.println("\r"); digitalWrite(13, LOW); // set the LED off } } } // print encoder value with leading zeros void printEncoderValue(long val) { unsigned long aval; if (val < 0) Serial.print("-"); else Serial.print("+"); aval = abs(val); if (aval < 10) Serial.print("0000"); else if (aval < 100) Serial.print("000"); else if (aval < 1000) Serial.print("00"); else if (aval < 10000) Serial.print("0"); Serial.print(aval); }