Skip to content

Instantly share code, notes, and snippets.

@squarecircleguy
Last active August 2, 2017 14:27
Show Gist options
  • Save squarecircleguy/010fcb8648a5146a87118a81fe6c94c9 to your computer and use it in GitHub Desktop.
Save squarecircleguy/010fcb8648a5146a87118a81fe6c94c9 to your computer and use it in GitHub Desktop.
odometer
#include <EEPROM.h>
#include <LiquidCrystal.h>
const int addr = 0;
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
const int buttonPin = 7; // the pin that the odometer is attached to
long int buttonPushCounter = 0; // counter for the number of revolutions
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button
void setup() {
lcd.begin(16, 2);
// initialize the button pin as a input:
pinMode(buttonPin, INPUT_PULLUP);
//Serial.begin(9600);
buttonPushCounter = EEPROM.read(addr); // disable this to reset odometer to 0
}
void loop() {
// read the pushbutton input pin:
buttonState = digitalRead(buttonPin);
// compare the buttonState to its previous state
if (buttonState != lastButtonState) {
// if the state has changed, increment the counter
if (buttonState == LOW) {
// if the current state is HIGH then the button went from off to on:
buttonPushCounter++;
lcd.print(buttonPushCounter);
lcd.setCursor(0, 16);
lcd.print(buttonPushCounter *0.002167);
lcd.print(" kilometres");
lcd.setCursor(0, 0);
//Serial.print("step ");
//Serial.print(buttonPushCounter);
//Serial.print(" ");
//Serial.print(buttonPushCounter * 0.002167);
//Serial.println("km");
//Serial.print(buttonPushCounter); //Makes onboard Tx signal flash
}
}
// save the current state as the last state, for next time through the loop
lastButtonState = buttonState;
EEPROM.update(addr, buttonPushCounter); // stores the step value (I dont think I'm storing
// the data properly!!!) Loses its place when
// buttonPushCounter > 256 approx...
//addr = addr + 1; //dont know what this does....
//if (addr == EEPROM.length()) {
//addr = 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment