Hello fellow makers,
I have limited experience with arduino, but I managed to set up a simple speedometer based on a hall effect sensor and a servo - worked a treat, but it was more of a proof of concept than a finished product.
Now, as part of converting an old motorbike to electric, I want to keep the original speedometer, but due to the fact that I no longer have the old style cable as the back wheel was replaced with a motor, I want to do it with a use of X27 stepper motor.
The plan is to drive the stepper directly from Arduino, feed it hall effect signal that is generated by the electric motor and have Arduino do the maths. Sounds simple.
I am struggling at the very start though and I cannot for the love of me figure out why. I've got an Arduino Nano, I've got an X27 stepper motor and I've got the SwitecX25 library.
Using the build in example, it works fine (the needle travels to zero, then to a set point, and then using the serial input I can make it go wherever I want). So I know my wiring is good, the motor works as it should and all is good.
This is where the problem happens - for now, I want a simple code that will make it go from 0, to max, then back to 0, just like shown in this video (https://www.youtube.com/watch?v=UJKaaRR9W6g&ab_channel=GuyCarpenter), but it just does not work. I tried doing a delay(1000) then going back to zero, but the motor just buzzes and does nothing... Anyone with an idea of what is going wrong?
//----------------------------------------------------------------------
// https://github.com/clearwater/SwitecX25
//
// This is an example of using the SwitchX25 library.
// It zero's the motor, sets the position to mid-range
// and waits for serial input to indicate new motor positions.
//
// Open the serial monitor and try entering values
// between 0 and 944.
//
// Note that the maximum speed of the motor will be determined
// by how frequently you call update(). If you put a big slow
// serial.println() call in the loop below, the motor will move
// very slowly!
//----------------------------------------------------------------------
#include <SwitecX25.h>
// standard X25.168 range 315 degrees at 1/3 degree steps
#define STEPS (315*3)
// For motors connected to digital pins 4,5,6,7
SwitecX25 motor1(STEPS,4,5,6,7);
void setup(void)
{
// run the motor against the stops
motor1.zero();
// start moving towards the center of the range
motor1.setPosition(944);
Serial.begin(9600);
Serial.print("Enter a step position from 0 through ");
Serial.print(STEPS-1);
Serial.println(".");
}
void loop(void)
{
static int nextPos = 0;
// the motor only moves when you call update
motor1.update();
if (Serial.available()) {
char c = Serial.read();
if (c==10 || c==13) {
motor1.setPosition(nextPos);
nextPos = 0;
} else if (c>='0' && c<='9') {
nextPos = 10*nextPos + (c-'0');
}
}
}
EDIT: I am doing some more testing, and while the issue persists, I have connected a hall effect sensor and using code from here (CB450 K0 Bomber electronic speedo/tach gauge | Honda Twins) it does work, albeit backwards (starts at position 944 and when I put the magnet close, it moves towards 0). I am even more confused - the motor cannot be wired wrong as when I put 0 in the serial input using the example above, it goes to the correct position. What's going on?