Hardware:
Arduino Mega 2560
Hall effect sensor: Honeywell 103SR13A-1, 3 wires, ground, power and signal (unipolar)
One magnet: one revolution is one signal, South side facing sensor
Small LED on breadboard to blink with Hall Effect signal (so I can tell signal is being sent)
Adafruit DS1307 RTC
Adafruit standard 4X20 LCD with Adafruit i2c backpack
Problem:
LCD displays the following;
Time: displayed correctly
RPM: first displays 62 briefly, then 132 briefly, then 662....after ±10 seconds of 662, keeping the magnet rotating on drill the display shows 662 ±80% of time and 132 ±20% of time. 132 is the correct RPM. When drill removed the display reads 062 if 662 was last reading or 052 if 132 was last reading.
Here is my sketch:
// include the library code:
#include <Wire.h>
#include "RTClib.h"
#include <LiquidCrystal.h>
LiquidCrystal lcd(0);
RTC_DS1307 rtc;
volatile byte half_revolutions;
unsigned int rpm;
unsigned long timeold;
float lastmillis;
//pinmode(5, INPUT, PULLUP);Fishing
void setup()
{
lcd.begin(20, 4);
delay(10);
lcd.setBacklight(HIGH);
Serial.begin(57600);
attachInterrupt(5,rpm_fun, RISING);//Initialize the intterrupt pin (Arduino Mega pin 18)
half_revolutions = 0;
rpm = 0;
timeold = 0;
}
void loop()
{
DateTime now = rtc.now();
lcd.setCursor(15, 0);
delay(100);
lcd.print(now.hour(), DEC);
lcd.print(':');
lcd.print(now.minute(), DEC);
delay(100);
//if (half_revolutions >= 20)//this is the problem
//rpm = (float(30*1000/(millis() - timeold)*half_revolutions)); //fishing
rpm = 30*1000/(millis() - timeold)*half_revolutions;
//rpm = ((millis() - timeold)*1000); // Fishing
timeold = millis();
half_revolutions = 0;
//Serial.println(rpm,DEC);
lcd.setCursor(10, 2);
delay(100);
lcd.print("RPM ");
lcd.print(rpm,DEC);
delay(100);
}
void rpm_fun()
{
half_revolutions++;
Serial.println("detect");
}
Attached is a representation of physical layout, schematic and Fritzing pictorial.
Of course I am very very inexperienced in both C++ and electronics, so sorry if my error(s) are super obvious.
I've tried dozens of resistor combinations to no avail and tried many variations on the sketch also to no avail.
Thanks in advance for any and all suggestions.
Jan 14 Fritzing.pdf (332 KB)