Manually set interval from serial monitor is not working if there is "delayMicroseconds"

How to set manual insertion for interval from serial monitor id there is "delayMicroseconds" in sketch ?

If there is "delay" only then insertion from serial monitor is working.

But if I use "delayMicroseconds", I can insert the interval value from serial monitor but it is not working.

Please suggest me how can I solve this issue...

void setup() {
  // initialize serial communication at 9600 bits per second:
  //Serial.begin(9600);
  //Serial.begin(500000);
  Serial.begin(115200);
}

// the loop routine runs over and over again forever:
void loop() {
  //float timer;
  static int timer = 1;
  //static int timer = 1;
  if(Serial.available() > 0)
  {
    timer = Serial.parseFloat();
    
    Serial.print(">");
    Serial.print(timer);
    Serial.println("<");
   //Serial.flush();
  }
  int sensorValue = analogRead(A0);
  float distance;
  float voltage = sensorValue * (5.0 / 1023.0);

  distance = 20*voltage - 0.051;
  Serial.println(distance);

  //delay(timer);
  delayMicroseconds(timer);
}

The first thing that I notice is that you are using a float with delayMicroseconds()

Why ?

Secondly, do you realise how short a microsecond is and what the largest number is that produces an accurate delay ?

From delayMicroseconds() - Arduino Reference

Currently, the largest value that will produce an accurate delay is 16383. This could change in future Arduino releases. For delays longer than a few thousand microseconds, you should use delay() instead.

Theoretically delay(1) is equal to delayMicroseconds(1000).
But delay(1) is unable to satisfy the project purpose. Whether delayMicroseconds(779) can give the results much better.

as following this code :


void setup() {
  Serial.begin(115200);
  //Serial.begin(9600);
}
void loop() {

  int sensorValue = analogRead(A0);
  float distance;
  float voltage = sensorValue * (5.0 / 1023.0);
  distance = 20*voltage - 0.051;
  Serial.println(distance);

  delayMicroseconds(779);
}

But even if i use "int" also doesn't work :

void setup() {
  // initialize serial communication at 9600 bits per second:
  //Serial.begin(9600);
  //Serial.begin(500000);
  Serial.begin(115200);
}

// the loop routine runs over and over again forever:
void loop() {
  //float timer;
  static int timer = 779;
  //static int timer = 1;
  if(Serial.available() > 0)
  {
    timer = Serial.parseInt();
    
    Serial.print(">");
    Serial.print(timer);
    Serial.println("<");
   //Serial.flush();
  }
  int sensorValue = analogRead(A0);
  float distance;
  float voltage = sensorValue * (5.0 / 1023.0);

  distance = 20*voltage - 0.051;
  Serial.println(distance);

  //delay(timer);
  delayMicroseconds(timer);
}

Is it possible to set interval from serial monitor in the case of using delayMicroseconds ?

Yes, of course it is

Please help me in code ? my sketch is not working.
What should be the correct code in that case ?

We don't know what you mean by "working", or what criteria you are using to decide that it is "not working"

Don't forget that analogRead introduces its own delay of around 100us

Let's start with basics

What have you got Line ending set to in the Serial monitor ? Anything except "No line ending" is the wrong answer

If I insert "10000000" from serial, it should delay for 10 seconds. But it is not delaying.

And the Line ending setting is ?

you mean that there is nothing wrong with the code ?
is "No line ending" is the wrong answer. then why you are not mentioning the right one.
i'm learning...

There's a note above about the maximum delay using delayMicroseconds

If you have a line ending set then when your code does

if(Serial.available() > 0)

there will be something available after what you enter so the timer variable will be given its value

I'm using "no line ending".

But still the interval "10000000" for 10 second is not working.

Note that the data type used by delayMicroseconds() is an unsigned int.

Which Arduino are you using ?

What is the largest value that can be held by an unsigned int on that Arduino ?

ardoUNO

I'm using arduino UNO.

In that case what should be the code ?

How long is the delay that you want to set ?

Delay I need to set from serial monitor is about 10 second = 10000000 micros or more than that.

Consider using micros() for timing instead of delayMicroseconds()

the argument to delayMicroseconds is an integer which has a max vale if 32767

The reference you linked says "unsigned int".

An "integer" could be "char", "int", "long" or "long long" (and unsigned variants thereof)