Advertisement
sarahdal

Cat Wheel Speedo

Jan 11th, 2025
575
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.31 KB | None | 0 0
  1. #include <Wire.h>
  2. #include <LiquidCrystal_I2C.h>
  3.  
  4. const int hallPin = 39;                 // Pin for the magentic Hall sensor
  5. const int buttonPin = 3;                // reset button
  6. const float wheelCircumference = 0.705;  // Circumference of the wheel in meters
  7. volatile unsigned long revolutions = 0;
  8. unsigned long lastTime = 0;  // Time to calculate speed
  9. float distance = 0.0;        // Total distance covered
  10. float speed = 0.0;           // Speed in meters per second
  11.  
  12. volatile unsigned long lastTriggerTime = 0;  // Last interrupt trigger time
  13. const unsigned long debounceDelay = 300;     // Debounce delay in milliseconds
  14.  
  15. volatile unsigned long lastButtonTime = 0;      // Last button press time
  16. const unsigned long buttonDebounceDelay = 300;  // Debounce delay for button (ms)
  17.  
  18. #define I2C_SCL 35
  19. #define I2C_SDA 33
  20.  
  21. //set up LCD display
  22. LiquidCrystal_I2C lcd(0x27, 16, 2);  // set the LCD address to 0x27 for a 16 chars and 2 line display
  23.  
  24. void count() {
  25.   unsigned long currentTime = millis();
  26.   if (currentTime - lastTriggerTime > debounceDelay) {
  27.     revolutions++;                  // Increment the total revolutions count
  28.  
  29.  
  30.     lastTriggerTime = currentTime;  // Update the last trigger time
  31.   }
  32. }
  33.  
  34. void setup() {
  35.   Serial.begin(115200);
  36.   Wire.begin(I2C_SDA, I2C_SCL);
  37.   pinMode(hallPin, INPUT_PULLUP);                                  // Set Hall magnet pin as input
  38.   pinMode(buttonPin, INPUT_PULLUP);                                // reset butotn
  39.   attachInterrupt(digitalPinToInterrupt(hallPin), count, RISING);  // Trigger count on rising edge
  40.   lcd.init();                                                      // INIT the LCD
  41.   lcd.backlight();                                                 // Light the beacons
  42. }
  43.  
  44. void loop() {
  45.   // Every 5 seconds, calculate speed and distance
  46.   unsigned long currentTime = millis();
  47.  
  48.   // Check for button press
  49.   if (digitalRead(buttonPin) == LOW) {
  50.     if (currentTime - lastButtonTime > buttonDebounceDelay) {
  51.       resetValues();                 // Reset the values
  52.       lastButtonTime = currentTime;  // Update last button press time
  53.     }
  54.   }
  55.  
  56.   if (currentTime - lastTime >= 5000) {
  57.     float timeInSeconds = (currentTime - lastTime) / 1000.0;
  58.  
  59.     // Calculate speed in meters per second
  60.     speed = (revolutions * wheelCircumference) / timeInSeconds;
  61.  
  62.     // Calculate total distance
  63.     distance += revolutions * wheelCircumference;
  64.  
  65.     // Go to the Write subroutine to output the results
  66.     Write();
  67.  
  68.     // Reset the count and update the last time
  69.     revolutions = 0;
  70.     lastTime = currentTime;
  71.   }
  72. }
  73.  
  74. void resetValues() {
  75.   revolutions = 0;
  76.   speed = 0;
  77.   distance = 0;
  78.   Serial.println("All values have been reset");
  79. }
  80.  
  81. //Writes the results
  82. void Write() {
  83.    // Debug information for Serial Monitor
  84.   Serial.println("Data:");
  85.   Serial.print("Revolutions: ");
  86.   Serial.println(revolutions);
  87.   Serial.print("Current Speed: ");
  88.   Serial.println(speed);
  89.   Serial.print("Distance: ");
  90.   Serial.println(distance);
  91.   Serial.println(" -- ");
  92.   // print to LCD
  93.   lcd.clear();
  94.   lcd.setCursor(0, 0);
  95.   lcd.print("Speed: ");
  96.   lcd.print(speed);
  97.   lcd.print("m/s");
  98.   lcd.setCursor(0, 1);
  99.   // lcd.print("Dist: ");
  100.   // lcd.print(distance);
  101.   // lcd.print("m");
  102.   lcd.print("count: ");
  103.   lcd.print(revolutions);
  104. }
  105.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement