Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Wire.h>
- #include <LiquidCrystal_I2C.h>
- const int hallPin = 39; // Pin for the magentic Hall sensor
- const int buttonPin = 3; // reset button
- const float wheelCircumference = 0.705; // Circumference of the wheel in meters
- volatile unsigned long revolutions = 0;
- unsigned long lastTime = 0; // Time to calculate speed
- float distance = 0.0; // Total distance covered
- float speed = 0.0; // Speed in meters per second
- volatile unsigned long lastTriggerTime = 0; // Last interrupt trigger time
- const unsigned long debounceDelay = 300; // Debounce delay in milliseconds
- volatile unsigned long lastButtonTime = 0; // Last button press time
- const unsigned long buttonDebounceDelay = 300; // Debounce delay for button (ms)
- #define I2C_SCL 35
- #define I2C_SDA 33
- //set up LCD display
- LiquidCrystal_I2C lcd(0x27, 16, 2); // set the LCD address to 0x27 for a 16 chars and 2 line display
- void count() {
- unsigned long currentTime = millis();
- if (currentTime - lastTriggerTime > debounceDelay) {
- revolutions++; // Increment the total revolutions count
- lastTriggerTime = currentTime; // Update the last trigger time
- }
- }
- void setup() {
- Serial.begin(115200);
- Wire.begin(I2C_SDA, I2C_SCL);
- pinMode(hallPin, INPUT_PULLUP); // Set Hall magnet pin as input
- pinMode(buttonPin, INPUT_PULLUP); // reset butotn
- attachInterrupt(digitalPinToInterrupt(hallPin), count, RISING); // Trigger count on rising edge
- lcd.init(); // INIT the LCD
- lcd.backlight(); // Light the beacons
- }
- void loop() {
- // Every 5 seconds, calculate speed and distance
- unsigned long currentTime = millis();
- // Check for button press
- if (digitalRead(buttonPin) == LOW) {
- if (currentTime - lastButtonTime > buttonDebounceDelay) {
- resetValues(); // Reset the values
- lastButtonTime = currentTime; // Update last button press time
- }
- }
- if (currentTime - lastTime >= 5000) {
- float timeInSeconds = (currentTime - lastTime) / 1000.0;
- // Calculate speed in meters per second
- speed = (revolutions * wheelCircumference) / timeInSeconds;
- // Calculate total distance
- distance += revolutions * wheelCircumference;
- // Go to the Write subroutine to output the results
- Write();
- // Reset the count and update the last time
- revolutions = 0;
- lastTime = currentTime;
- }
- }
- void resetValues() {
- revolutions = 0;
- speed = 0;
- distance = 0;
- Serial.println("All values have been reset");
- }
- //Writes the results
- void Write() {
- // Debug information for Serial Monitor
- Serial.println("Data:");
- Serial.print("Revolutions: ");
- Serial.println(revolutions);
- Serial.print("Current Speed: ");
- Serial.println(speed);
- Serial.print("Distance: ");
- Serial.println(distance);
- Serial.println(" -- ");
- // print to LCD
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print("Speed: ");
- lcd.print(speed);
- lcd.print("m/s");
- lcd.setCursor(0, 1);
- // lcd.print("Dist: ");
- // lcd.print(distance);
- // lcd.print("m");
- lcd.print("count: ");
- lcd.print(revolutions);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement