UTFT, URTouch and Stepper motor control

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
        }
      }
  }
  

Here is the whole program:

#include <UTFT.h> 
#include <URTouch.h>
#include <Stepper.h>
#include <HX711.h>

// Define Stepper Motor Connections
#define dir 8
#define pul 9

// 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, pul, dir);
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 = map(x, ) ;*/
        
  
  /*// 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(motorSpeed);
          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
        }
      }
  }
  
  //======= Load Cell Page ========================================================================== 
  if (currentPage == '2') {
    if (myTouch.dataAvailable()) {
        myTouch.read();
        x=myTouch.getX();
        y=myTouch.getY();
             
        //Back button
        if ((x>=10) && (x<=310) &&(y>=10) && (y<=36)) {
          drawFrame(10, 10, 310, 36);
          currentPage = '0';
          myGLCD.clrScr();
          drawHomeScreen();
        }
    }
  }

  
// ===== Heat Control Page =======================================================================
  if (currentPage == '3') {
    if (myTouch.dataAvailable()) {
        myTouch.read();
        x=myTouch.getX();
        y=myTouch.getY();
        
        //Back button
        if ((x>=10) && (x<=310) &&(y>=10) && (y<=36)) {
          drawFrame(10, 10, 310, 36);
          currentPage = '0';
          myGLCD.clrScr();
          drawHomeScreen();
        }
    }
  }
}
 
// ====== Custom Functions =========================================================================

// drawHomeScreen - Custom Function
void drawHomeScreen() {
  // Title
  myGLCD.setBackColor(0,0,0); // Sets the background color of the area where the text will be printed to black
  myGLCD.setColor(255, 255, 255); // Sets color to white
  myGLCD.setFont(BigFont); // Sets font to big
  myGLCD.setColor(255, 255, 255); // Sets color to white
  myGLCD.setFont(SmallFont); // Sets the font to small
  myGLCD.print("Alden Dynamics", CENTER, 220); // Prints the string
  //myGLCD.setFont(BigFont);
  //myGLCD.print("Select Example", CENTER, 64);
  
  // Button - Motor Control
  myGLCD.setColor(0, 128, 128); // Sets green color
  myGLCD.fillRoundRect (35, 50, 285, 90); // Draws filled rounded rectangle
  myGLCD.setColor(255, 255, 255); // Sets color to white
  myGLCD.drawRoundRect (35, 50, 285, 90); // Draws rounded rectangle without a fill, so the overall appearance of the button looks like it has a frame
  myGLCD.setFont(BigFont); // Sets the font to big
  myGLCD.setBackColor(0, 128, 128); // Sets the background color of the area where the text will be printed to green, same as the button
  myGLCD.print("Motor", CENTER, 62); // Prints the string


  // Button - Compression Pressure
  myGLCD.setColor(0, 128, 128);
  myGLCD.fillRoundRect (35, 140, 285, 180);
  myGLCD.setColor(255, 255, 255);
  myGLCD.drawRoundRect (35, 140, 285, 180);
  myGLCD.setFont(BigFont);
  myGLCD.setBackColor(0, 128, 128);
  myGLCD.print("Compression", CENTER, 152);

  // Button - Heat
  myGLCD.setColor(0, 128, 128);
  myGLCD.fillRoundRect (35, 190, 285, 230);
  myGLCD.setColor(255, 255, 255);
  myGLCD.drawRoundRect (35, 190, 285, 230);
  myGLCD.setFont(BigFont);
  myGLCD.setBackColor(0, 128, 128);
  myGLCD.print("Heat", CENTER, 202);
}

// Highlights the button when pressed
void drawFrame(int x1, int y1, int x2, int y2) {
  myGLCD.setColor(255, 165, 0);
  myGLCD.drawRoundRect (x1, y1, x2, y2);
  while (myTouch.dataAvailable())
    myTouch.read();
    myGLCD.setColor(255, 255, 255);
    myGLCD.drawRoundRect (x1, y1, x2, y2);
}
//====================================================
void drawMotorControl() {
  myGLCD.setColor(0, 0, 0);
  myGLCD.fillRoundRect (10, 10, 310, 36);
  myGLCD.setColor(255, 255, 255);
  myGLCD.drawRoundRect (10, 10, 310, 36);
  myGLCD.setFont(BigFont);
  myGLCD.setBackColor(0, 0, 0);
  myGLCD.print("<-", 18, 15);
  myGLCD.setBackColor(0, 0, 0);
  myGLCD.setFont(SmallFont);
  myGLCD.print("Back", CENTER, 18);
  myGLCD.setFont(BigFont);
  myGLCD.print("Motor Control", CENTER, 50);
  myGLCD.setColor(0, 128, 128);
  myGLCD.drawLine(0,70,319,70);
  myGLCD.setBackColor(0, 0, 0);
  myGLCD.setFont(BigFont);
  myGLCD.print("RPM:", 10, 100);
  //myGLCD.drawRect(75, 100, 310, 110); // RPM slider
  myGLCD.setColor(0, 128, 128);
  myGLCD.fillRoundRect (10, 135, 310, 163);
  myGLCD.setColor(255, 255, 255);
  myGLCD.drawRoundRect (10, 135, 310, 163);
  myGLCD.setBackColor(0, 128, 128);
  myGLCD.setColor(255, 255, 255);
  myGLCD.print("cw", CENTER, 140);
  myGLCD.setColor(0, 128, 128);
  myGLCD.fillRoundRect (10, 173, 310, 201);
  myGLCD.setColor(255, 255, 255);
  myGLCD.drawRoundRect (10, 173, 310, 201);
  myGLCD.setBackColor(0, 128, 128);
  myGLCD.setColor(255, 255, 255);
  myGLCD.print("ccw", CENTER, 180);
  myGLCD.setBackColor(0, 0, 0);
  myGLCD.setFont(SmallFont);
  myGLCD.print("Alden Dynamics", CENTER, 220);  
}

//===== set Motor Direction and Speed based on button push and slider position==============
/*void setMotorCW(int RPM) {
  mystepper.setSpeed(RPM);
  mystepper.runSpeed();
  }

void setMotorCCW(int RPM) {
  mystepper.setSpeed(RPM);
  -mystepper.runSpeed();
}*/


//====================================================
void drawLoadCell() {
  myGLCD.setColor(0, 0, 0);
  myGLCD.fillRoundRect (10, 10, 310, 36);
  myGLCD.setColor(255, 255, 255);
  myGLCD.drawRoundRect (10, 10, 310, 36);
  myGLCD.setFont(BigFont);
  myGLCD.setBackColor(0, 0, 0);
  myGLCD.print("<-", 18, 15);
  myGLCD.setBackColor(0, 0, 0);
  myGLCD.setFont(SmallFont);
  myGLCD.print("Back", CENTER, 18);
  myGLCD.setFont(BigFont);
  myGLCD.print("Compression", CENTER, 50);
  myGLCD.print("PSI:", 10, 95);
  myGLCD.setColor(255, 0, 0);
  myGLCD.drawLine(0,75,319,75); 
  myGLCD.setColor(255, 255, 255);
  myGLCD.drawRect(30, 138, 310, 148); // R - Slider
  myGLCD.drawRect(30, 178, 310, 188);
  myGLCD.drawRect(30, 218, 310, 228);  
}
//====================================================
//============= getPressure() - Returns the load cell reading
void getPressure() {
  //PSI=scale.get_units();
  if (myTouch.dataAvailable()) {
    myTouch.read();
    x=myTouch.getX();
    y=myTouch.getY();
    scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
     // Optionally, set the gain factor (128, 64, or 32)
    scale.set_gain(128); // Most common gain setting
    scale.set_scale();
    scale.tare();  
    scale.set_scale(calibration_factor);  
    myGLCD.setFont(SevenSegNumFont);
    myGLCD.setColor(0, 255, 0);
    myGLCD.setBackColor(0, 0, 0);
    //myGLCD.printNumI(PSI,130, 145, 3,'0');
    myGLCD.setFont(BigFont);
    myGLCD.print("PSI", 235, 178); 
    }
 }

 
//====================================================
void drawHeatControl() {
  myGLCD.setColor(0, 0, 0);
  myGLCD.fillRoundRect (10, 10, 310, 36);
  myGLCD.setColor(255, 255, 255);
  myGLCD.drawRoundRect (10, 10, 310, 36);
  myGLCD.setFont(BigFont);
  myGLCD.setBackColor(0,0, 0);
  myGLCD.print("<-", 18, 15);
  myGLCD.setBackColor(0, 0, 0);
  myGLCD.setFont(SmallFont);
  myGLCD.print("Back", CENTER, 18);
  myGLCD.setFont(BigFont);
  myGLCD.print("Temperature Control", CENTER, 65);
  myGLCD.print("Set Temp", 10, 135);
  myGLCD.print("Current Temp", 10, 175);
  myGLCD.setColor(255, 0, 0);
  myGLCD.drawLine(0,80,319,80); 
  myGLCD.setColor(255, 255, 255);
  myGLCD.drawRect(30, 138, 310, 148); // Set Temp SLider
  myGLCD.drawRect(30, 178, 310, 188); // Current Temp 

accelstepper has blocking functions that you call only once and then the stepper-motor runs until target-position is reached. But as long as the stepper-motor is movng you can't do anything else

then there are non-blocking functions that only work with a fast running loop where you call stepper.run() all the time at a higher frequency as the step-pulses must occur.

Thanks for the info. I have made some significant progress, but I feel that I am running into a blocking function that I have not been able to handle properly.
readScale() and updateDisplayValues() functions seem to be the issue. When implemented the stepper motor moves in small pulses as it iterates through the loop. If I comment out the sections, the motor runs continuously as should. Are there any workarounds or methods to resolve this bug and update my display readings without interrupting the the motor functionality?

#include <UTFT.h>
#include <URTouch.h>
#include <AccelStepper.h>
#include <HX711.h>
#include <math.h>

// Define Stepper Motor Connections
#define stepperPul 8  // Pulse Pin
#define stepperDir 9  // Direction Pin

// Define HX711 Load Cell connections
#define loadCellDOUT 10  // Data pin
#define loadCellSCK 11   // Clock pin

//==== Creating Objects ================================================================================

UTFT myGLCD(ITDB32S_V2, 38, 39, 40, 41);  //Parameters should be adjusted to the Display/Schield model
URTouch myTouch(6, 5, 4, 3, 2);
AccelStepper myStepper(AccelStepper::FULL2WIRE, stepperPul, stepperDir);  // Driver, digital i/o pins (pulse, direction)
HX711 scale;

//==== 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;  // This value is specific to the motor and driver setup
const int maxSpeed = 150000;
const int fwdSpeed = 9000;
const int revSpeed = -9000;
bool latched = false;
int xR = 38;
int xRC = map(xR, 38, 310, 0, 9000);

//======= Load Cell =================================================================================
float calibration_factor = 4790.00;  // This value is specific to the load cell and HX711
float cachedForce = 0;
float cachedPressure = 0;

void setup() {

  // Initialize LCD & Touch
  myGLCD.InitLCD();
  myGLCD.clrScr();
  myTouch.InitTouch(1);
  myTouch.setPrecision(PREC_MEDIUM);

  // Initialize home screen
  drawHomeScreen();   // Draws the Home Screen
  currentPage = '0';  // Indicates that we are at Home Screen

  // Initialize motor
  myStepper.setMaxSpeed(maxSpeed);

  // Initalize load cell
  scale.begin(loadCellDOUT, loadCellSCK);
  scale.set_scale(calibration_factor);  // calibrated to 10 lbs
  scale.tare();
}

void loop() {

  // ====== Main Paige =============================================================================
  if (currentPage == '0') {
    myGLCD.setColor(255, 255, 255);
    readScale();
    updateDisplayValues();

    if (latched) {
      myStepper.runSpeed();
    }

    if (myTouch.dataAvailable()) {
      myTouch.read();
      int x = myTouch.getX();  // X coordinate where the screen has been pressed
      int y = myTouch.getY();  // Y coordinates where the screen has been pressed

      // If we press the TARE Button
      if ((x >= 8) && (x <= 210) && (y >= 60) && (y <= 80)) {
        drawFrame(8, 60, 210, 80);
        scale.tare();
      }

      // If we press the SETTINGS Button
      if ((x >= 211) && (x <= 310) && (y >= 60) && (y <= 80)) {
        drawFrame(211, 60, 310, 80);
        currentPage = '1';
        myGLCD.clrScr();
        drawSettingsScreen();
      }

      // Motor RPM Slider
      if ((y >= 130) && (y <= 156)) {
        xR = x;          // Stores the X value where the screen has been pressed in to variable xR
        if (xR <= 38) {  // Confines the area of the slider to be above 38 pixels
          xR = 38;
        }
        if (xR >= 303) {  /// Confines the area of the slider to be under 310 pixels
          xR = 303;
        }
      }

      // If we press the FWD Button
      if ((x >= 8) && (x <= 108) && (y >= 160) && (y <= 220) && !latched) {
        latched = true;
        drawFrame(8, 160, 108, 220);
        myStepper.setSpeed(fwdSpeed);
        myStepper.runSpeed();
      }

      // If we press the REV Button
      if ((x >= 109) && (x <= 209) && (y >= 160) && (y <= 220) && !latched) {
        latched = true;
        drawFrame(109, 160, 209, 220);
        myStepper.setSpeed(revSpeed);
        myStepper.runSpeed();
      }

      // If we press the STOP Button
      if ((x >= 210) && (x <= 310) && (y >= 160) && (y <= 220)) {
        latched = false;
        drawFrame(210, 160, 310, 220);
      }
    }
  }

  // ====== Settings Page =============================================================================
  if (currentPage == '1') {
    myGLCD.setColor(255, 255, 255);
    readScale();
    updateDisplayValues();

    if (myTouch.dataAvailable()) {
      myTouch.read();
      int x = myTouch.getX();  // X coordinate where the screen has been pressed
      int y = myTouch.getY();  // Y coordinates where the screen has been pressed

      // If we press the CALIBRATION Button
      if ((x >= 8) && (x <= 210) && (y >= 100) && (y <= 120)) {
        drawFrame(8, 100, 210, 120);
        currentPage = '2';
        myGLCD.clrScr();
        drawLoadCellCalibrationScreen();
      }

      // If we press the BACK Button
      if ((x >= 211) && (x <= 310) && (y >= 60) && (y <= 80)) {
        drawFrame(211, 60, 310, 80);
        currentPage = '0';
        myGLCD.clrScr();
        drawHomeScreen();
      }
    }
  }

  // ====== Load Cell Calibration Page =============================================================================
  if (currentPage == '2') {
    myGLCD.setColor(255, 255, 255);
    readScale();
    updateDisplayValues();

    if (myTouch.dataAvailable()) {
      myTouch.read();
      int x = myTouch.getX();  // X coordinate where the screen has been pressed
      int y = myTouch.getY();  // Y coordinates where the screen has been pressed

      // If we press the BACK Button
      if ((x >= 211) && (x <= 310) && (y >= 60) && (y <= 80)) {
        drawFrame(211, 60, 310, 80);
        currentPage = '1';
        myGLCD.clrScr();
        drawSettingsScreen();
      }
    }
  }
}

// ====== Main Page Styling =========================================================================

// drawHomeScreen - Custom Function
void drawHomeScreen() {

  myGLCD.setBackColor(0, 0, 0);
  myGLCD.setFont(BigFont);
  myGLCD.setColor(0, 128, 128);
  myGLCD.drawLine(210, 0, 210, 60);

  myGLCD.setFont(SmallFont);
  myGLCD.setColor(255, 255, 255);
  myGLCD.print("Compression", 60, 1);

  // Tare Button
  myGLCD.setFont(BigFont);
  myGLCD.setColor(0, 128, 128);
  myGLCD.fillRoundRect(8, 60, 210, 80);
  myGLCD.setColor(255, 255, 255);
  myGLCD.drawRoundRect(8, 60, 210, 80);
  myGLCD.setBackColor(0, 128, 128);
  myGLCD.setColor(255, 255, 255);
  myGLCD.print("Tare", 75, 63);

  // Settings Button
  myGLCD.setFont(SmallFont);
  myGLCD.setColor(128, 128, 128);
  myGLCD.fillRoundRect(211, 60, 310, 80);
  myGLCD.setColor(255, 255, 255);
  myGLCD.drawRoundRect(211, 60, 310, 80);
  myGLCD.setBackColor(128, 128, 128);
  myGLCD.setColor(0, 0, 0);
  myGLCD.print("Settings", 230, 63);

  // Motor RPM Slider
  myGLCD.setColor(255, 255, 255);
  myGLCD.fillRect(xR, 139, (xR + 4), 147);  // Positioner
  myGLCD.setColor(xRC, 0, 0);
  myGLCD.fillRect(31, 139, (xR - 1), 147);
  myGLCD.setColor(0, 0, 0);
  myGLCD.fillRect((xR + 5), 139, 309, 147);

  // Fwd Button
  myGLCD.setFont(BigFont);
  myGLCD.setColor(0, 175, 128);
  myGLCD.fillRoundRect(8, 160, 108, 220);
  myGLCD.setColor(255, 255, 255);
  myGLCD.drawRoundRect(8, 160, 108, 220);
  myGLCD.setBackColor(0, 175, 128);
  myGLCD.setColor(255, 255, 255);
  myGLCD.print("Fwd", 32, 185);

  // Rev Button
  myGLCD.setColor(0, 175, 128);
  myGLCD.fillRoundRect(109, 160, 209, 220);
  myGLCD.setColor(255, 255, 255);
  myGLCD.drawRoundRect(109, 160, 209, 220);
  myGLCD.setBackColor(0, 175, 128);
  myGLCD.setColor(255, 255, 255);
  myGLCD.print("Rev", CENTER, 185);

  // Stop Button
  myGLCD.setColor(255, 0, 0);
  myGLCD.fillRoundRect(210, 160, 310, 220);
  myGLCD.setColor(255, 255, 255);
  myGLCD.drawRoundRect(210, 160, 310, 220);
  myGLCD.setBackColor(255, 0, 0);
  myGLCD.setColor(255, 255, 255);
  myGLCD.print("X", 252, 185);

  // displays website at bottom of page
  myGLCD.setBackColor(0, 0, 0);
  myGLCD.setFont(SmallFont);
  myGLCD.print("www.aldendynamics.com", CENTER, 225);
}

// ====== Settings Page Styling =========================================================================

void drawSettingsScreen() {

  myGLCD.setBackColor(0, 0, 0);
  myGLCD.setFont(BigFont);
  myGLCD.setColor(0, 128, 128);
  myGLCD.drawLine(210, 0, 210, 60);

  myGLCD.setFont(SmallFont);
  myGLCD.setColor(255, 255, 255);
  myGLCD.print("Compression", 60, 1);

  // Tare Button
  myGLCD.setFont(BigFont);
  myGLCD.setColor(0, 128, 128);
  myGLCD.fillRoundRect(8, 60, 210, 80);
  myGLCD.setColor(255, 255, 255);
  myGLCD.drawRoundRect(8, 60, 210, 80);
  myGLCD.setBackColor(0, 128, 128);
  myGLCD.setColor(255, 255, 255);
  myGLCD.print("Tare", 75, 63);

  // Calibration Button
  myGLCD.setFont(SmallFont);
  myGLCD.setColor(128, 128, 128);
  myGLCD.fillRoundRect(8, 100, 210, 120);
  myGLCD.setColor(255, 255, 255);
  myGLCD.drawRoundRect(8, 100, 210, 120);
  myGLCD.setBackColor(128, 128, 128);
  myGLCD.setColor(255, 255, 255);
  myGLCD.print("Load Cell Calibration", 25, 104);

  // Back Button
  myGLCD.setFont(SmallFont);
  myGLCD.setColor(128, 128, 128);
  myGLCD.fillRoundRect(211, 60, 310, 80);
  myGLCD.setColor(255, 255, 255);
  myGLCD.drawRoundRect(211, 60, 310, 80);
  myGLCD.setBackColor(128, 128, 128);
  myGLCD.setColor(0, 0, 0);
  myGLCD.print("Back", 246, 64);

  // Displays website at bottom of page
  myGLCD.setBackColor(0, 0, 0);
  myGLCD.setFont(SmallFont);
  myGLCD.print("www.aldendynamics.com", CENTER, 225);
}

// ====== Load Cell Calibration Page Styling =========================================================================

void drawLoadCellCalibrationScreen() {

  myGLCD.setBackColor(0, 0, 0);
  myGLCD.setFont(BigFont);
  myGLCD.setColor(0, 128, 128);
  myGLCD.drawLine(210, 0, 210, 60);

  myGLCD.setFont(SmallFont);
  myGLCD.setColor(255, 255, 255);
  myGLCD.print("Compression", 60, 1);

  // Tare Button
  myGLCD.setFont(BigFont);
  myGLCD.setColor(0, 128, 128);
  myGLCD.fillRoundRect(8, 60, 210, 80);
  myGLCD.setColor(255, 255, 255);
  myGLCD.drawRoundRect(8, 60, 210, 80);
  myGLCD.setBackColor(0, 128, 128);
  myGLCD.setColor(255, 255, 255);
  myGLCD.print("Tare", 75, 63);

  // Back Button
  myGLCD.setFont(SmallFont);
  myGLCD.setColor(128, 128, 128);
  myGLCD.fillRoundRect(211, 60, 310, 80);
  myGLCD.setColor(255, 255, 255);
  myGLCD.drawRoundRect(211, 60, 310, 80);
  myGLCD.setBackColor(128, 128, 128);
  myGLCD.setColor(0, 0, 0);
  myGLCD.print("Back", 246, 64);

  // Displays website at bottom of page
  myGLCD.setBackColor(0, 0, 0);
  myGLCD.setFont(SmallFont);
  myGLCD.print("www.aldendynamics.com", CENTER, 225);
}

// ====== Custom Functions =========================================================================

void setMotorSpeed() {
}

void readScale() {

  static unsigned long lastScaleReadTime = 0;
  const unsigned long scaleReadInterval = 500;  // Read scale every 100ms
  if (millis() - lastScaleReadTime < scaleReadInterval) {
    return;  // Exit if not time to read scale
  }
  lastScaleReadTime = millis();
  cachedForce = scale.get_units();  // Read and cache the value
  cachedPressure = (cachedForce / 12);
}

void updateDisplayValues() {

  unsigned long lastUpdateTime = 0;
  const unsigned long updateInterval = 500;  // Update every xxx ms

  if (millis() - lastUpdateTime < updateInterval) {
    return;  // Exit if not yet time to update
  }
  lastUpdateTime = millis();

  myGLCD.setFont(BigFont);
  myGLCD.printNumI(fwdSpeed, 227, 13);
  myGLCD.print("rpm", 234, 36);
  myGLCD.printNumI(cachedForce, 33, 13, 1);
  myGLCD.print("lbf", 20, 36);
  myGLCD.printNumI(cachedPressure, CENTER, 13, 1);
  myGLCD.print("psi", CENTER, 36);
}

// Highlights the button when pressed
void drawFrame(int x1, int y1, int x2, int y2) {
  
  myGLCD.setColor(255, 165, 0);
  myGLCD.drawRoundRect(x1, y1, x2, y2);
  while (myTouch.dataAvailable())
  myTouch.read();
  myGLCD.setColor(255, 255, 255);
  myGLCD.drawRoundRect(x1, y1, x2, y2);
}

//===================================================================================

The solution is to use a different library than accelStepper.
As written above accelStepper is either blocking or requiring very short loop-iteration times.

There is a different stepper-Motor-library that creates the step-pulses "in the background" based on a timer-interrupt.

This timer-interrupt is able to - as the name says - interrupt your main code totally regardless of what part of your main code is executed right in the interrupting moment
create the step-pulse and main code continues 50 microseconds later.

This library is called MobaTools.

You can download it with the library-manager of the arduino-IDE

Thanks @StefanL38 . That was the ticket.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.