Hello all:
I am trying to control a stepper motor using an Arduino UNO. [See attached file]
I also want to control/monitor the speed using and LCD with some built-in buttons:
I noticed that regardless of the method that I use for motor_control, when I use the LCD, arduino start sending inconsistent steps and make the motor to jitter and not run properly.
Once I turn of the code related to the LCD, it works just fine!
I am not sure if it is related to the "Adafruit_RGBLCDShield.h" library and if it messes up with the pins or it is just a timer setting that is not set properly!
Here is the drive in case some one is intrestd:
http://leadshineusa.com/UploadFile/Down/MX3660d_V1.0.pdf
Would appreciate your input!
Here is the code:
// include the library code:
#include <TimerOne.h>
#include <Adafruit_RGBLCDShield.h>
#include <utility/Adafruit_MCP23017.h>
#include <AccelStepper.h>
// The shield uses the I2C SCL and SDA pins. On classic Arduinos
// this is Analog 4 and 5 so you can't use those for analogRead() anymore
// However, you can connect other I2C sensors to the I2C bus and share
// the I2C bus.
Adafruit_RGBLCDShield lcd = Adafruit_RGBLCDShield();
// These #defines make it easy to set the backlight color
AccelStepper stepper(1,7,6);
#define RED 0x1
unsigned int t_sec = 300;
unsigned int Speed = 100;
unsigned int SpeedChange = 100;
unsigned int SpeedFineTune = 10;
unsigned int SpeedLowerTrsh = 10;
unsigned int SpeedUpprtTrsh = 1000;
long pd = 500; // Pulse Delay period
boolean setdir = LOW; // Set Direction
char MotDir = 'L';
int reverseSwitch = 2; // Push button for reverse
uint8_t i = 0;
void setup() {
///////////////////////////////////////////////// Motor Drive and LCD Init /////////////////////////////////////////
lcd.begin(16, 2);
lcd.print("XXXX Tech.");
stepper.setMaxSpeed(1000);
stepper.setSpeed(500);
pinMode(9,OUTPUT);
Timer1.initialize(100); //100us = 10khz
Timer1.pwm(9,512); // 50% DC
}
///////////////////////////////////////////////// Main Loop ////////////////////////////////////////////////////////////
void loop() {
stepper.runSpeed();
//lcd_handler();
}
///////////////////////////////////////////// This is the LCD Functionality part ///////////////////////////////////////
void lcd_handler(){
// set the cursor to column 0, line 1
lcd.setCursor(0, 1);
lcd.print("Speed is: ");
lcd.setCursor(15, 1);
lcd.print(MotDir);
lcd.setCursor(10, 1);
lcd.print(Speed);
uint8_t buttons = lcd.readButtons();
if (buttons) {
lcd.clear();
lcd.setCursor(0,0);
if (buttons & BUTTON_UP) {
if (Speed+SpeedChange<SpeedUpprtTrsh){
Speed += SpeedChange;
delay(t_sec);
}
lcd.print("Increase Speed");
}
if (buttons & BUTTON_DOWN) {
if (Speed > SpeedChange+SpeedLowerTrsh){
Speed -= SpeedChange;
delay(t_sec);
}
lcd.print("Decrease Speed ");
}
if (buttons & BUTTON_LEFT) {
if (Speed > SpeedFineTune+SpeedLowerTrsh){
Speed -= SpeedFineTune;
delay(t_sec);
}
lcd.print("Fine Tune (Low)");
}
if (buttons & BUTTON_RIGHT) {
if (Speed+SpeedFineTune<SpeedUpprtTrsh){
Speed += SpeedFineTune;
delay(t_sec);
}
lcd.print("Fine Tune (Up)");
}
if (buttons & BUTTON_SELECT) {
lcd.print("* No Action *");
}
}
}
