I'm a new italian user, so sorry for my english. I've found this great forum searching for build a simple rpm meter for my sim, and after viewing other user post i realize my first project. The platform is an arduino uno connected to an h bridge to drive a small stepper motor from real car cluster( a clone of switec x25). I,'m using x-sim 3. The bound rate is setting to 115200 , x-sim sending data at 8 bit, decimal output. If i start the simulator the meter moves accordly but with some issue, is slow to reach the high point of the scale and sometimes instead of return to min, hold a * position. I try to change the pause in converter from 33ms to 40ms and slower but seems only affected the accuracy of the movement, if set to lower value instead the motor jitter or not moving at all.
I've tried the example sketch motor knob and other and the stepper works properly.
I think the problem is that arduino analog.read function is too slow to reading the data coming from x-sim as decimal output. Maybe if i send it in binary form the aquisition could be faster but i need a help for the code. I post the currently used.
Thanks in advance.
- Code: Select all
#include <Stepper.h>
Stepper stepper(720, 2, 3, 4, 5);
int rpm;
int nextStep;
int pos = 0;
char kind_of_data;
void setup(){
Serial.begin(115200);
stepper.setSpeed(30);
}
void loop()
{
while(Serial.available() > 0)
{
kind_of_data = Serial.read();
if (kind_of_data == 'R' ) Read_rpm();
}
}
void Read_rpm(){
delay(1);
int rpm100 = Serial.read()- '0';
delay(1);
int rpm10 = Serial.read()- '0';
delay(1);
int rpm1 = Serial.read()- '0';
rpm = 100*rpm100 + 10*rpm10 + rpm1;
rpm = map(rpm,127,255,0,600);
nextStep = rpm - pos;
stepper.step(nextStep);
pos = rpm;
}