/* Attiny44 Speedometer by Cesare Brizio | CC by-sa-nc// Modified by Jobin Varghese this sketch is in the public domain Version 2.0 - 17 jun 2019 ------------------------------ The implementation is relatively rough: - wheel circumference has to be hard-coded - an attempt at debouncing has been made both at ohysical and software level - to no avail. Will be revised soon. A few more details here: http://www.cesarebrizio.it/Arduino/Speedometer.html Modified from an original by John Boxall Example 37.3 – Basic speedometer using millis(); http://tronixstuff.com/tutorials > chapter 37 John Boxall | CC by-sa-nc Hardware – you will need a sensor. For example – a reed switch and magnet. Consider the reed switch to be a normally-open button, and connect as usual with a pull-down resistor. ========================== The circuit for LCD board (HD4770 compatible display) 4-wire dialogue ========================== * LCD RS pin to digital pin 15 * LCD Enable pin to digital pin 14 * LCD D4 pin to digital pin 5 * LCD D5 pin to digital pin 4 * LCD D6 pin to digital pin 3 * LCD D7 pin to digital pin 2 * LCD R/W pin to ground * LCD VSS pin to ground * LCD VCC pin to 5V * 10K resistor: * ends to +5V and ground * wiper to LCD VO pin (pin 3) */ // include the library code: #include // initialize the library with the numbers of the interface pins const int rs = 5, en = 4, d11 = 3, d12 = 2, d13 = 1, d14 = 0; LiquidCrystal lcd(rs, en, d11, d12, d13, d14); float start, finished; float elapsed, time; float circMetric=2.093; // wheel circumference (in meters) float circImperial; // using 1 kilometer = 0.621371192 miles float speedk, speedm; // holds calculated speed vales in metric and imperial void setup() { // convert metric to imperial for MPH calculations circImperial=circMetric*.62137; // the syntax with digitalPinToInterrupt should allow portability //among different Arduino models - see https://www.arduino.cc/reference/en/language/functions/external-interrupts/attachinterrupt/ attachInterrupt(digitalPinToInterrupt(8), speedCalc, RISING); // interrupt called when sensors sends digital 2 high (every wheel rotation) //attachInterrupt(0, speedCalc, RISING); // interrupt called when sensors sends digital 2 high (every wheel rotation) //start now (it will be reset by the interrupt after calculating revolution time) start=millis(); // set up the LCD's number of columns and rows: lcd.begin(16, 2); // Print a transitory message to the LCD. lcd.print(" Bi_SPEED"); // delay(5000); //just to allow you to read the initialization message } void speedCalc() { //Function called by the interrupt if((millis()-start)>100) // 100 millisec debounce { //calculate elapsed elapsed=millis()-start; //reset start start=millis(); //calculate speed in km/h speedk=(3600*circMetric)/elapsed; //calculate speed in mph speedm=(3600*circImperial)/elapsed; } } void loop() { // The loop will be interrupted by the sensor each time the // magnet passes near the sensor, in other words once per revolution // Top line in the 16 char, 2 lines display - speed data lcd.setCursor(0,1); lcd.setCursor(0,1); lcd.print(int(speedk)); lcd.print(" km/h "); lcd.print(int(speedm)); lcd.print(" MPH "); }