Stepper moves roughly (due to millis(), I think)

Hi folks,

I am almost done with my project (I will share with all of you when finished) but I am finding now some problems with my steppers.
I am using AccelStepper library and I think it is awesome. It is probably my fault, because of a dirty code, but I am running out of time and trial-error method can be too much time-consuming.

I am getting rough movement when using the loop, but not when using blocking queries (like runToNewPosition) so that way I can confirm that problem is not related to nor motors neither drivers.

// Copyright (c) 2016 Daniel Grau Gil-Díez - danielgraugildiez@gmail.com
// Based on a project of Stefano Guglielmetti
// https://github.com/amicojeko
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.

// Include Bridge, Proccess and Stepper libraries
#include <Bridge.h>
#include <Process.h>
#include <AccelStepper.h>

// Declare variables, LEDs and Steppers (out of voids are declared global vars)

// Stepper definitions
AccelStepper stepper1(AccelStepper::DRIVER, 9, 8);
AccelStepper stepper2(AccelStepper::DRIVER, 11, 10);
AccelStepper stepper3(AccelStepper::DRIVER, 13, 12);
boolean stepperOneLaunched = false;
boolean stepperTwoLaunched = false;
boolean stepperThreeLaunched = false;
int mechanicalEnd = 1000;
int mechanicalEndPin1 = A0;
int mechanicalEndPin2 = A1;
int mechanicalValue1;
int mechanicalValue2;

// Miilis variables
unsigned long currentMillis;
unsigned long hashtagOneMillis = 0; // Long (32bits) positive variable for time management
unsigned long hashtagTwoMillis = 0; // Long (32bits) positive variable for time management
unsigned long hashtagThreeMillis = 0; // Long (32bits) positive variable for time management
const unsigned long delayInterval = 30000; // Time interval between stepper moves
const unsigned long delayDown = 15000; // Time interval until the stepper goes up

// Declare bo_buffer variable which gets values from 'go' bridge variable
char go_buffer[2];

// Initialize go variable
int  go = 0;

// Security reset counter to relaunch the script if fails.
int resetCount = 0; 

// Initialize the Python Streaming script
Process p;

void setup() {
  
  // Hardware initialization
  
  // Initialize the serial port
  Serial.begin(9600);

  // Initialize the Bridge Library
  Bridge.begin();

  // Initialize Tweepy Streaming Script
  callTweepyStreaming();

  // Set steppers properties
   
  stepper1.setMaxSpeed(400.0);
  stepper1.setAcceleration(100.0);
  
  // Home position stepper 1 (PUT INTO A FUNCTION)
  stepper1.moveTo(3000);
  stepper1.setSpeed(-300);
  
  mechanicalValue1 = analogRead(mechanicalEndPin1);
  while (mechanicalValue1 > mechanicalEndPin1) { 
    mechanicalValue1 = analogRead(mechanicalEndPin1);
    stepper1.runSpeed();
    Serial.print(mechanicalValue1);
  }
  delay(500);
  stepper1.stop();
  stepper1.setCurrentPosition(200);
  stepper1.move(200);
  stepper1.runToPosition();

  // Home position Stepper 2 (PUT INTO A FUNCTION)
  
  stepper2.setMaxSpeed(400.0);
  stepper2.setAcceleration(100.0);
  stepper2.moveTo(3000);
  stepper2.setSpeed(-300.0);
  
  mechanicalValue2 = analogRead(mechanicalEndPin2);
  while (mechanicalValue2 > mechanicalEndPin2) { 
    mechanicalValue2 = analogRead(mechanicalEndPin2);
    stepper2.runSpeed();
    Serial.print(mechanicalValue2);
  }
  delay(500);
  stepper2.stop();
  stepper2.setCurrentPosition(200);
  stepper2.move(200);
  stepper2.runToPosition();

  // Home position Stepper 3 (PUT INTO A FUNCTION)
  stepper3.stop();
  stepper3.setMaxSpeed(400.0);
  stepper3.setAcceleration(400.0);
  stepper3.moveTo(5000);
  stepper3.setSpeed(300.0);
  mechanicalValue2 = analogRead(mechanicalEndPin2);
  while (mechanicalValue2 > mechanicalEndPin2) { 
    mechanicalValue2 = analogRead(mechanicalEndPin2);
    stepper3.runSpeed();
    Serial.print(mechanicalValue2);
  }
  delay(500);
  stepper3.stop();
  stepper3.setCurrentPosition(0);
  stepper3.move(-50);
  stepper3.runToPosition();

}

void loop() {
  // Always check if Python is running and we are not in an empty loop
  while (p.running() && (resetCount < 2000)) {
    // Read the "go" Bridge var and copies it into the go_buffer variable
    currentMillis = millis();

    Bridge.get("go", go_buffer, 2);
    //Serial.println("Entramos al loop");

    // Convert the bugger into an int variable
    go = atoi(go_buffer);
    //Serial.println(go);

    // Check for "go" var and go to respective function
    if ((go == 1) && ((unsigned long)(millis() - hashtagOneMillis >= delayInterval))) {
        hashtagOneMillis = currentMillis;
        stepperOneLaunched = true;
        stepper1.moveTo(200);
    } else if ((go == 2) && ((unsigned long)(millis() - hashtagTwoMillis >= delayInterval))) {
        hashtagTwoMillis = currentMillis;
        stepperTwoLaunched = true;
        stepper2.moveTo(200);
    } else if ((go == 3) && ((unsigned long)(millis() - hashtagThreeMillis >= delayInterval))) {
        hashtagThreeMillis = currentMillis;
        stepperThreeLaunched = true;
        stepper3.moveTo(-750);
    } else if (go == 9) {
      Serial.println("Closing time");
      //closingTime(); (Not implemented yet)
    }
    if (((unsigned long)(currentMillis - hashtagOneMillis >= delayDown)) && stepperOneLaunched) {
      stepper1.moveTo(-stepper1.currentPosition());
    }
    if (((unsigned long)(currentMillis - hashtagTwoMillis >= delayDown)) && stepperTwoLaunched) {
      stepper2.moveTo(-stepper2.currentPosition());
    }
    if (((unsigned long)(currentMillis - hashtagThreeMillis >= delayDown)) && stepperThreeLaunched) {
      stepper3.moveTo(-stepper3.currentPosition());
    }
    if (stepperOneLaunched == true) {
      stepper1.run();
      Serial.println("StepperOneLaunched");
    }
    if (stepperTwoLaunched == true) {
      stepper2.run();
      Serial.println("StepperTwoLaunched");
    }
    if (stepperThreeLaunched == true) {
      stepper3.run();
      Serial.println("StepperThreeLaunched");
    }
    
    resetCount++;
  }
  // Something went wrong and we are out of p.running loop. Reset counter and relaunch
  resetCount = 0;
  callTweepyStreaming();
  Serial.println("Re-start script");
}

// Call the Tweepy streaming Python script
void callTweepyStreaming() {
  p.begin("python");
  p.addParameter("/root/python/TwitterBlink/myStreamingTweepy.py");
  //p.addParameter("&2>1");
  p.runAsynchronously();
}
// Restarts program from beginning but does not reset peripherals and registers (Not using now)
void softwareReset() {
  asm volatile ("  jmp 0");
}

I am aware I am using MechanicalValue2 in two different places, it is for testing purposes.

Some of my ideas:

-Is it necessary to compare with (unsigned long) even with all variables participating in the comparison were previusly declared as unsigned long?

-Will microsteps use do any favor?

-Any extra thought?

Thanks in advance,

All your println's inside loop are slowing it done chronically - comment them out.

You need all the step() methods to be called frequently, think 10kHz or so.

This seems to be the same project as in your other Thread - why start a new Thread when all the useful info is already in the first Thread ?

…R

I thought it, Robin!
But also thought that now the topics are much further from each other. There I was talking about H-Bridge and heat problems, and now it is completely different.

Nevertheless, if I am wrong and both posts deserve the same place, I would be happy if moderators could move it!

Thanks!