Hi,
So I'm writing code that will move a stepper motor a given amount. I am using a easy driver (EasyDriver - Stepper Motor Driver - ROB-12779 - SparkFun Electronics), a working four wire stepper motor, and an Arduino Uno. I am also using an 16 pin LCD screen. All these components work because I am able to control the stepper motor or write text to the LCD using various other code.
Goal:
Here's what I want the buttons to do. I also want the current step count to be reflected on the LCD.
I have 5 buttons.
Button 1: +1 step
Button 2: -1 step
Button 3: +10 steps
Button 4: -10 steps
Button 5. Actually move stepper.
Problem:
Buttons 1 through 4 do work. Currently I can make the number go up or down at will. The Uno also registers button 5 has been pressed. However, when I hold button 5, the stepper motor does not move. I don't know what my problem is. I'll copy paste my code below, I've also attached it.
I realize I'm asking alot but I would really appreciate your help with this, I'm pretty much stuck.
Thanks,
Brian
#include <LiquidCrystal.h>
LiquidCrystal lcd(12,11,5,4,3,2);
int switchState1 = 0;
int switchState2 = 0;
int switchState3 = 0;
int switchState4 = 0;
int switchState5 = 0;
int prevSwitchState1 = 0;
int prevSwitchState2 = 0;
int prevSwitchState3 = 0;
int prevSwitchState4 = 0;
int prevSwitchState5 = 0;
int stepdis = 0;
int dis = 0;
const int switchPin1 = 6;
const int switchPin2 = 7;
const int switchPin3 = 8;
const int switchPin4 = 9;
const int switchPin5 = 10;
void setup(){
lcd.begin(16,2);
pinMode(switchPin1,INPUT);
pinMode(switchPin2,INPUT);
pinMode(switchPin3,INPUT);
pinMode(switchPin4,INPUT);
pinMode(switchPin5,INPUT);
lcd.print("Measurement Sys");
lcd.setCursor(0,1);
lcd.print("1.0");
pinMode(0, OUTPUT);
pinMode(1, OUTPUT);
digitalWrite(0, LOW);
digitalWrite(1, LOW);
}
void loop() {
switchState1 = digitalRead(switchPin1);
switchState2 = digitalRead(switchPin2);
switchState3 = digitalRead(switchPin3);
switchState4 = digitalRead(switchPin4);
switchState5 = digitalRead(switchPin5);
if (switchState1 != prevSwitchState1) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Steps");
lcd.setCursor(0, 1);
lcd.print(dis);
if (switchState1 == HIGH) {
int ndis = dis + 1;
dis = ndis;
}
}
if (switchState2 != prevSwitchState2) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Steps");
lcd.setCursor(0, 1);
lcd.print(dis);
if (switchState2 == HIGH) {
int ndis = dis - 1;
dis = ndis;
}
}
if (switchState3 != prevSwitchState3) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Steps");
lcd.setCursor(0, 1);
lcd.print(dis);
if (switchState3 == HIGH) {
int ndis = dis + 10;
dis = ndis;
}
}
if (switchState4 != prevSwitchState4) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Steps");
lcd.setCursor(0, 1);
lcd.print(dis);
if (switchState4 == HIGH) {
int ndis = dis - 10;
dis = ndis;
}
}
if (switchState5 != prevSwitchState5) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Moving...");
lcd.setCursor(0, 1);
lcd.print(dis);
if (switchState5 == HIGH) {
digitalWrite(1, HIGH);
delayMicroseconds(100);
digitalWrite(1, LOW);
delayMicroseconds(100);
stepdis = stepdis + 1;
if (stepdis == dis)
{
digitalWrite(0,LOW);
digitalWrite(1,LOW);
}
}
}
prevSwitchState1 = switchState1;
prevSwitchState2 = switchState2;
prevSwitchState3 = switchState3;
prevSwitchState4 = switchState4;
prevSwitchState5 = switchState5;
}
lcdmeasurement.ino (3.72 KB)