I'm using this code, but it seems to have a bug while measuring speed... Can someone find it? Please... There's also a little bit of junk on it...
////////////////////////////////////
///////Headers/libraries//////////////////////
///////////////////////////////
#include <avr/pgmspace.h>
#include <stdlib.h>
#include <LiquidCrystal.h>
#include <Wire.h>
//#include "Timer.h"
////////////////////////////////////
/////Variables to set/////////////////////////
///////////////////////////////
//set metric = false to use miles
boolean metric = true;
//wheel circumference in centimeters
float wheelC = 212.5;
////////////////////////////////////
///////Pins to set////////////////////////////
///////////////////////////////
/*
Note: Change the pin numbers according
to your individual circuit. For most pins,
it doesn't matter whether you use analog
or digital I/O.
*/
const int photoPin = A0;
const int reedPin = A1; //any input
const int turnRLED = A5; //any output; pin used to turn on button LED
const int turnLLED = A4; //any output; pin used to turn on button LED
const int cs = 10; //any output; LED panel, white wire
const int blueBack = 9; //analog out (~PWM pin); LCD pin #18
int light = 0;
/*
for LCD pin wiring: RGB Backlit LCDs | Character LCDs | Adafruit Learning System
LCD# - Arduino#
1 - GND
2 - 5V
3 - LCD potentiometer (not connected to Arduino)
4 - LCD1 (I used pin D2)
5 - GND
6 - LCD2 (I used pin D3)
7 through 10 aren't connected to the Arduino
11 - LCD3
12 - LCD4
13 - LCD5
14 - LCD6
15 - 5V
16 - redLCD, ~PWM pin (I used pin D5)
17 - greenLCD, ~PWM pin (I used pin D6)
18 - blueLCD, ~PWM pin (I used pin D9)
16 through 18 control the color of the LCD backlight
*/
////////////////////////////////////
///////Bike Variables/////////////////////////
///////////////////////////////
///////Reed Switch//////////////
int circleNum = 0;
float odometer = 0;
float miles = 0;
float kilometers = 0;
float speedometer = 0;
float MPH = 0;
float KPH = 0;
int reedTime = 0;
int reedTimeDelta = 0;
boolean reedOn = false;
///////LCD/////////////
//LiquidCrystal lcd(LCD4, LCD6, LCD11, LCD12, LCD13, LCD14);
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int LCDButton;
//int brightness = 50;
//int red = 130;
//int green = 222;
int blue = 219;
int counter;
boolean brightUp;
boolean backlightOn;
//////////Timer Variables///////
/*int blinkTime = 1000;
Timer scrollTimer;
int scrollTime = 130;
Timer strobeTimer;
int strobeTime = 100;
boolean blinkOn;
*/
///////////////////////////////////////////////////////////////
//////////////////////////SETUP//////////////////////
///////////////////////////////////////////
void setup() {
lcd.begin(16, 2); // set up the LCD's number of rows and columns:
lcd.print("By Caio M. Poit");
{
light = analogRead (A0); //Backlight control via LDR
analogWrite(blueBack, 255 - light / 4);
}
// scrollTimer.every(scrollTime, scroll);
// strobeTimer.every(strobeTime, strobe);
pinMode(blueBack, OUTPUT);
delay(5000);
}
///////////LOOP/////////////////////
void loop() {
// scrollTimer.update();
// strobeTimer.update();
checkReed();
printSpeed();
printDistance();
{
light = analogRead (A0); //Backlight control via ldr
analogWrite(blueBack, 255 - light / 4);
}
}
////////////////CHECK/////////////////////
void checkReed() {
int r = digitalRead(reedPin);
if (r == 1 && reedOn == false) {
reedOn = true;
reedTimeDelta = millis() - reedTime;
reedTime = millis();
circleNum++;
}
else if (r == 0 && reedOn) {
reedOn = false;
}
}
/////////////////////PRINT/////////////////////
void printSpeed() {
speedometer = wheelC / reedTimeDelta;
if (metric) {
KPH = speedometer * 36;
lcd.setCursor(1, 0);
lcd.print("Km/h: ");
lcd.setCursor(6, 0);
lcd.print(KPH, 2);
}
else {
MPH = speedometer * 22.369;
lcd.setCursor(1, 0);
lcd.print("MPH: ");
lcd.setCursor(6, 0);
lcd.print(MPH, 2);
}
}
void printDistance() {
odometer = wheelC * circleNum;
if (metric) {
kilometers = odometer / 100000;
lcd.setCursor(1, 1);
lcd.print("KM: ");
lcd.setCursor(5, 1);
lcd.print(kilometers, 2);
}
else {
miles = odometer / 160934.4;
lcd.setCursor(1, 1);
lcd.print("Miles: ");
lcd.setCursor(5, 1);
lcd.print(miles, 2);
}
}