I’m trying to put together my first code for a fully analog antique motorcycle speedometer using a hall sensor and two steppers: one for speed and the other for rotating odometer and trip meter via some gears. I’ve completed the speedometer build and now I’m trying to put together the code for the speed dial stepper. The steppers are tiny 4 wire steppers with Easydrivers. Like I said, only using the speed gauge stepper for now. As I’m using two steppers at the same time, I’m utilizing AccelStepper library.
Attached is my latest attempt on the code. So far, everything I’ve tried works the led but not the motor. I thought I would post it and see if some kind soul would review and let me know what stupid mistake I’m making.
15 mm 4 wire steppers, A1120 hall sensor, arduino uno board, EasyDriver board, 6 volt battery connected to V in.
Code is as follows: Many Thanks
#define LED_BUILTIN 13
float diameter_of_wheel = 2.192; // measure the diameter of wheel and enter here in feet (now for 26.3in od)
volatile byte rotation; //variable for interrupt must be volatile
float timetaken, rpm, detect_time;
int v;
int speeddial;
//int count; to be used for odometer
#include <AccelStepper.h>
AccelStepper stepperS(AccelStepper::DRIVER, 6, 7); // pins for SpeedoS stepper driver
// AccelStepper stepperO(AccelStepper::DRIVER,8,9); will include odometer later
void setup()
{
pinMode(LED_BUILTIN, OUTPUT);
stepperS.setMaxSpeed(300.0);
stepperS.setAcceleration(300.0);
// stepperO.setMaxSpeed(300.0);
// stepperO.setAcceleration(300.0);
attachInterrupt(digitalPinToInterrupt(2), magnet_detect, RISING); // interrupt called when sensors sends digital pin 2 high (every wheel rotation)
rotation = rpm = detect_time = v = timetaken = speeddial = 0; // initialize all variables to zero
}
void loop()
{
if (rotation != 1)
{
digitalWrite(LED_BUILTIN, HIGH);
}
else
{
digitalWrite(LED_BUILTIN, LOW); // led on every other rotation to ensure hall effect is functioning
}
if (rotation == 1)
{
timetaken = millis() - detect_time; //time in milliseconds ffom last magnet detect or one rotation
rpm = (1000 / timetaken) * 60; //to calculate rpm
v = diameter_of_wheel * 3.14 * rpm * 60 / 5280; //velocity in mphpevtime = millis();
speeddial = (v * 2.31 / 2.25);
//Speedo max speed is 120 mph and 10-120 on face is 254 degrees which is 2.31 degrees per mph
// steppers are 18 degrees per step. Using easydriver 1/8 steps, each step is 2.25 degrees
// therefore speedo position (speeddial) is integer of velocity (v) x 2.31/2.25
rotation = 0; //reset rotation to zero
stepperS.moveTo(speeddial);
stepperS.run();
}
}
void magnet_detect() //called whenever a magnet is detected
{
rotation++;
detect_time = millis(); // reset timer for next revolution
// count++; to be used for odometer
}