I have developed a program using accelstepper library to control a 3-axis stepper motors via Arduino.
The program on its desktop version works perfectly, but when i tried to migrate it to Arduino cloud (in order to control the motors from iPhone), the same code has the following bug, the motors move in a very slow speed and i cannot change their speed.
I am attaching you the code i am using in the Arduino cloud. 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 stepper1; // Defaults to AccelStepper::FULL4WIRE (4 pins) on 2, 3, 4, 5
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 = 5; // 5ms
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.runSpeed();
}
if(moveY){
stepperY.runSpeed();
}
stepperZ.runSpeed();
}
/*
Since PushbuttonX is READ_WRITE variable, onPushbuttonXChange() is
executed every time a new value is received from IoT Cloud.
*/
void onPushbuttonXChange() {
int x;
}
/*
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 {
ArduinoCloud.update();
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 {
ArduinoCloud.update();
stepperY.stop();
stepperY.disableOutputs();
digitalWrite(LED_BUILTIN, LOW);
}
}
void onSpeedXChange() {
Serial.print("SpeedX updated: ");
Serial.println(speedX);
stepperX.setMaxSpeed(speedX);
}