Hello all.
I would like to help me with a problem that is quite common.
Interferences between LCD and stepper motors.
I am a beginner in arduino and therefore I would need a little help.
I am working on a project realized for some time by another user, which I would like to improve. (Help coding a push button to reverse direction with a potentiometer - Project Guidance - Arduino Forum)
This circuit that i've made should spin a stepper motor one way or the other, and, on the LCD, display on the first line the RPM and on the second, the direction of the motor.
In my circuit i added an i2C LCD. I want the LCD to display the following:
-the direction of the stepper motor (Left-Stop-Right(3 way switch))
-RPM of the motor instead of percentage of the potentiometer
When the motor is stopped(switch to the mid position) i want it to be deactivated(without power).
I want to increase the max motor speed, but keep the minimum.
My main problem is that when i activete (delete the comment) on the lines of code for the display(what to print), my stepper motor spins VERY slowly. (My stepper is: 42BYGH34 and driver A4988)
How can i solve these problems?Any tips?
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <MsTimer2.h>
//Connect to LCD via I2C, default address 0x27 (A0-A2 not jumpered)
//LiquidCrystal_I2C lcd(0x27, 20, 4); // set the LCD address to 0x27 for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x27, 16, 2); //Change to (0x27,16,2) for 16x2 LCD.
// defines pins numbers
const int stepPin = 3;
const int dirPin = 4;
const int switchPinA = A1;
const int switchPinB = A2;
int customDelay, customDelayMapped; // Defines variables
const int ms1 = 8;
const int ms2 = 9;
const int ms3 = 10;
int switchPinAVal = 0;
int switchPinBVal = 0;
//const int stepsPerRevolution = 200; //dar
//int dim = stepsPerRevolution; //cip //dar
void setup() {
{
//Initiate the LCD
lcd.init(); // initialize the lcd
lcd.backlight(); // turn on backlight
lcd.begin(16, 2); //cip
lcd.print("Speed:"); //cip
lcd.setCursor(11, 0); //cip
lcd.print("n/min"); //cip
lcd.setCursor(0, 1); //cip
lcd.print("Direction:"); //cip
}
Serial.begin(9600); //cip
pinMode(switchPinA, INPUT_PULLUP);
pinMode(switchPinB, INPUT_PULLUP);
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
//pinMode(ms1, OUTPUT);
//pinMode(ms2, OUTPUT);
//pinMode(ms3, OUTPUT);
}
void loop()
{
// myStepper.step(dim); //cip
// void Direction(); //cip //dar
if (switchPinAVal == HIGH && switchPinBVal == LOW)
{
motorRunClockwise();
//dim = stepsPerRevolution; //dar
lcd.setCursor(10, 1); //cip
lcd.print(">>>>>>"); //cip
lcd.print(millis()); //cip
}
if (switchPinAVal == LOW && switchPinBVal == HIGH)
{
motorRunAntiClockwise();
// dim = -stepsPerRevolution; //cip
//lcd.setCursor(10, 1); //cip
//lcd.print("<<<<<<"); //cip
//lcd.print(millis()); //cip
}
//else
if (switchPinAVal == HIGH && switchPinBVal == HIGH || switchPinAVal == LOW && switchPinBVal == LOW) //cip
{
digitalWrite(stepPin, LOW); // stop the motor
//lcd.setCursor(10, 1); //cip
//lcd.print(" STOP "); //cip
//lcd.print(millis()); //cip
}
switchPinAVal = digitalRead(A1);
switchPinBVal = digitalRead(A2);
}
///////////////////////////////////// Function for reading the Potentiometer
int speedControl() {
int customDelay = analogRead(A0); // Reads the potentiometer
int newCustom = map(customDelay, 0, 1023, 0, 4000); // Converts the read values of the potentiometer from 0 to 1023 into desireded delay values (0 to 150)
//int newCustom = map(customDelay, 0, 1023, 300, 4000); // Convrests the read values of the potentiometer from 0 to 1023 into desireded delay values (300 to 4000)
//myStepper.setSpeed(customDelay); //cip
//lcd.setCursor(6, 0); //cip
//lcd.print(float(float(customDelay) / float(200))); //cip
return newCustom; //dar
}
////////////////////////////////////
void motorRunClockwise() {
customDelayMapped = speedControl();
digitalWrite(stepPin, LOW);
delayMicroseconds(customDelayMapped);
digitalWrite(stepPin, HIGH);
delayMicroseconds(customDelayMapped);
digitalWrite(dirPin, LOW);
digitalWrite(ms1, HIGH);
digitalWrite(ms2, HIGH);
digitalWrite(ms3, HIGH);
}
////////////////////////////////////
void motorRunAntiClockwise() {
customDelayMapped = speedControl();
digitalWrite(stepPin, LOW);
delayMicroseconds(customDelayMapped);
digitalWrite(stepPin, HIGH);
delayMicroseconds(customDelayMapped);
digitalWrite(dirPin, HIGH);
digitalWrite(ms1, HIGH);
digitalWrite(ms2, HIGH);
digitalWrite(ms3, HIGH);
}