Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <LiquidCrystal_I2C.h>
- LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
- const int reedPin = 2;
- float radius = 18;
- float circumference;
- int reedState;
- int lastReedState = LOW;
- long timer = 0;
- float kmh = 0.00;
- float maximal = 0.00;
- // the following variables are unsigned longs because the time, measured in
- // milliseconds, will quickly become a bigger number than can be stored in an int.
- unsigned long lastDebounceTime = 0;
- unsigned long lastDebounceTime1 = 0;
- unsigned long lastDebounceTime2 = 0;
- unsigned long debounceDelay = 20; // the debounce time; increase if the output flickers
- unsigned long previousMillis13 = 0;
- unsigned long previousMillis1 = 0;
- void setup()
- {
- circumference = 2*3.14*radius; // 89.92
- pinMode(reedPin, INPUT);
- Serial.write(12);//clear
- lcd.begin(16, 2);
- Serial.begin(9600);
- }
- void loop() {
- // read the state of the switch into a local variable:
- int reading = digitalRead(reedPin);
- // check to see if you just pressed the button
- // (i.e. the input went from LOW to HIGH), and you've waited long enough
- // since the last press to ignore any noise:
- // If the switch changed, due to noise or pressing:
- if (reading != lastReedState) {
- // reset the debouncing timer
- lastDebounceTime = millis();
- }
- if ((millis() - lastDebounceTime) > debounceDelay) {
- if (reading != reedState) {
- reedState = reading;
- if (reedState == HIGH) {
- lastDebounceTime1 = millis();
- timer = lastDebounceTime1 - lastDebounceTime2;
- }else{
- lastDebounceTime2 = millis();
- }
- Serial.println(timer);
- kmh = (56.8*float(circumference))/float(timer);//calculate miles per hour
- }
- }
- // set the LED:
- //digitalWrite(ledPin, ledState);
- // save the reading. Next time through the loop, it'll be the lastButtonState:
- lastReedState = reading;
- if(millis() - previousMillis13 >= 500){
- previousMillis13 = millis();
- displayKMH();
- if(kmh > maximal){
- maximal = kmh;
- }
- displayMAX();
- }
- if(millis() - lastDebounceTime2 >= 2000){
- kmh = 0.00;
- }
- }
- void displayKMH(){
- lcd.setCursor(0, 0);
- lcd.print(" ");
- lcd.setCursor(0, 0);
- lcd.print(kmh);
- lcd.setCursor(6, 0);
- lcd.print(" KM/H ");
- }
- void displayMAX(){
- lcd.setCursor(0, 1);
- lcd.print(" MAX ");
- lcd.print(maximal);
- lcd.setCursor(11, 1);
- lcd.print(" KM/H ");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement