// Copyright (C) 2014 Paul T Middlehurst. // Permission is granted to copy, distribute and/or modify this document // under the terms of the GNU Free Documentation License, Version 1.3 // or any later version published by the Free Software Foundation; // with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. // A copy of the license is included in the section entitled "GNU Free Documentation License". // // // PTM DRO V0.1 10th January 2013 // V0.2 1st February 2013 3 & 4 DP for mm and inch #include #define encoder0PinA 3 #define encoder0PinB 2 #define zeroPin 5 #define inchPin 4 LiquidCrystal lcd(12,11,6,10,9,8,7); volatile long encoder0Pos = 0; float tmp = 0; long Aold = 0; long Bnew = 0; // change the following line to suit your application float Mpitch = 1; //pitch of leadscrew in mm float Ipitch ; float Slots = 90; //slots in wheel void setup() { Slots *=4; //convert slots to edges Ipitch = Mpitch * 0.0393700787; // pitch of leadscrew in inches pinMode(encoder0PinA, INPUT); pinMode(encoder0PinB, INPUT); pinMode(zeroPin, INPUT); digitalWrite(zeroPin, HIGH); digitalWrite(inchPin, HIGH); lcd.begin(16,2); lcd.clear(); lcd.print(" PTM DRO V0.2"); lcd.setCursor(0,1); // encoder A on interrupt 1 (pin 3) attachInterrupt(1, doEncoderA, CHANGE); // encoder B on interrupt 0 (pin 2) attachInterrupt(0, doEncoderB, CHANGE); } void loop() { if (tmp != encoder0Pos || (digitalRead(zeroPin) == 0)) { if (digitalRead(zeroPin) == 0) { tmp = 0; encoder0Pos = 0; } if (digitalRead(inchPin) == 0) { tmp = encoder0Pos / Slots * Ipitch; lcd.setCursor(0,1); lcd.print(tmp,4); lcd.print(" inch"); } else { tmp = encoder0Pos / Slots * Mpitch; lcd.setCursor(0,1); lcd.print(tmp,3); lcd.print(" mm"); } lcd.print(" "); } } // Interrupt A void doEncoderA() { // if Bnew = Aold, increment, otherwise decrement Bnew^Aold ? encoder0Pos++:encoder0Pos--; Aold=digitalRead(encoder0PinA); } // Interrupt B void doEncoderB() { Bnew=digitalRead(encoder0PinB); // if Bnew = Aold, increment, otherwise decrement Bnew^Aold ? encoder0Pos++:encoder0Pos--; }