AccelStepper in Arduino Cloud

Hello,
While I have successfully used AccelStepper library to move 3 stepper motors, when I use it on Arduino Cloud it seems that the motors move very slow and the speed given from the slider I am using, is not set to the motors the command stepperX.setMaxSpeed(speedX); fails to set the desired speed.

I am attaching the code I use. Any ideas ?

#include <WiFiNINA.h>
#include <AccelStepper.h>

/* 
  Sketch generated by the Arduino IoT Cloud Thing "Untitled"
  https://create.arduino.cc/cloud/things/069dba6c-43b5-42cc-afd0-e62bf37878a1 

  Arduino IoT Cloud Variables description

  The following variables are automatically generated and updated when changes are made to the Thing

  int speedX;
  int stepsX;
  int stepsY;
  bool moveX;
  bool moveY;

  Variables which are marked as READ/WRITE in the Cloud Thing will also have functions
  which are called when their values are changed from the Dashboard.
  These functions are generated with the Thing and added at the end of this sketch.
*/

#include "thingProperties.h"
#include <EEPROM.h>
#include <Bounce2.h>

#define dirPinX 8
#define stepPinX 9

#define dirPinY 7
#define stepPinY 6

#define dirPinZ 4
#define stepPinZ 5

// Define the steppers and pins that are used
AccelStepper stepperX(1, stepPinX, dirPinX);
AccelStepper stepperY(1, stepPinY, dirPinY);
AccelStepper stepperZ(1, stepPinZ, dirPinZ);

int x;

// Timing variables
unsigned long lastUpdateX = 0, lastUpdateY = 0, lastUpdateZ = 0;
const unsigned long updateInterval = 50; // 50ms for smooth updates

void setup() {
  // Initialize serial and wait for port to open:
  Serial.begin(9600);
  // This delay gives the chance to wait for a Serial Monitor without blocking if none is found
  delay(1500); 

  // Defined in thingProperties.h
  initProperties();
  
  pinMode(LED_BUILTIN, OUTPUT);

  // Connect to Arduino IoT Cloud
  ArduinoCloud.begin(ArduinoIoTPreferredConnection);
  ArduinoCloud.printDebugInfo();
  setDebugMessageLevel(2);
  ArduinoCloud.printDebugInfo();

  pinMode(stepPinX,OUTPUT); 
  pinMode(dirPinX,OUTPUT); 
  pinMode(stepPinY,OUTPUT); 
  pinMode(dirPinY,OUTPUT);
  
  stepperX.setMaxSpeed(10000);
  stepperY.setMaxSpeed(10000);
  stepperZ.setMaxSpeed(10000);

  stepperX.setAcceleration(8000);
  stepperY.setAcceleration(8000);
  stepperZ.setAcceleration(8000);

  stepperX.disableOutputs();
  stepperY.disableOutputs();
  stepperZ.disableOutputs();

}


void loop() {
  ArduinoCloud.update();
  setDebugMessageLevel(2);

  if (moveX){
    stepperX.run();
  }
  if(moveY){
    stepperY.run();
  }

  stepperZ.run();

}
 
/*
  Since PushbuttonX is READ_WRITE variable, onPushbuttonXChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onPushbuttonXChange()  {
  int x;

}


void onRunXChange()  {
  // Add your code here to act upon RunX change
}


/*
  Since StepsX is READ_WRITE variable, onStepsXChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onStepsXChange()  {
  Serial.println(stepsX);
}

/*
  Since StepsY is READ_WRITE variable, onStepsYChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onStepsYChange()  {
  // Add your code here to act upon StepsY change
}



void onMoveXChange()  {
  if(moveX){
    unsigned long currentMillis = millis();
    ArduinoCloud.update();
    digitalWrite(LED_BUILTIN, HIGH);
    
    stepperX.enableOutputs();
    stepperX.setMaxSpeed(speedX);
    Serial.println(speedX);
    //stepperX.setSpeed(10000);
    stepperX.move(stepsX);

  } else {
    stepperX.stop();
    stepperX.disableOutputs();
    digitalWrite(LED_BUILTIN, LOW);
  }
}


void onMoveYChange()  {
  if(moveY) {
    unsigned long currentMillis = millis();
    ArduinoCloud.update();
    digitalWrite(LED_BUILTIN, HIGH);
    stepperY.enableOutputs();
    stepperY.setMaxSpeed(5000);
    //stepperY.setSpeed(1000);
    stepperY.move(stepsY);

  } else {
    stepperY.stop();
    stepperY.disableOutputs();
    digitalWrite(LED_BUILTIN, LOW);
  }
}

void onSpeedXChange() {
  Serial.println(speedX);  
}

Perhaps this will help. The difference is I'm using a slider to control "desiredRPM" whereas it seems you may be using "MaxRPM". The other difference is I'm updating stepper.setSpeed from the onChange function for the speed slider whereas you're updating max speed from the onMove function.

Have you tried using setSpeed instead of setMaxSpeed?

Here's what I have, and the speed does update whenever I change the speed slider:

void setup() {
  float desiredRPM = 120; // Set the desired speed in rpm (revolutions per minute)
  float MaxRPM = 300; // Set max speed in rpm (revolutions per minute)
  // Calculate and set the desired and max speed in steps per second
  float speedStepsPerSec = (microstepSetting * stepsPerRevolution*desiredRPM) / 60.0;
  float Max_Speed_StepsPerSec = microstepSetting * stepsPerRevolution * MaxRPM / 60; // Specify max speed in steps/sec (converted from RPM)
  stepper.setMaxSpeed(Max_Speed_StepsPerSec);
  stepper.setSpeed(speedStepsPerSec);
}
void onVarSpeedChange()  { //from the speed slider
  float desiredRPM = var_Speed;
  float speedStepsPerSec = (microstepSetting * stepsPerRevolution*desiredRPM) / 60.0;
  stepper.setSpeed(speedStepsPerSec);
}

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