Issue with delayMicroseconds iot and r4 wifi

i dont seem to be able to get delayMicroseconds to work from the arduino iot cloud, delay works if substituted but i need microseconds to control the stepper delays(not using a library).

/* 
  Sketch generated by the Arduino IoT Cloud Thing "Untitled"
  https://create.arduino.cc/cloud/things/862e6649-0da1-4814-82e7-3a8560ff4f19 

  Arduino IoT Cloud Variables description

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

  CloudLight led;
  bool step_back;
  bool step_for;
  CloudTime step_speed;

  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.
*/
int xstep = 10;
int xenable = 9;
int xdir = 8;
unsigned int speedState;
#include "thingProperties.h"

void setup() {
pinMode(xstep, OUTPUT);
pinMode(xenable, OUTPUT);
pinMode(xdir, OUTPUT);
pinMode(13, OUTPUT);
  // Defined in thingProperties.h
  initProperties();
  

  // Connect to Arduino IoT Cloud
  ArduinoCloud.begin(ArduinoIoTPreferredConnection);
  
  /*
     The following function allows you to obtain more information
     related to the state of network and IoT Cloud connection and errors
     the higher number the more granular information you’ll get.
     The default is 0 (only errors).
     Maximum is 4
 */
  setDebugMessageLevel(2);
  ArduinoCloud.printDebugInfo();
}

void loop() {
  ArduinoCloud.update();
  // Your code here 
  digitalWrite(xenable, HIGH);
  speedState = step_speed;
 

  if(step_for == HIGH){
    onStepForChange();
  }
  if(step_back == HIGH){
    onStepBackChange();
  }
}

/*
  Since Led is READ_WRITE variable, onLedChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onLedChange()  {
  if(led == HIGH){
    digitalWrite(13, HIGH);
  }
  else{
    digitalWrite(13, LOW);
  // Add your code here to act upon Led change
  }
}

/*
  Since StepFor is READ_WRITE variable, onStepForChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onStepForChange()  {
  digitalWrite(xenable, LOW);
  digitalWrite(xdir, HIGH);
  digitalWrite(xstep, HIGH);
  delayMicroseconds(speedState);
  //delay(speedState);
  digitalWrite(xstep, LOW);
  delayMicroseconds(speedState);
  //delay(speedState);
  // Add your code here to act upon StepFor change
}

/*
  Since StepBack is READ_WRITE variable, onStepBackChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onStepBackChange()  {
  digitalWrite(xenable, LOW);
  digitalWrite(xdir, LOW);
  digitalWrite(xstep, HIGH);
  delayMicroseconds(speedState);
  //delay(speedState);
  digitalWrite(xstep, LOW);
  delayMicroseconds(speedState);
  //delay(speedState);
  // Add your code here to act upon StepBack change
}

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




if i change delayMicroseconds to delay everything works albeit very slowly.

any help gratefully recieved

rich

Welcome to the forum

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the < CODE/ > icon above the compose window) to make it easier to read and copy for examination

https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum

Please post your full sketch, using code tags when you do

Posting your code using code tags prevents parts of it being interpreted as HTML coding and makes it easier to copy for examination

In my experience the easiest way to tidy up the code and add the code tags is as follows

Start by tidying up your code by using Tools/Auto Format in the IDE to make it easier to read. Then use Edit/Copy for Forum and paste what was copied in a new reply. Code tags will have been added to the code to make it easy to read in the forum thus making it easier to provide help.

It is also helpful to post error messages in code tags as it makes it easier to scroll through them and copy them for examination

ok sorted that, many thanks for your help

What is the expected interval range for 'step_speed' ?

its variable from 500-2500 microseconds,
i have tried multiple variable types so far and none seem to work, int, long, unsigned and normal.

thanks

An unsigned int has a range of 0 - 65536, so, in theory, ought to be sufficient to store a value in the range you mention.

Can I ask how are you determining that delayMicroseconds() does not work? The reason I ask is that a delay of 2000 microseconds or 2 milliseconds is so small as to not be perceptible.

thank you for taking the time to help with this,

when i wrote the code in the normal offline arduino ide to troubleshoot this, 500 - 2500 were the values that made the stepper run reliably.
so have transferred those values over, i will try much larger values and see if there is an improvement.

rich

it seems that when i call the stepper void it only runs once and then cycles the rest of the code back to the "if" and that is the delay i am experiencing.

so i have misunderstood the "if" and calling the void, i thought that the code would stay there until the button was released.

back to the drawing board with the code for me.

many thanks for your time