Problem with stepper "following" a pulse input


got variable pulse input arduino , want work "motor knob example" in arduino ide. differences between example , project have l297 controller. stepper going used in speedometer.

the stepper following pulse input i'm feeding (between 0 , 800hz) it's not running smooth. doesnt jitter seems interrupted something. if go 0 800hz in second smooth way, if increase, 15hz/s takes 20 step waits 300ms before starts again.


does have suggestion make smoother?

code: [select]
const int speedsensor = 4;
const int speedodir = 14;
const int speedostep = 5;

const int speedteeth = 48;
float tirediameter = 0.5; // diameter of tire (meter)
float tirecircumference = pi * tirediameter;

const int updateinterval = 500; // milliseconds
float lastupdate = millis();
unsigned long speedpulsecount = 0l;
float currentspeed = 0;
int prevspeed = 0;
int pos = 0;
int val = 0;

void setup() {
  serial.begin(9600);
 
  pinmode(speedsensor, input);
  pinmode(speedodir, output);
  pinmode(speedostep, output);
 
  pinmode(ododir, output);
  pinmode(odostep, output);
 
  digitalwrite(speedostep, high);
  digitalwrite(speedodir, low);
 
  attachinterrupt(0, speedcount, rising);
 
}

void loop() {
 
  if ( millis() - lastupdate >= updateinterval ) {
   
    // calculate
    nointerrupts();
    // number of rotations per sample (updateinterval)
    currentspeed = speedpulsecount / (((millis() - lastupdate) / 1000) * speedteeth);
    // speed in m/s
    currentspeed = (currentspeed * tirecircumference) / ((millis() - lastupdate) / 1000);
    // m/s km/h
    currentspeed = currentspeed * 3.6;
    interrupts();
   
    // debug
    serial.print("pulsecount:");
    serial.print(speedpulsecount);
    serial.print(" current speed:");
    serial.print(currentspeed, 0);
    serial.print(" val:");
    serial.print(val);
    serial.print(" pos:");
    serial.println(pos);
   
    // reset pulse counter
    speedpulsecount = 0;
    lastupdate = millis();
    prevspeed = currentspeed;
  }
 
  // "follow" routine
  val = map((int)currentspeed, 0, 220, 0, 200);
 
  if ( abs(val - pos) > 2 ) {
    if ( (val - pos) > 0 ) {
      digitalwrite(speedodir, low);
      digitalwrite(speedostep, low);
      delay(10);
      digitalwrite(speedostep, high);
      pos++;
    }
   
   if ( (val - pos) < 0 ) {
      digitalwrite(speedodir, high);
      digitalwrite(speedostep, low);
      delay(10);
      digitalwrite(speedostep, high);
      pos--;
    }
  }
 
}

void speedcount() {
  speedpulsecount++;
}

quote
does have suggestion make smoother?
lose prints , delay?


Arduino Forum > Using Arduino > Project Guidance > Problem with stepper "following" a pulse input


arduino

Comments

Popular posts from this blog

invalid use of void expresion in FlexiTimer2 library

error: a function-definition is not allowed here before '{' token

LED Strip Code