Skip to content

Instantly share code, notes, and snippets.

@matthewrudy
Forked from squarecircleguy/counter_5_with_save.ino
Last active August 2, 2017 14:35
Show Gist options
  • Save matthewrudy/821e42c998ae5dc7c2fd04b0fcc2075d to your computer and use it in GitHub Desktop.
Save matthewrudy/821e42c998ae5dc7c2fd04b0fcc2075d to your computer and use it in GitHub Desktop.
odometer
#include <EEPROM.h>
#include <LiquidCrystal.h>
const int counterAddr = 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);
EEPROM.get(counterAddr, buttonPushCounter); // 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.put(counterAddr, 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