/*
Speedo Code
*/

#include [opencarrot]LiquidCrystal.h>

LiquidCrystal lcd(5, 4, 3, 2, 1, 0); //Initialize the pins
int sensorPin = 7; // select the input pin for the hall effect sensor
int ledPin = 8; // select the pin for the LED
int sensorValue = 510; // variable to store the value coming from the sensor. Default value (no magnet) drifts between 507 and 511
int sensorState = 0;
int lastsensorState = 0;
int sensorCount = 0;
unsigned long startTime = 0;
unsigned long stopTime = 0;
unsigned long sampleLength = 10000;

void setup() {
// declare the ledPin as an OUTPUT:
pinMode(ledPin, OUTPUT);
lcd.begin(16, 2);
lcd.print("Your Speed is:");
}

void loop() {
for(int i=0; i < 3; i++){
digitalWrite(ledPin, HIGH);
delay(100);
digitalWrite(ledPin, LOW);
delay(100);
}
startTime = millis();
stopTime = startTime + sampleLength;
while(millis() < stopTime){
sensorValue = analogRead(sensorPin);
if (sensorValue > 530){
sensorState = 1;
if (sensorState != lastsensorState){
sensorCount++;
lastsensorState = sensorState;
delay(3);
}
}
else {
sensorState = 0;
lastsensorState = 0;
delay(3);
}
}

lcd.setCursor(9,1);
lcd.print(" ");
lcd.setCursor(8,1);
lcd.print(" ");
lcd.setCursor(7, 1);
lcd.print(sensorCount);

sensorCount = 0;
sensorState = 0;
lastsensorState = 0;
delay(1000);
}