I am trying to make a bike speedometer with an Arduino Nano and an LCD display and I recently figured out that the attachinterrupt function only worked on certain pin. I had it on pin 10 before but changed it to pin 2 as it was not working. Once I changed it to pin 2 the speed kept going up without the reed being connected or when it was connected it still kept going up. Does anyone know why it would be doing this?
Code:
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 6, 5, 4, 3);
float start, finished;
float elapsed, time;
float circMetric = 2.314194; // wheel circumference (in meters)
float circImperial;
float speedm;
void setup()
{
circImperial = circMetric * .62137;
attachInterrupt(digitalPinToInterrupt(2), speedCalc, RISING);
start = millis();
analogWrite(7, 20);
lcd.begin(16, 2);
lcd.print(“NONNACYCLES”);
delay(2000);
Serial.begin(115200);
}
void speedCalc()
{
if ((millis() - start) > 100)
{
elapsed = millis() - start;
start = millis();
speedm = (3600 * circImperial) / elapsed;
}
}
void loop()
{
lcd.setCursor(0, 0);
lcd.print(" “);
lcd.setCursor(0, 0);
lcd.print(int(speedm));
lcd.print(” MPH ");
delay(100);
}
source_code.ino (838 Bytes)