Nano - Nextion - Stepper

Hi

I am using an Arduino Nano to display/input data to/from Nextion HMI and control a stepper motor (without any library) using a proximity sensor.

The Nextion, displays/gets parameters e.g. steps to rotate the stepper.
The sensor tells when to rotate.
The stepper is rotated with a for-next loop for number of steps declared thru Nextion.

All my stuff is working as planned except that Nextion slows down the stepper. If I run my sketch without the Nextion portion, the stepper works fine.

I am pretty sure that there is something fishy somewhere. Do I need to show my entire sketch here? Or do I need to show the code on the Nextion? I am bit confused how to put up my case here.

Yes, without it no one has a clue what you are doing and what might be the problem.

Better still show a small sketch that shows the problem.

Please read How to get the best out of this forum before continuing.

Have you read Using Nextion displays with Arduino ?

Thanks for your response. First of all, I forgot to mention earlier that I am also using an SD card.

Coming to my issue, maybe the delay(30) and delay(50) used to stable the display are disturbing the stepper routine. But without them the page on HMI doesn't get updated correctly :roll_eyes:. On the contrary, even when the conditions skip the delays the stepper does not work correctly.

void loop() 
{
  //################## STRIP SETTING #######################################
  reading = digitalRead(FWDpin);
  debounce();
  if (reading == LOW)  ///// FWD button pressed
  {
    jogMoveStrip();
  }
  
  reading = digitalRead(REVpin);
  debounce();
  if (reading == LOW)  ///// REV button pressed
  {
    digitalWrite(dirPin, Clockwise);   // Change direction to reverse
    jogMoveStrip();                    // Jog move the Strip
    digitalWrite(dirPin, !Clockwise);  // Change back to forward direction
  }
  //############## STRIP SETTING END ########################################


  readSelectorSwitch(); // Read Selector Switch and prepare data for display 
  displayData();        // Update data on HMI

  //%%%%%%%%%%%%%%%%% If Something changed on the HMI %%%%%%
  if (Serial.available()) 
  {
    getDataFromDisplay();
    delay(50);
    rcv1stChar = rcvdDataFromDisplay.substring(0,1);
    rcvdDataFromDisplay = rcvdDataFromDisplay.substring(1);
    
    if (rcv1stChar == "R") // Today's counter has been RESET on the HMI
    {
      Count = "0";
    }
    else  // Size changed
    {
      if (rcv1stChar == "A") // Size 1 has been changed
      {
        stripLength = rcvdDataFromDisplay;
        Length[0] = rcvdDataFromDisplay;
      }
      if (rcv1stChar == "B") // Size 2 has been changed
      {
        stripLength = rcvdDataFromDisplay;
        Length[1] = rcvdDataFromDisplay;
      }
      if (rcv1stChar == "C") // Size 3 has been changed
      {
        stripLength = rcvdDataFromDisplay;
        Length[2] = rcvdDataFromDisplay;
      }
      if (rcv1stChar == "D") // Size 4 has been changed
      {
        stripLength = rcvdDataFromDisplay;
        Length[3] = rcvdDataFromDisplay;
      }
      if (rcv1stChar == "E") // Size 5 has been changed
      {
        stripLength = rcvdDataFromDisplay;
        Length[4] = rcvdDataFromDisplay;
      }
      if (rcv1stChar == "F") // Size 6 has been changed
      {
        stripLength = rcvdDataFromDisplay;
        Length[5] = rcvdDataFromDisplay;
      }
      calculateSteps();
    }
    updateSDcard(); // Update data on SD Card

  }//%%%%%%%%%%%%% Something changed on HMI ends %%%%%%%%%%%%

  //$$$$$$$$$$$$$ If Strip Present $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$

  if (digitalRead(stripPresentSensorPin) == HIGH) 
  {
    digitalWrite(strokePausePin, LOW); // Do NOT pause stroke
  }
  else
  { // if strip NOT present
    detectionDelay = analogRead(stripDetectionDelay);
    detectionDelay = map(detectionDelay, 0, 1023, 0, 1000);
    delay(detectionDelay);
    if (digitalRead(stripPresentSensorPin) == LOW) // recheck if Strip not present
    {
      digitalWrite(strokePausePin, HIGH); // PAUSE the stroke
    }
  }//$$$$$$$$$$$$$$$$$$$ If Strip Present END $$$$$$$$$$$$$$$$$$$$$$$$$$$$
    
}

Here are the functions or subroutines:

void readSelectorSwitch() // 2) Read Selector Switch and prepare data for display
{ 
  selectorSw = analogRead(selectorPin);
   
  if (selectorSw < 30)  ///// Size 1-1/16" selected
  {
    stripLength = Length[0];
    
    if (Size != "0")  // Size changed, last size was not 0
    {
      calculateSteps();
      totalCount = "0";
      Serial.print("page page1116"); // Display Page 1-1/16
      Serial.write(0xff);
      Serial.write(0xff);
      Serial.write(0xff);
      delay(50);
    }
    Size = "0";
    if (Start)
    {
      Start = false;
      calculateSteps();
      Serial.print("page page1116"); // Display Page 1-1/16
      Serial.write(0xff);
      Serial.write(0xff);
      Serial.write(0xff);
      delay(50);
    }
  }

  if (selectorSw > 180 && selectorSw < 220)  ///// Size 1-1/4" selected
  {
    stripLength = Length[1];
    
    if (Size != "1")  // Last size was not 1
    {
      calculateSteps();
      totalCount = "0";
      Serial.print("page page114"); // Display Page 1-1/4
      Serial.write(0xff);
      Serial.write(0xff);
      Serial.write(0xff);
      delay(50);
    }
    Size = "1";
    if (Start)
    {
      calculateSteps();
      Start = false;
      Serial.print("page page114"); 
      Serial.write(0xff);
      Serial.write(0xff);
      Serial.write(0xff);
      delay(50);
    }
  }
  
  if (selectorSw > 410 && selectorSw < 450)  ///// Size 1-1/2" selected
  {
    stripLength = Length[2];
      
    if (Size != "2")  // Last size was not 2
    {
      calculateSteps();
      totalCount = "0";
      Serial.print("page page112"); // Display Page 1-1/2
      Serial.write(0xff);
      Serial.write(0xff);
      Serial.write(0xff);
      delay(50);
    }
    Size = "2";
    if (Start)
    {
      calculateSteps();
      Start = false;
      Serial.print("page page112"); 
      Serial.write(0xff);
      Serial.write(0xff);
      Serial.write(0xff);
      delay(50);
    }
  }
  
  if (selectorSw > 580 && selectorSw < 620)  ///// Size 1-3/4" selected
  {
    stripLength = Length[3];
      
    if (Size != "3")  // Last size was not 3
    {
      calculateSteps();
      totalCount = "0";
      Serial.print("page page134"); // Display Page 1-3/4
      Serial.write(0xff);
      Serial.write(0xff);
      Serial.write(0xff);
      delay(50);
    }
    Size = "3";
    if (Start)
    {
      calculateSteps();
      Start = false;
      Serial.print("page page134"); 
      Serial.write(0xff);
      Serial.write(0xff);
      Serial.write(0xff);
      delay(50);
    }
  }
  
  if (selectorSw > 680 && selectorSw < 720)  ///// Size 2" selected
  {
    stripLength = Length[4];
      
    if (Size != "4")  // Last size was not 4
    {
      calculateSteps();
      totalCount = "0";
      Serial.print("page page2"); // Display Page 2
      Serial.write(0xff);
      Serial.write(0xff);
      Serial.write(0xff);
      delay(50);
    }
    Size = "4";
    if (Start)
    {
      calculateSteps();
      Start = false;
      Serial.print("page page2"); 
      Serial.write(0xff);
      Serial.write(0xff);
      Serial.write(0xff);
      delay(50);
    }
  }

  if (selectorSw > 810 && selectorSw < 850)  ///// Size 2-1/4" selected
  {
    stripLength = Length[5];
      
    if (Size != "5")  // Last size was not 5
    {
      calculateSteps();
      totalCount = "0";
      Serial.print("page page214"); // Display Page 2-1/4
      Serial.write(0xff);
      Serial.write(0xff);
      Serial.write(0xff);
      delay(50);
    }
    Size = "5";
    if (Start)
    {
      calculateSteps();
      Start = false;
      Serial.print("page page214"); 
      Serial.write(0xff);
      Serial.write(0xff);
      Serial.write(0xff);
      delay(50);
    }
  }
}/////////////////////////////////////////////////////////

void displayData() // 3) Update data on touchscreen
{    
  Serial.print("StripLength.val=" + stripLength); // Update the strip length on display
  Serial.write(0xff);
  Serial.write(0xff);
  Serial.write(0xff);
  
  Serial.print("n0.val=" + stripLength); 
  Serial.write(0xff);
  Serial.write(0xff);
  Serial.write(0xff);
  
  Serial.print("TodaysCount.val=" + Count); // Update the value of Today's Count on display
  Serial.write(0xff);
  Serial.write(0xff);
  Serial.write(0xff);
  
  Serial.print("Total.val=" + totalCount); // Update the value of Total Count on display
  Serial.write(0xff);
  Serial.write(0xff);
  Serial.write(0xff);
} //////////////////////////////////////////////////////

void getDataFromDisplay()
{
  rcvdDataFromDisplay = "";
  delay(30);
  while (Serial.available())
  {
    rcvdDataFromDisplay += char(Serial.read());
  }
}//////////////////////////////////////////////////////////

void calculateSteps()
{
  steps = ( 7 * stepsPerRev * stripLength.toInt() ) / (22 * rollerDia);
}//////////////////////////////////////////////////

void jogMoveStrip()
{
  digitalWrite(pulsePin, HIGH);
  delay(10);
  digitalWrite(pulsePin, LOW);
}/////////////////////////////////////////////////////////

void moveStrip()
{
  digitalWrite(strokePausePin, HIGH); // For Testing
  //===================================
  digitalWrite(dirPin, !Clockwise); //////// FORWARD DIRECTION
  for(int x = 0; x < steps; x++) 
  {
    digitalWrite(pulsePin,HIGH); 
    delayMicroseconds(pulseDelay); 
    digitalWrite(pulsePin,LOW); 
    delayMicroseconds(pulseDelay); 
  }
  //======================================
  //delay(2000);
  digitalWrite(strokePausePin, LOW); // For Testing
  Count = String(Count.toInt() + 1);
  totalCount = String(totalCount.toInt() + 1);
  updateSDcard();
}////////////////////////

I am considering to induct a separate controller for the stepper. An AT Tiny85 would do the job well but the number of steps to rotate the stepper are being calculated in Nano. Passing that value to Tiny85 is another hurdle, so another Nano would be a better choice.

Delay? Stabilise the display? You don't need to stabilise the display it's perfectly stable as it is, and you most certainly should not be using delay for anything.

Please study my tutorial.