//This code was made by Alex Cockman with inspiration from the theory from the websites linked below.
//Adruino.com's link: https://www.arduino.cc/en/Tutorial/LibraryExamples/HelloWorld
//danielleamya's linked: https://forum.arduino.cc/t/bike-speedometer-using-reed-switch/514581
//Arduino.com's linked: https://www.arduino.cc/reference/en/language/functions/external-interrupts/attachinterrupt/
//Adminastrator from Eletronics Hub eletronic - set up and understanding of reed switches: https://www.electronicshub.org/how-to-interface-reed-switch-with-arduino/#:~:text=Connect%20one%20end%20of%20the,of%20a%20Pull%2Dup%20Resistor
#include <LiquidCrystal.h>
int reedSwitch = 2; //Declaring pin for reed switch to be used. Pin 2 or 3 must be used for the reed switch on the Uno
int radius = 12.5; //hard coding in the radius of the wheel for future conversion into circumference
int circumference; //declaring the circumference variable
int totaltime; //declaring the total time until the next trip on the reed switch variable
int Speed; //declaring the Speed variable
int timer; //declaring the timer to calculate the total time variable
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 6; // Declaring pins for LCD Display
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
void isr()
{
timer++; // Begin the timer and start counting
delayMicroseconds(9600); // Delay to start after a 0.0096 seconds
}
void setup(){
Serial.begin(9600); // Start serial monitor after a few seconds. Mainly for testing code to get it to work.
lcd.begin(16,2); // Declare the scope that the LCD screen displays on
lcd.setCursor(6, 0); // Set Speed in the middle of the lCD display
lcd.print("Speed"); // Print on LCD screen what is inbetween the quotation marks
attachInterrupt(digitalPinToInterrupt(2), isr, RISING); //Begins the count of seconds when the reed switch in pin 2 is set off
pinMode(reedSwitch, INPUT_PULLUP); // Sets the reed switch to be an input
timer = 0; // Sets timer to 0 seconds
Speed = 0; // sets the speed to 0 seconds
totaltime = 0; // sets the total time to start off at 0
circumference = 6.28 * radius; // calculated the circumference
}
void loop() {
if (millis() - totaltime >= 1000) { // Gets varibales to go into the loop everytime the millis() - the total time elapsed is less than 1ms
// So unless I become superman then no human will be able to achieve this.
detachInterrupt(digitalPinToInterrupt(2)); // Stops the timer from the reading from pin 2 aka the reed switch
Speed = ((8.181818182*circumference)/(millis() - totaltime)*timer); // Calculates the speed of the bike. The 8.18 number is converting the time in seconds into hours and inches into feet.
timer = 0; // Makes the timer go back to zero
totaltime = millis(); // Makes the total time the new time
attachInterrupt(digitalPinToInterrupt(2), isr, RISING); // Make the ISR loop attach back
lcd.setCursor(8,1); // Sets cursor to the bottom left
lcd.print(Speed); // Prints the speed to the LCD display
Serial.print("Speed"); // Prints "Speed" to serial monitor. Mainly for troubleshooting.
Serial.print(Speed); // Prints the speed to the serial monitor. Mainly for troubleshooting.
}
}
Comments