Stepper motor and TFT lcd

Hello! So, I want to control a stepper motor with a TFT LCD. Basically, the LCD needs to control how many spins and the velocity of the motor. When it comes to the LCD, everything works fine but when I press "START" the motor doesn't work. I don't know where is the error. Can you help me, please?

#include <AccelStepper.h> // biblioteca do motor
#include <LiquidCrystal.h> // biblioteca do LCD
#include <Adafruit_GFX.h>
#include <MCUFRIEND_kbv.h>
MCUFRIEND_kbv tft;      
#include <TouchScreen.h>

const int DIR = 52; // entrada DIR do driver
const int STEP = 53; // entrada STEP do driver

unsigned long volta;
int botaomais=8; //entrada do botão incremento
int botaovolta_mil=45; //entrada botão de incremento 1000 na volta
int botaovolta_cem=49; //entrada botão de incremento 100 na volta
int botaovolta_dez=41; //entrada botão de incremento 10 na volta
int botaoroda=9; //entrada do botão roda
int botaovelocidade=7; //entrada do botão velocidade
unsigned long lap;
unsigned long count=0;
unsigned long volta_atual=0;
unsigned long vel=0;

unsigned long startMillis;
unsigned long currentMillis;
const unsigned long period = 1000;

uint8_t YP = A2;  
uint8_t XM = A1;  
uint8_t YM = 6;   
uint8_t XP = 7;   
uint8_t SwapXY = 0;

uint16_t TS_LEFT = 169;
uint16_t TS_RT  = 902;
uint16_t TS_TOP = 913;
uint16_t TS_BOT = 172;

int16_t BOXSIZE;
int16_t PENRADIUS = 3;
uint16_t identifier, oldcolor, currentcolor;
uint8_t Orientation = 1; 


bool valor_botao1 = 0;

int countvol = 0;
int countvel = 0;


#define InterfaceMotor 1 // Defina o tipo de interface do motor; interface 1 = driver

AccelStepper Stepper(InterfaceMotor, STEP, DIR); // Cria uma instância

TouchScreen ts = TouchScreen(XP, YP, XM, YM, 200);
TSPoint tp;
#define MINPRESSURE 1
#define MAXPRESSURE 10000

#define BLACK   0x0000
#define BLUE    0x001F
#define RED     0xF800
#define GREEN   0x07E0
#define CYAN    0x07FF
#define MAGENTA 0xF81F
#define YELLOW  0xFFE0
#define WHITE   0xFFFF
#define GREY    0x7BEF


void show_tft(void)
{
  tft.drawRoundRect(0, 5, 320, 84, 5, GREY);
  tft.setCursor(40,10);
  tft.setTextSize(3);
  tft.setTextColor(BLACK);
  tft.print("Voltas:");

  tft.setCursor(190,10);
  tft.setTextSize(3);
  tft.setTextColor(BLACK);
  tft.print(count);
  
  tft.fillRoundRect(3, 35, 50, 50, 5, RED);
  tft.setTextColor(WHITE);
  tft.setTextSize(2);
  tft.setCursor(23, 43);
  tft.println("+");
  tft.setCursor(10, 60);
  tft.println("100");
  
  tft.fillRoundRect(56, 35, 50, 50, 5, RED); 
  tft.setCursor(77, 43);
  tft.println("+");
  tft.setCursor(70, 60);
  tft.println("10");
  
  tft.fillRoundRect(109, 35, 50, 50, 5, RED);
  tft.setCursor(130, 43);
  tft.println("+");
  tft.setCursor(130, 60);
  tft.println("1");
  
  tft.fillRoundRect(163, 35, 50, 50, 5, RED);
  tft.setCursor(185, 43);
  tft.println("-");
  tft.setCursor(185, 60);
  tft.println("1");
  
  tft.fillRoundRect(217, 35, 50, 50, 5, RED);
  tft.setCursor(239, 43);
  tft.println("-");
  tft.setCursor(232, 60);
  tft.println("10");
  
  tft.fillRoundRect(270, 35, 48, 50, 5, RED);
  tft.setCursor(293, 43);
  tft.println("-");
  tft.setCursor(278, 60);
  tft.println("100");

  tft.drawRoundRect(0, 92, 320, 82, 5, GREY);
  tft.setCursor(10,95);
  tft.setTextSize(3);
  tft.setTextColor(BLACK);
  tft.print("Velocidade:");

  tft.setCursor(210,95);
  tft.setTextSize(3);
  tft.setTextColor(BLACK);
  tft.print(vel);

  tft.setCursor(266,95);
  tft.setTextSize(3);
  tft.setTextColor(BLACK);
  tft.print("rpm");

  tft.fillRoundRect(56, 120, 50, 50, 5, RED); 
  tft.setTextColor(WHITE);
  tft.setTextSize(2);
  tft.setCursor(77, 128);
  tft.println("+");
  tft.setCursor(70, 145);
  tft.println("10");
  
  tft.fillRoundRect(109, 120, 50, 50, 5, RED);
  tft.setCursor(130, 128);
  tft.println("+");
  tft.setCursor(130, 145);
  tft.println("1");
  
  tft.fillRoundRect(163, 120, 50, 50, 5, RED);
  tft.setCursor(185, 128);
  tft.println("-");
  tft.setCursor(185, 145);
  tft.println("1");
  
  tft.fillRoundRect(217, 120, 50, 50, 5, RED);
  tft.setCursor(239, 128);
  tft.println("-");
  tft.setCursor(232, 145);
  tft.println("10");

  tft.drawRoundRect(0, 177, 320, 57, 5, GREY);
  tft.fillRoundRect(15, 180, 200, 50, 5, RED);
  tft.setTextSize(3);
  tft.setTextColor(WHITE);
  tft.setCursor(69, 195);
  tft.println("START");

  tft.fillRoundRect(230, 180, 70, 50, 5, RED);
  tft.setTextSize(2);
  tft.setTextColor(WHITE);
  tft.setCursor(237, 195);
  tft.println("RESET");
  
}

void setup() {
   
 uint16_t tmp;
  tft.begin(0x9320);
  tft.reset();

  ts = TouchScreen(XP, YP, XM, YM, 300);
  tft.begin(0x9320);
  tft.setRotation(Orientation);
  tft.fillScreen(WHITE);
  show_tft(); 
   
 startMillis = millis();  //tempo inicial do programa

}

//end setup

void loop() {
  
  uint16_t x, y;  
    tp = ts.getPoint();  
    pinMode(XM, OUTPUT);
    pinMode(YP, OUTPUT);
    pinMode(XP, OUTPUT);
    pinMode(YM, OUTPUT);

  currentMillis = millis(); // tempo decorrido desde a inicialização do programa
  
  
  if (tp.z > MINPRESSURE && tp.z < MAXPRESSURE) {
  x=map(tp.y, TS_LEFT, TS_RT, 0, 320);
  y=map(tp.x, TS_TOP, TS_BOT, 0, 240);  
  
  //Ao cliclar no botão mais, ocorre acréscimo +1 na variável "count"
  if( 35 < y  && y < 85 && 3 < x && x < 53){
       tft.fillRoundRect(3, 35, 50, 50, 5, GREEN);
       tft.setTextColor(WHITE);
       tft.setTextSize(2);
       tft.setCursor(23, 43);
       tft.println("+");
       tft.setCursor(10, 60);
       tft.println("100");
       delay(100);
       tft.fillRoundRect(3, 35, 50, 50, 5, RED);
       tft.setTextColor(WHITE);
       tft.setTextSize(2);
       tft.setCursor(23, 43);
       tft.println("+");
       tft.setCursor(10, 60);
       tft.println("100");
       delay(200);
       count+=100;
       tft.fillRoundRect(190, 10, 80, 25, 0, WHITE);
       tft.setCursor(190, 10);
       tft.setTextSize(3);
       tft.setTextColor(BLACK);
       tft.print(count);
       
    }

   if ( 35 < y  && y < 85 && 56 < x && x< 106 ){
       tft.fillRoundRect(56, 35, 50, 50, 5, GREEN);
       tft.setTextColor(WHITE);
       tft.setTextSize(2); 
       tft.setCursor(77, 43);
       tft.println("+");
       tft.setCursor(70, 60);
       tft.println("10"); 
       delay(100);
       tft.fillRoundRect(56, 35, 50, 50, 5, RED); 
       tft.setCursor(77, 43);
       tft.println("+");
       tft.setCursor(70, 60);
       tft.println("10");
       delay(200);
       count+=10;
       tft.fillRoundRect(190, 10, 80, 25, 0, WHITE);
       tft.setCursor(190,10);
       tft.setTextSize(3);
       tft.setTextColor(BLACK);
       tft.print(count);
    }

 if( 35 < y  && y < 85 && x > 109 && x < 159 ){
      tft.fillRoundRect(109, 35, 50, 50, 5, GREEN);
      tft.setTextColor(WHITE);
      tft.setTextSize(2);
      tft.setCursor(130, 43);
      tft.println("+");
      tft.setCursor(130, 60);
      tft.println("1");
      delay(100);
      tft.fillRoundRect(109, 35, 50, 50, 5, RED);
      tft.setCursor(130, 43);
      tft.println("+");
      tft.setCursor(130, 60);
      tft.println("1");
      delay(200);
      count+=1;
      tft.fillRoundRect(190, 10, 80, 25, 0, WHITE);
      tft.setCursor(190,10);
      tft.setTextSize(3);
      tft.setTextColor(BLACK);
      tft.print(count);
    }  

if( 35 < y  && y < 85 && x > 163 && x < 213 ){
      tft.fillRoundRect(163, 35, 50, 50, 5, GREEN);
      tft.setTextColor(WHITE);
      tft.setTextSize(2);
      tft.setCursor(185, 43);
      tft.println("-");
      tft.setCursor(185, 60);
      tft.println("1");
      delay(100);
      tft.fillRoundRect(163, 35, 50, 50, 5, RED);
      tft.setCursor(185, 43);
      tft.println("-");
      tft.setCursor(185, 60);
      tft.println("1");
      delay(200);
      count-=1;
      tft.fillRoundRect(190, 10, 80, 25, 0, WHITE);
      tft.setCursor(190,10);
      tft.setTextSize(3);
      tft.setTextColor(BLACK);
      tft.print(count);
    }

if( 35 < y  && y < 85 && x > 217 && x < 267  ){
      tft.fillRoundRect(217, 35, 50, 50, 5, GREEN);
      tft.setTextColor(WHITE);
      tft.setTextSize(2);
      tft.setCursor(239, 43);
      tft.println("-");
      tft.setCursor(232, 60);
      tft.println("10");
      delay(100);
      tft.fillRoundRect(217, 35, 50, 50, 5, RED);
      tft.setCursor(239, 43);
      tft.println("-");
      tft.setCursor(232, 60);
      tft.println("10");
      delay(200);
      count-=10;
      tft.fillRoundRect(190, 10, 80, 25, 0, WHITE);
      tft.setCursor(190,10);
      tft.setTextSize(3);
      tft.setTextColor(BLACK);
      tft.print(count);
    }

 if( 35 < y  && y < 85 && x > 270){
    tft.fillRoundRect(270, 35, 48, 50, 5, GREEN);
    tft.setTextColor(WHITE);
    tft.setTextSize(2);
    tft.setCursor(293, 43);
    tft.println("-");
    tft.setCursor(278, 60);
    tft.println("100");
    delay(100);
    tft.fillRoundRect(270, 35, 48, 50, 5, RED);
    tft.setCursor(293, 43);
    tft.println("-");
    tft.setCursor(278, 60);
    tft.println("100");
    delay(200);
    count-=100;
    tft.fillRoundRect(190, 10, 80, 25, 0, WHITE);
    tft.setCursor(190,10);
    tft.setTextSize(3);
    tft.setTextColor(BLACK);
    tft.print(count);
   }
 
   if( y>120 && y<170 && x>56 && x<106){
    tft.fillRoundRect(56, 120, 50, 50, 5, GREEN);
    tft.setTextColor(WHITE); 
    tft.setTextColor(WHITE);
    tft.setTextSize(2);
    tft.setCursor(77, 128);
    tft.println("+");
    tft.setCursor(70, 145);
    tft.println("10");
    delay(100);
    tft.fillRoundRect(56, 120, 50, 50, 5, RED); 
    tft.setTextColor(WHITE);
    tft.setTextSize(2);
    tft.setCursor(77, 128);
    tft.println("+");
    tft.setCursor(70, 145);
    tft.println("10");
    delay(200);
    vel+=10;
    tft.fillRoundRect(210, 95, 50, 25, 0, WHITE);
    tft.setCursor(210,95);
    tft.setTextSize(3);
    tft.setTextColor(BLACK);
    tft.print(vel);
    
   }

 if( y>120 && y<170 && x>109 && x<159 ){
    tft.fillRoundRect(109, 120, 50, 50, 5, GREEN);
    tft.setTextColor(WHITE);
    tft.setTextSize(2);
    tft.setCursor(130, 128);
    tft.println("+");
    tft.setCursor(130, 145);
    tft.println("1");
    delay(100);
    tft.fillRoundRect(109, 120, 50, 50, 5, RED);
    tft.setCursor(130, 128);
    tft.println("+");
    tft.setCursor(130, 145);
    tft.println("1");
    delay(200);
    vel+=1;
    tft.fillRoundRect(210, 95, 50, 25, 0, WHITE);
    tft.setCursor(210,95);
    tft.setTextSize(3);
    tft.setTextColor(BLACK);
    tft.print(vel);
   }

if( y>120 && y<170 && x>163 && x<213){
    
    tft.fillRoundRect(163, 120, 50, 50, 5, GREEN);
    tft.setTextColor(WHITE);
    tft.setTextSize(2);
    tft.setCursor(185, 128);
    tft.println("-");
    tft.setCursor(185, 145);
    tft.println("1");
    delay(100);
    tft.fillRoundRect(163, 120, 50, 50, 5, RED);
    tft.setCursor(185, 128);
    tft.println("-");
    tft.setCursor(185, 145);
    tft.println("1");
    delay(200);
    vel-=1;
    tft.fillRoundRect(210, 95, 50, 25, 0, WHITE);
    tft.setCursor(210,95);
    tft.setTextSize(3);
    tft.setTextColor(BLACK);
    tft.print(vel);
  }

if( y>120 && y<170 && x>217 && x<267){
   tft.fillRoundRect(217, 120, 50, 50, 5, GREEN);
   tft.setTextColor(WHITE);
   tft.setTextSize(2);
   tft.setCursor(239, 128);
   tft.println("-");
   tft.setCursor(232, 145);
   tft.println("10");
   delay(100);
   tft.fillRoundRect(217, 120, 50, 50, 5, RED);
   tft.setCursor(239, 128);
   tft.println("-");
   tft.setCursor(232, 145);
   tft.println("10");
   delay(200);
   vel-=10;
   tft.fillRoundRect(210, 95, 50, 25, 0, WHITE);
   tft.setCursor(210,95);
   tft.setTextSize(3);
   tft.setTextColor(BLACK);
   tft.print(vel);
  }


volta=count*200; // volta é o número de passos que o motor irá fazer, logo, multiplica-se o count por 200

if (currentMillis - startMillis >= period)
    {   
      Stepper.setAcceleration(10*(pow(vel,2))); // y=10x²
      Stepper.setSpeed(vel);
      Stepper.setMaxSpeed(vel);
      
//Acionamento do botão roda
  if ( y>180 && y<230 && x> 15 && x< 215 && valor_botao1==0){

          //O motor roda a quantidade de voltas solicitada e mostra quantas voltas faltam para parar  
          for(unsigned long i=0;i<=volta; i++){
            Stepper.runToNewPosition(i);
            
           tft.fillRoundRect(15, 180, 200, 50, 5, GREEN);
           tft.setTextSize(3);
           tft.setTextColor(WHITE);
           tft.setCursor(83, 195);
           tft.println("STOP");
           valor_botao1= !valor_botao1;
          volta_atual =count - (i/200);
          if(volta_atual==0){
          tft.fillRoundRect(15, 180, 200, 50, 5, RED);
          tft.setTextSize(3);
          tft.setTextColor(WHITE);
          tft.setCursor(69, 195);
          tft.println("START");
          valor_botao1= !valor_botao1;
          }
          }
          }
      
        startMillis = currentMillis;
        Stepper.setCurrentPosition(0);
    }

}
}


thank you so much for your help!!

Have you previously made sketches/projects for these devices?

  • the tft control anything, like an LED
  • the motor run, on its own, in a controlled manner.

Yesss, I did. Both of them worked before. The problem starts when I try to make them work together.
I believe the problem is when the LCD sends the comand to the motor.

When one device or the other works on its own, but not together, your power supply is probably not adequate for running all the devices in the system. Would you show a drawing of your system, and data from the devices showing the power required to run each device.

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