Repetition of loop function does not occur after giving command through LCD HMI

Hi
I am doing a project using stepper motor, Arduino mega and LCD HMI All I want to rotate motor if press on button on LCD as it will take a step and then stop. I have set VP address 1000 and key value 0001 in my GUI. Same values I have stored in buffer locations. first time when I press on button on LCD it works and motor gets on for a step and then it stops but when I again press button motor does not work. I have tried almost all the methods according to my knowledge as m not pro in programming.
Thanks in advance
Code:

#include <Arduino.h>
#include<Stepper.h>
#include <MsTimer2.h>

const int stepsPerRevolution = 200;                       // change this to fit the number of steps per revolution for your motor
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11); 

unsigned char tcount = 0;
unsigned char Buffer[80]; 
unsigned char Buffer_Len = 0;

unsigned char a[7] = {0X5A, 0XA5, 0X07, 0X82, 0X10, 0X00, 0X00
                     };       
void Timer2Interrupt()  
{
  if (tcount > 0)
    tcount--;
}

void setup() {
  Serial.begin(115200);
  Serial1.begin(115200);
  myStepper.setSpeed(50);
  MsTimer2::set(5, Timer2Interrupt);
  MsTimer2::start();
}

void loop() {
  
  if (Serial.available())
  {
    Buffer[Buffer_Len] = Serial.read();
    Buffer_Len++;
    tcount = 5;
  }
  else {
    if (tcount == 0) {
      if (Buffer[0] == 0X5A && Buffer[5]==0X00) {
        if (Buffer[8] == 0X01) {
          
          myStepper.step(100);

          myStepper.setSpeed(0);
        }
      }
     Buffer_Len = 0;

    } 
  }

}

LCDs normally do not have buttons on them. Can you please post a schematic of your circuit. Hand drawn is fine Fritzing is not.

Any variable that is used inside and outside of an ISR must be declared volatile. So that would be

volatile unsigned char tcount = 0;

Have a look at the value of tcount as the program progresses.

sir I have designed button in GUI and i mean that button when i press it does not work. Secondly I have tried
volatile unsigned char tcount = 0; but got nothing.

@lastchancename sir can you plzz explain as m not getting your point.

The declaration of tcount is wrong, but changing it is not necessarily going to make your code work, but not changing it will make it not work when everything else fine.

So how does the GUI communicate with the Arduino, and where is it read inside the code?

This is why I asked for a schematic.

In GUI i have set VP address 1000 and key value 0001 that is stored in location 4 and 5 using this line

unsigned char a[7] = {0X5A, 0XA5, 0X07, 0X82, 0X10, 0X00, 0X00 };

and here i have put the check using this loop

      if (Buffer[0] == 0X5A && Buffer[5]==0X00) {
        if (Buffer[8] == 0X01) {
          
          myStepper.step(100);

          myStepper.setSpeed(0);
        }

and here is schmatic.

Thanks for the schematic.
Now as a Mega has 4 serial ports why are you using port 0? These are connected to the USB to serial chip on the mega and so uploading and serial print commands will also try and talk to your LCD. I would use TX1 and RX1 on pins RX1 19 and TX1 18. You need to switch the reading of your LCD to Serial1.

Now you have shown the TX on your touch LCD to RX on the Arduino. Are you sure that is right? The standard is a bit tricky to interpreter because it depends on what sort of device your LCD is classed as.

It could be you need to connect the TX on the LCD to the TX of the Arduino. You will have to check if the TX or RX is an output. You can measure this with a voltmeter. Disconnect the TX and RX lines from your Arduino and now measure the voltage from your LCD on both pins. The one that measures 5V ( or so ) is the output and this must be connected to the RX on the Arduino.

Next is the way you read the data from the board. Your code will not work as written even if you have the connections correct.

This means if you are say expecting to receive an 8 byte message, as soon as the first byte arrives the code reads that and the loop finishes setting the Buffer_Len variable to zero, which next time round the loop puts the next byte into the first position of the buffer again.

If you know how many bytes you are expecting then just wait until they are all in before trying to read them.
So for 8 bytes that would be

if (Serial.available() >= 8) 

Finally I am not sure why you are using a Timer2Interrupt because you never seem to set up what time you are using. Anyway this is a silly way to do things because you should be using the system millis() or micros() to do your timing.

Also, you never clear Buffer, and once you set the stepper speed to 0 you never change it again.

@david_2018 once i have changed the stepper speed then motor works like it just start moving to never get stop. i have also used

myStepper.step(0);

but it also does not stop after taking step 100

@david_2018 and for clearing the buffer i have used

 Buffer_Len = 0;

@Grumpy_Mike thanks sir I have made changings like as I got my problem as I was not clearing each and every location to get into serial. Available so I have used a for loop to clear all locations and defining buffer length again as 0. Secondly I used timer function to get interrupt. here is my final code.



#include <Arduino.h>
#include<Stepper.h>
#include <MsTimer2.h>

const int stepsPerRevolution = 200;                      
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11); 

unsigned char tcount = 0;
unsigned char Buffer[80]; 
unsigned char Buffer_Len = 0;

unsigned char a[8] = {0X5A, 0XA5, 0X05, 0X82, 0X10, 0X00, 0X00};       

void Timer2Interrupt()  
{
  if (tcount > 0)
  tcount--;
}

void setup() {
  Serial.begin(115200);
  Serial2.begin(115200);
  myStepper.setSpeed(50);
  MsTimer2::set(8, Timer2Interrupt);
  MsTimer2::start();
}

void loop() {
  
  if (Serial2.available())
  {
    Buffer[Buffer_Len] = Serial2.read();
    Buffer_Len++;
    tcount = 5;
    
  }
  else {
    if (tcount == 0) {

      if (Buffer[0] == 0X5A && Buffer[5]==0X00) {

        if (Buffer[8] == 0X01) {

          myStepper.step(100);
          
          myStepper.setSpeed(0);
        }
      }

          for(int j=0;j<8;j++)
          {
            Buffer[j] = 0;
            myStepper.setSpeed(50);
          }
      Buffer_Len = 0;   
    }
  }

}

Thanks alot

@lastchancename @david_2018 Thanks sir for your help I got solution of my problem