RPM printing on lcd shield

Hello everyone,

I am definitely a beginner and have an easy question. I can drive a stepper by controlling its speed with a potentiometer. My code is below and i want to see the rpm of that motor on the lcd shield. How can i do could you help me?

#define potmeterPin A1

#include <LiquidCrystal.h>

const int pin_RS = 8; 
const int pin_EN = 9; 
const int pin_d4 = 4; 
const int pin_d5 = 5; 
const int pin_d6 = 6; 
const int pin_d7 = 7; 
const int pin_BL = 10; 

LiquidCrystal lcd( pin_RS,  pin_EN,  pin_d4,  pin_d5,  pin_d6,  pin_d7);

// defines pins numbers

const int stepPin = 12;

const int dirPin = 11;

const int enPin = 13;

int p;

float delay_time=500;

void setup() {


  Serial.begin(9600);

lcd.begin(16, 2);


  // Sets the two pins as Outputs

  pinMode(stepPin,OUTPUT);

  pinMode(dirPin,OUTPUT);



  pinMode(enPin,OUTPUT);

  digitalWrite(enPin,LOW);

  // Set Dir to Home switch

  digitalWrite(dirPin,HIGH); // Enables the motor to move in a particular direction

}

void loop() {

     p = analogRead(potmeterPin);

    delay_time = map(p,0,1023,1200,250);

     motorStep(1);
     
}

void motorStep( int MAX){



   for(int x = 0; x < MAX; x++) {

        digitalWrite(stepPin,HIGH);

        delayMicroseconds(delay_time);

        digitalWrite(stepPin,LOW);

        delayMicroseconds(delay_time);

      }

Welcome to the forum

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

When you posted your code without code tags did you receive a warning message ?

Hello jerrysnye
Run some tutorials for the hardware selected first.
If you are happy with the results of the tutorials you can merge these to your project.
Have a nice day and enjoy coding in C++.

Thanks for adding the code tags. I hope that you can see how much easier it is to read and copy

When you posted your code without code tags did you receive a warning message ?

looks like you specify the delay between steps in usec. i've found it difficult to step a motor faster than ~1msec

do you know how to calculate the rpm?

do you know how to display something on the LCD?

i got warning but i can't do it for the first. Anyway, now I know.

As i know, motor works 0.25ms high and 0.25ms low. Totally its pulse period is 0.5ms. About lcd when i write

lcd.print ("SOMETHING");

It seems on the screen. Actually my question is that how can i found the actual rpm value on every position of pot.

You can estimate RPM by calculating steps per minute and dividing by steps per revolution.

There are 60 million microseconds in a minute. Divide that by the time (microseconds) between steps to get steps per minute. The time between steps is 2*delay_time + loop overhead. You have two digital writes and an analogRead() per loop so maybe add 20 microseconds for overhead:

const unsigned StepsPerRevolution = 200;
unsigned long microseconds_per_step = 2 * delay_time + 20;
unsigned long stepsPerMinute = 60000000ul / microseconds_per_step;
unsigned RPM = stepsPerMinute / StepsPerRevolution;

I am really sorry about my mistake. I said rpm but what i want is "spin number". I try to revise my code like below. In this code, the motor speed decreases. I think its because it calculate and print the spin number in every cycle of for loop. How can i simplify it?

#define potmeterPin A1

#include <LiquidCrystal.h>

const int pin_RS = 8; 
const int pin_EN = 9; 
const int pin_d4 = 4; 
const int pin_d5 = 5; 
const int pin_d6 = 6; 
const int pin_d7 = 7; 
const int pin_BL = 10; 


LiquidCrystal lcd( pin_RS,  pin_EN,  pin_d4,  pin_d5,  pin_d6,  pin_d7);

const int stepPin = 12;

const int dirPin = 11;

const int enPin = 13;

float delay_time;

int a;

int b;

float i ;

int p;

void setup() {


  Serial.begin(9600);

lcd.begin(16, 2);



  // Sets the two pins as Outputs

  pinMode(stepPin,OUTPUT);

  pinMode(dirPin,OUTPUT);



  pinMode(enPin,OUTPUT);

  digitalWrite(enPin,LOW);

  // Set Dir to Home switch

  digitalWrite(dirPin,HIGH); // Enables the motor to move in a particular direction

}

void loop() {

     p = analogRead(potmeterPin);

    delay_time = map(p,0,1023,1200,250);

     motorStep(1);
     
}

void motorStep( int MAX){



   for(int x = 0; x < MAX; x++) {

        digitalWrite(stepPin,HIGH);

        delayMicroseconds(delay_time);

        a=digitalRead(stepPin);

        digitalWrite(stepPin,LOW);

        delayMicroseconds(delay_time);

        b=digitalRead(stepPin);

        if (a!=b){
        
           i=i+1;
           lcd.setCursor(0,0);
           lcd.print("spin=");
           lcd.setCursor(5,0);
           lcd.print(i/200);
          

        }
 

          }
        
       }

what is spin #? do you mean position in # steps, # of rotations or fractions of rotation?

You can remove the two digitalRead() calls and the 'if'.

void motorStep( int MAX)
{
  lcd.setCursor(0, 0);
  lcd.print("spin=");
  
  for (int x = 0; x < MAX; x++)
  {
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(delay_time);

    digitalWrite(stepPin, LOW);
    delayMicroseconds(delay_time);

    i = i + 1;

    lcd.setCursor(5, 0);
    lcd.print(i / 200);
  }
}

Thanks my friend, now I'm closer to my goal but another problem occurs. It counts digits without fractions(which i don't want) after 163 it returns back like 162, 161.. It gets pretty ridiculous after 100 while counting back. The final code is below:

#define potmeterPin A1

#include <LiquidCrystal.h>

const int pin_RS = 8; 
const int pin_EN = 9; 
const int pin_d4 = 4; 
const int pin_d5 = 5; 
const int pin_d6 = 6; 
const int pin_d7 = 7; 
const int pin_BL = 10; 


LiquidCrystal lcd( pin_RS,  pin_EN,  pin_d4,  pin_d5,  pin_d6,  pin_d7);

const int stepPin = 12;

const int dirPin = 11;

const int enPin = 13;

float delay_time;

int a;

int b;

int i = 0 ;

int j ;

int p;

void setup() {

  Serial.begin(9600);

  lcd.begin(16, 2);

  pinMode(stepPin,OUTPUT);

  pinMode(dirPin,OUTPUT);

  pinMode(enPin,OUTPUT);
  

  digitalWrite(enPin,LOW);

  digitalWrite(dirPin,HIGH);

}

void loop() {

  p = analogRead(potmeterPin);

  delay_time = map(p,0,1023,1200,200);

  motorStep(1);
     
}

void motorStep( int MAX) {
  
  lcd.setCursor(0, 0);
  lcd.print("spin=");
  
  for (int x = 0; x < MAX; x++)
  {
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(delay_time);

    digitalWrite(stepPin, LOW);
    delayMicroseconds(delay_time);

    i = i + 1;
    j=i/200;
    if (isDigit(j))
      lcd.setCursor(5, 0);
      lcd.print(j);
  }
}

You need to remove the previously printed value before printing the new one otherwise the third digit of 100 will still be on the screen when you print 99 so that it looks like 990

I added clearing lcd function in my loop and problem is going on. It is not after 99. It counts well until 163 turns, then it starts to count down. 163 number is equal to approximately 32600 steps for my stepper. Do you know anything about this 32600 number? Btw, i use 1602 LCD Keypad Shield.

I wrote my code with millis function just for trying. It still counts down after 163 value.

It's probably 32767. The largest number a two byte signed integer can hold.

You are overflowing the values of int. Make these variables unsigned longs.

How many steps do you really really need to count?

if (isDigit(j))
Why do you have this?

lcd.setCursor(0, 0);
  lcd.print("spin=");

This is unchanging, and is best printed in setup() and not repeated with each step.

Yes man definitely right, unsigned long solved the problem. Many thanks.

It's a useful number to remember: any time someone is complaining that their code goes crazy after 30 seconds. Hmmmm, I wonder what in the world that could be?

Now; after every complete tour, print command is working. Just at this time, there is strong vibration on my stepper like an hammer drill. It dashes when every turn is complete. Is there a way to eliminate this not-stable working? Vibration is not a good thing for my target tool. I am working between 300-400 rpm. I tried to decrease is to 50rpm by increasing delays. This dash effect still continue.

What code are you using when this happens?