So this is the code I wrote despite it being 24 hours late.
/* Distance Analyzer 3000 (Pro-Trinket EDC hackaday contest) John Hamann Started November 29th 2014 */ #include <LiquidCrystal.h> #include <Encoder.h> Encoder salad(3, 4 ); //inside joke LiquidCrystal lcd(12, 11 , 18, 17, 16, 15); int encoderCount = 0; const double pi = 3.14159; const double radius = 4.1; //millimeters const int totalSteps = 92; //number of clicks for one full rotation const int unitCyclePin = 0; const int resetPin = 5; int unitCount = 3; void setup(){ pinMode(unitCyclePin, INPUT); pinMode(resetPin, INPUT); lcd.begin(16, 2); lcd.print("Distance"); lcd.setCursor(0,1); lcd.print("Analyzer 3000"); delay(2000); } void loop(){ encoderCount = salad.read(); int unitState = digitalRead(unitCyclePin); if(unitState == HIGH){ unitCount++; } if(unitCount == 5){ unitCount = 0; } updateDisplay(unitCount, calculateDistance(encoderCount, unitCount)); } void updateDisplay(int unit, double distance){ lcd.clear(); lcd.setCursor(0,0); lcd.print(distance); if(unit == 0){ lcd.setCursor(0,1); lcd.print("cm"); } if(unit == 1){ lcd.setCursor(0,1); lcd.print("m"); } if(unit == 2){ lcd.setCursor(0,1); lcd.print("in"); } if(unit == 3){ lcd.setCursor(0,1); lcd.print("ft"); } } /* 0 = centimeters 1 = meters 2 = inches 3 = feet */ double calculateDistance(int count, int unit){ double radianCount = ((2 * pi) / totalSteps) * count; if(unit == 0){ double radiusCent = radius / 10; //Change millimeters to centimeters return radiusCent * radianCount; //Arc length calculation } if(unit == 1){ double radiusMeter = radius / 100; return radiusMeter * radianCount; } if(unit == 2){ double radiusInch = radius * 0.039370; return radiusInch * radianCount; } if(unit == 3){ double radiusFeet = (radius * 0.039370) / 12; return radiusFeet * radianCount; } return 0; }
The reset and unit changer are as of now unimplemented. But for my first real arduino project it turned out pretty well.
Discussions
Become a Hackaday.io Member
Create an account to leave a comment. Already have an account? Log In.