Hello all,
My hardware consists of an Arduino Mega 2560 with TFT LCD Shield and an ILI9341 Touch Screen.
I am trying to control the speed and direction of a Nema 34 stepper motor with a CL86T driver through the touch screen interface.
I have successfully tested it using some example programs with Stepper library. I tried this setup with the AccelStepper library and was not able to get the motor to run properly. I have implemented the Stepper library into my below program and I was looking for some insight on to why this code is not turning on the stepper motor. I feel like I need to set the state of the last button push some way, but I am a little unclear on how to implement that feature in this scenario. Thanks for the help.
#include <UTFT.h>
#include <URTouch.h>
#include <Stepper.h>
#include <HX711.h>
// Define Load Cell connections
#define tarePin 11 // Clears the scale
#define LOADCELL_DOUT_PIN 12 // Data pin
#define LOADCELL_SCK_PIN 13 // Clock pin
// Define MAX6675 pin connections
//#define thermoSO 9
//#define thermoCS 10
//#define thermoSCK 11
//#define ssrPin 12
//==== Creating Objects ================================================================================
UTFT myGLCD(ITDB32S_V2,38,39,40,41); //Parameters should be adjusted to your Display/Schield model
URTouch myTouch( 6, 5, 4, 3, 2);
Stepper myStepper(1, 9, 8);
HX711 scale;
//MAX6675 thermocouple(thermoSCK, thermoCS, thermoSO);
//==== Touch Screen ===================================================================================
extern uint8_t SmallFont[];
extern uint8_t BigFont[];
extern uint8_t SevenSegNumFont[];
int x, y;
char currentPage;
// ======= Stepper Motor =============================================================================
const int stepsPerRevolution = 800;
// ======= Load Cell =================================================================================
// Calibration factor (this needs to be adjusted for your setup)
float calibration_factor = 1300.0; // This value is specific to your load cell and HX711
// ======= Heat ======================================================================================
void setup() {
// Initial setup
myGLCD.InitLCD();
myGLCD.clrScr();
myTouch.InitTouch(1);
myTouch.setPrecision(PREC_MEDIUM);
drawHomeScreen(); // Draws the Home Screen
currentPage = '0'; // Indicates that we are at Home Screen
Serial.begin(9600);
}
void loop() {
// ====== Home Screen =============================================================================
if (currentPage == '0') {
if (myTouch.dataAvailable()) {
myTouch.read();
x=myTouch.getX(); // X coordinate where the screen has been pressed
y=myTouch.getY(); // Y coordinates where the screen has been pressed
// If we press the Motor Control Button
if ((x>=35) && (x<=285) && (y>=50) && (y<=90)) {
drawFrame(35, 50, 285, 90); // Custom Function -Highlighs the buttons when it's pressed
currentPage = '1'; // Indicates that we are the first example
myGLCD.clrScr(); // Clears the screen
drawMotorControl(); // It is called only once, because in the next iteration of the loop, this above if statement will be false so this funtion won't be called. This function will draw the graphics of the first example.
}
// If we press the Load Cell Button
if ((x>=35) && (x<=285) && (y>=140) && (y<=180)) {
drawFrame(35, 140, 285, 180);
currentPage = '2';
myGLCD.clrScr();
drawLoadCell();
}
// If we press the Heat Control Button
if ((x>=35) && (x<=285) && (y>=190) && (y<=230)) {
drawFrame(35, 190, 285, 230);
currentPage = '3';
myGLCD.clrScr();
drawHeatControl();
}
}
}
// ====== Motor Control Page ======================================================================
if (currentPage == '1') {
if (myTouch.dataAvailable()) {
myTouch.read();
x=myTouch.getX();
y=myTouch.getY();
/* // If we move the RPM slider
if( (y>=90) && (y<=110)) {
if (x<=75) { // Confines the area of the slider to be above 38 pixels
x=75;
}
if (x>=310){ /// Confines the area of the slider to be under 310 pixels
x=310;
}
}
// Maps the values of the X - Axis from 38 to 0 and 310 to 1000. 0-1000 is the motors speed range
int motorSpeed = 500;*/
/*// Draws the positioners
myGLCD.setColor(255, 255, 255);
myGLCD.fillRect(x,80,(x+4),147); // Positioner
myGLCD.setColor(motorSpeed, 0, 0);
myGLCD.fillRect(75, 80, (x-1), 120);
myGLCD.setColor(0, 0, 0);
myGLCD.fillRect((x+5), 80, 309, 1150);*/
// If we press the CW Button
if ((x>=10) && (x<=310) &&(y>=90) && (y<=163)) {
drawFrame(10, 135, 310, 163);
myStepper.setSpeed(100);
myStepper.step(stepsPerRevolution/100);
Serial.println("cw");
}
// If we press the CCW Button
if ((x>=10) && (x<=310) &&(y>=173) && (y<=201)) {
drawFrame(10, 173, 310, 201);
//myStepper.setSpeed(100);
//myStepper.step(-stepsPerRevolution/100);
Serial.println("ccw");
}
// If we press the Back Button
if ((x>=10) && (x<=310) &&(y>=10) && (y<=36)) {
drawFrame(10, 10, 310, 36);
currentPage = '0'; // Indicates we are at home screen
myGLCD.clrScr();
drawHomeScreen(); // Draws the home screen
}
}
}