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);
}
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++.
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:
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);
}
}
}
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 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.