Hello, I am playing around with my Stepper Motor and LCD display at the same time. My aim was to use buttons to turn the stepper and whilst it's turning have the LCD write "Direction = Anti-Clockwise" for example.
Individually I can make the stepper motor work, and the LCD work for what I want them to do. However, as soon as I put them BOTH into my loop, only one will work.
I have tried to highlight the code to make it easier, but it's still messy and I apologise.
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
#include <CustomStepper.h>
CustomStepper stepper(7, 8, 9, 10, (byte[]){8, B1000, B1100, B0100, B0110, B0010, B0011, B0001, B1001}, 4075.7728395, 12, CW);
boolean rotate1 = false;
boolean rotatedeg = false;
boolean crotate = false;
boolean cwStatus = false;
const int buttonPin = 11;
const int ledPin = 12;
const int buttonPin2 = 6;
const int buttonPin3 = 5;
const int ledPin2 = 4;
const int buttonPin4 = 3;
int buttonState2 = 0;
int buttonState = 0;
int buttonState3 = 0;
int currentSpeedValue;
int currentSensorValue;
int buttonState4 = 0;
const int potPin = A0;
const int sensorPin = A1;
void setup()
{
lcd.begin(16,2);
lcd.setCursor(0,0);
lcd.print("Louis's Stepper");
lcd.setCursor(0,1);
lcd.print("Motor");
stepper.setRPM(12);
stepper.setSPR(4075.7728395);
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);
pinMode(buttonPin2, INPUT);
pinMode(buttonPin3, INPUT);
pinMode(ledPin2, OUTPUT);
pinMode(buttonPin4, INPUT);
delay(3000);
lcd.clear();
}
void loop() //############# SOMETHING IN HERE WON'T ALLOW LCD AND
//STEPPER MOTOR CONTROL##############################
{
//---------------------LCD CODE-----------------------------------
//lcd.setCursor(0,0); // motor won't turn with these 4 lines??
//lcd.print("Direction :");
//lcd.setCursor(0,1);
//lcd.print("Speed :");
//-----------------------------------------------------------------
currentSpeedValue = map(analogRead(potPin), 0, 1024, 1, 17);
stepper.setRPM(currentSpeedValue);
if (stepper.isDone() == true) {
digitalWrite(ledPin, LOW);
digitalWrite(ledPin2, LOW);
}
if (cwStatus == true && stepper.isDone() == false) {
digitalWrite(ledPin, HIGH);
}
if (cwStatus == false && stepper.isDone() == false) {
digitalWrite(ledPin2, HIGH);
}
buttonState = digitalRead(buttonPin);
buttonState2 = digitalRead(buttonPin2);
buttonState3 = digitalRead(buttonPin3);
buttonState4 = digitalRead(buttonPin4);
if (stepper.isDone() && rotate1 == false)
{
stepper.setDirection(CW);
cwStatus = true;
stepper.rotate(1);
cwStatus = false;
rotate1 = true;
}
if (buttonState3 == HIGH) {
stepper.setDirection(STOP);
}
if (buttonState == HIGH && stepper.isDone() == true) {
stepper.setDirection(CW);
cwStatus = true;
stepper.rotate();
}
if (buttonState2 == HIGH && stepper.isDone() == true) {
cwStatus = false;
stepper.setDirection(CCW);
stepper.rotate();
}
//this is very important and must be placed in your loop, it is this that makes the motor steps
//when necessary
stepper.run();
}