// Author: Andrew Hutcheon // Email: andy@hutchyweb.co.uk // Web: www.hutchyweb.co.uk/astro // Description: This code is for my home made focuser see my BLOG for more info // Date: 18/06/2014 // include the library code: #include int keyboardPin = 6; // Analog input pin that the keypad is attached to int keyboardValue = 0; // value read from the keyboard // initialize the library with the numbers of the interface pins LiquidCrystal lcd(11, 13, 5, 4, 3, 2); // Define variables int nBig = 25; // Size of a big step int fPin = 19; // L293D Chip Forward pin int rPin = 18; // L293D Chip Reverse pin int fBut = 15; // 1 step forward button pin int ffBut = 16; // Big step forward button int rBut = 14; // 1 step backward button pin int rrBut = 17; // Big step backward butto int cPos = 0; // Current position int sSize = 10; // Step sizes int pStep = 1; // Pause Between Steps int maxPos = 2250; // Set max position int minPos = 0; // Set minimum position int f = 0; // Variable for steps forward int ff = 0; // Variable for steps fast forward (big step) int r = 0; // Variable for steps backward int rr = 0; // Variable for steps fast backward (big step) int nKeys; // Stor keys pressed int mKeys; // Keys to move (all presses) int cmd = 0; // Serial command int mCmd; // Serial move int DoMove; // Move triger int iSteps; // Move steps char sKeys[6]; // Array for key presses int i = 0; // Counter int mi = 0; // Move counter String sPos = "Cur Pos: "; // LCD string String KeyPress; // Key Presses for LCD dispay String KeyEnd; // Keys place holder for LCD String nPos; // Position number String sEnd = "#"; // Serial End String FIN; // Final Serial Send char key; // Key press char keypressed; // Keypad keypress void setup() { // set up the LCD's number of columns and rows: lcd.begin(16, 2); // Print a message to the LCD. lcd.print(" Focuser "); lcd.setCursor(0, 1); lcd.print(" Loading... "); lcd.noAutoscroll(); // Setup Pins pinMode(fBut, INPUT); pinMode(ffBut, INPUT); pinMode(rBut, INPUT); pinMode(rrBut, INPUT); pinMode(fPin, OUTPUT); pinMode(rPin, OUTPUT); // Setup Derial Serial.begin(57600); Serial.flush(); // Reset Pins digitalWrite(fPin, LOW); digitalWrite(rPin, LOW); //delay(2000); lcd.setCursor(0, 0); lcd.print("Focuser: Ready "); lcd.setCursor(0, 1); lcd.print(sPos + "0"); } void loop() { //Key collection in here keyboardValue = analogRead(keyboardPin); // read the keyboard value (0 - 1023) delay(50); if (keyboardValue > 25) { readkeyboard(keyboardValue); //get the value of key being pressed "keypressed" i.e. 0-9 key = keypressed; } else { key = NULL; } if (key) { // Check for a valid key. switch (key) { case '*': //MoveFocuser(-cPos); DoMove = 1; iSteps = -cPos; nKeys = 0; i = 0; memset(sKeys,'\0',6); break; case '#': if(nKeys > cPos) { mKeys = nKeys - cPos; } if (nKeys < cPos) { mKeys = nKeys - cPos; } if (nKeys == cPos) { break; } //MoveFocuser(mKeys); DoMove = 1; iSteps = mKeys; nKeys = 0; i = 0; memset(sKeys,'\0',6); break; default: sKeys[i] = key; i = i + 1; nKeys = atoi(sKeys); lcd.setCursor(0, 0); lcd.print("Focuser: " + nKeys); } } // Read the pushbutton input pin: f = digitalRead(fBut); ff = digitalRead(ffBut); r = digitalRead(rBut); rr = digitalRead(rrBut); if (f == 1) { DoMove = 1; iSteps = 1; delay(300); } if (ff == 1) { DoMove = 1; iSteps = nBig; } if (r == 1) { DoMove = 1; iSteps = -1; delay(300); } if (rr == 1) { DoMove = 1; iSteps = -nBig; } // Read serial input if (Serial.available() >0) { lcd.setCursor(0, 0); cmd = Serial.parseInt(); if (cmd == 9999) { // This is the GET position call FIN = cPos + sEnd; Serial.println(FIN); cmd = 0; } else if (cmd >0){ mCmd = cmd - cPos; DoMove = 1; iSteps = mCmd; cmd = 0; } } else { // Write keypress to LCD if (i > 0) { KeyPress = "Focuser: "; KeyEnd = " "; lcd.setCursor(0, 0); lcd.print(KeyPress + nKeys + KeyEnd); nPos = sPos + cPos + " "; lcd.setCursor(0, 1); lcd.print(nPos); } else { lcd.setCursor(0, 0); lcd.print("Focuser: Ready "); nPos = sPos + cPos + " "; lcd.setCursor(0, 1); lcd.print(nPos); } } //New Move Focuser if (DoMove == 1) { //do stepper motor stuff lcd.setCursor(0, 0); lcd.print("Focuser: Slewing"); if (iSteps > 0) { if ((mi != iSteps) && (cPos != maxPos)) { mi = mi + 1; cPos = cPos + 1; digitalWrite(fPin, HIGH); // This LOW to HIGH change is what creates the delay(sSize); // This delay time between steps digitalWrite(fPin, LOW); // "Rising Edge" so the easydriver knows to when to step. delay(pStep); // This delay time between steps nPos = sPos + cPos + " "; lcd.setCursor(0, 1); lcd.print(nPos); } else { DoMove = 0; mi = 0; FIN = cPos + sEnd; Serial.println(FIN); } } else { if ((mi != iSteps) && (cPos != minPos)) { mi = mi - 1; cPos = cPos - 1; digitalWrite(rPin, HIGH); // This LOW to HIGH change is what creates the delay(sSize); // This delay time between steps digitalWrite(rPin, LOW); // "Rising Edge" so the easydriver knows to when to step. delay(pStep); // This delay time between steps nPos = sPos + cPos + " "; lcd.setCursor(0, 1); lcd.print(nPos); } else { DoMove = 0; mi = 0; FIN = cPos + sEnd; Serial.println(FIN); } } } } //read the keyboard routine void readkeyboard(int KeyVal){ if ((KeyVal >25) && (KeyVal < 67)){keypressed = '1';} if ((KeyVal >67) && (KeyVal < 108)){keypressed = '2';} if ((KeyVal >108) && (KeyVal < 162)){keypressed = '3';} if ((KeyVal >162) && (KeyVal < 253)){keypressed = '4';} if ((KeyVal >253) && (KeyVal < 361)){keypressed = '5';} if ((KeyVal >361) && (KeyVal < 479)){keypressed = '6';} if ((KeyVal >479) && (KeyVal < 619)){keypressed = '7';} if ((KeyVal >619) && (KeyVal < 765)){keypressed = '8';} if ((KeyVal >765) && (KeyVal < 819)){keypressed = '9';} if ((KeyVal >819) && (KeyVal < 889)){keypressed = '*';} if ((KeyVal >889) && (KeyVal < 938)){keypressed = '0';} if (KeyVal >938){keypressed = '#';} //NOTE: the values used above are all halfway between the value obtained with each keypress in previous test sketch while (KeyVal > 25) { //Serial.println(keypressed); //lcd.setCursor(0, 1); //lcd.print(keypressed); delay (100); KeyVal = analogRead(keyboardPin); }//wait until key no longer being pressed before continuing }