delayMicroseconds to millis

how can i re write this in millis so that its non-blocking i did the top part but i dont know what to do with the delaymicroseconds?

unsigned long WaterWait = 4000;
unsigned long Waterdelay =0;

int trig = 12;
int echo = 11;

void setup()
{
  Serial.begin(9600);
  pinMode(trig, OUTPUT);
  pinMode(echo, INPUT); 
}

void loop()
{
 if (millis() - Waterdelay > WaterWait) {
  long t = 0, h = 0, hp = 0;
  digitalWrite(trig, LOW);
  delayMicroseconds(2);
  digitalWrite(trig, HIGH);
  delayMicroseconds(10);
  digitalWrite(trig, LOW);
  t = pulseIn(echo, HIGH);
  h = t / 45;
  h = h - 5;  // offset correction
  h = 35 - h;  // water height, 0 - 50 cm
  hp = 2.7 * h;  // distance in %, 0-100 %
  Serial.print(hp);
  Serial.println(F("%"));
  Waterdelay = millis();
 }
}

Is it critical to get rid of the very short blocking delayMicroseconds?
In fact, those delays are almost certainly shorter than pulseIn

TolpuddleSartre:
Is it critical to get rid of the very short blocking delayMicroseconds?
In fact, those delays are almost certainly shorter than pulseIn

i dont know i have alot of other timers in the background this is just a small part of code that im going to integrate into my main sketch. so far i have come up with this,

unsigned long WaterWait = 4000;
unsigned long Waterdelay =0;
unsigned long WaterWait2 = 10;
unsigned long Waterdelay2 =0;
unsigned long Waterdelay3 =0;

int trig = 12;
int echo = 11;

void setup()
{
  Serial.begin(9600);
  pinMode(trig, OUTPUT);
  pinMode(echo, INPUT); 
}

void loop()
{
 if (millis() - Waterdelay > WaterWait) {
  long t = 0, h = 0, hp = 0;
  digitalWrite(trig, LOW);
 if (millis() - Waterdelay2 > WaterWait2) {
  digitalWrite(trig, HIGH);
 if (millis() - Waterdelay3 > WaterWait2) {
  digitalWrite(trig, LOW);
  t = pulseIn(echo, HIGH);
  h = t / 45;
  h = h - 5;  // offset correction
  h = 35 - h;  // water height, 0 - 50 cm
  hp = 2.7 * h;  // distance in %, 0-100 %
  Waterdelay2 = millis();
  Waterdelay2 = millis();
  Waterdelay = millis();
  Serial.print(hp);
  Serial.println(F("%"));
 }
 }
 }
}
const unsigned long waterWait = 4000;
unsigned long waterdelay;

const byte trig = 12;
const byte  echo = 11;

void setup()
{
  Serial.begin(9600);
  pinMode(trig, OUTPUT);
  digitalWrite(trig, LOW);
  pinMode(echo, INPUT);
}

void loop()
{
 if (millis() - waterdelay > waterWait) {
  digitalWrite(trig, HIGH);
  delayMicroseconds(10);
  digitalWrite(trig, LOW);
  unsigned long t = pulseIn(echo, HIGH);
  unsigned long h = t / 45;
  h = h - 5;  // offset correction
  h = 35 - h;  // water height, 0 - 50 cm
  unsigned long hp = 2.7 * h;  // distance in %, 0-100 %
  Serial.print(hp);
  Serial.println(F("%"));
  waterdelay += waterWait;
 }
}

(I hate too much code) This code has not been compiled and it has not been tested.

The code you posted won't work, and anyway you need to learn the difference between "millis()" and "micros()"

TolpuddleSartre:
The code you posted won't work, and anyway you need to learn the difference between "millis()" and "micros()"

'
so there is really nothing i can do about the delaymicroseconds?

TolpuddleSartre:
The code you posted won't work, and anyway you need to learn the difference between "millis()" and "micros()"

right now this is what im using

unsigned long WaterWait = 4000;
unsigned long Waterdelay =0;
unsigned long WaterWait2 = 10;
unsigned long WaterWait3 = 20;
unsigned long Waterdelay2 =0;
unsigned long Waterdelay3 =0;

int trig = 12;
int echo = 11;

void setup()
{
  Serial.begin(9600);
  pinMode(trig, OUTPUT);
  pinMode(echo, INPUT); 
}

void loop()
{
 if (millis() - Waterdelay > WaterWait) {
  long t = 0, h = 0, hp = 0;
  digitalWrite(trig, LOW);
 if (micros() - Waterdelay2 > WaterWait2) {
  digitalWrite(trig, HIGH);
 if (micros() - Waterdelay3 > WaterWait3) {
  digitalWrite(trig, LOW);
  t = pulseIn(echo, HIGH);
  h = t / 45;
  h = h - 5;  // offset correction
  h = 35 - h;  // water height, 0 - 50 cm
  hp = 2.7 * h;  // distance in %, 0-100 %
  Waterdelay2 = millis();
  Waterdelay3 = millis();
  Waterdelay = millis();
  Serial.print(F("The Water Level Is At "));
  Serial.print(hp);
  Serial.println(F("%"));
 }
 }
 }
}

You're worried about a 12 microsecond blocking (my code blocks for 10 microseconds) every four MILLION microseconds.

That's not something you should be worrying about at the moment.

Given that the speed of sound is something like 8.5mm in 25 microseconds, pulseIn blocks for much longer than your delays.

Your revised code still does not work, and you have demonstrated that you still need to learn the difference between milliseconds and microseconds.

Two things: you cannot delay for 2 microseconds or 10 microseconds.

The off-the-shelf micros() function has a resolution of 1/256th of about a millisecond, so pick 4, 8 or 12.

Usually for non-blocking you would mark the time and carry on, returning from time to time, via the loop, to see if a prescribed period has passed. You would do it with micros() the same way you would do it with millis(). In reality though how far are you going to get through your code before your time runs out? Is it worth it? Or just stick with your delays?

The two microsecond delay is redundant anyway, as my code earlier demonstrated.

 Serial.print(hp);
  Serial.println(F("%"));

This serial prints, blocking much more than 10 microseconds.

GrOnThOs:

 Serial.print(hp);

Serial.println(F("%"));




This serial prints, blocking much more than 10 microseconds.

Not every four seconds it doesn't.

This unnecessary six time calls of millis function blocking much more than 10 microsecond.

GrOnThOs:
This unnecessary six time calls of millis function blocking much more than 10 microsecond.

Four calls to millis, that I can see in the most-recently posted code.

Four calls to millis, that I can see in the most-recently posted code.

Six times to the first post but you lost my point.

GrOnThOs:
Six times to the first post but you lost my point.

The first post has only two calls to millis - you need to pay attention to detail.
It's very important if you want to get anywhere in software.

DKWatson:
Two things: you cannot delay for 2 microseconds or 10 microseconds.

The off-the-shelf micros() function has a resolution of 1/256th of about a millisecond, so pick 4, 8 or 12.

Nonsense. delayMicroseconds() has nothing to do with micros().

(rephrasing:)

void delayMicroseconds(unsigned int us)
{
    us = calculate_number_of_loops_based_on_F_CPU(us);
 
    __asm__ __volatile__ (
        "1: sbiw %0,1" "\n\t" // 2 cycles
        "brne 1b" : "=w" (us) : "0" (us) // 2 cycles
    );
}

The first post has only two calls to millis - you need to pay attention to detail.
It's very important if you want to get anywhere in software.

Of course. six times in the second post but what i want to say is that anything more than one call is unnecessary and increase the code execut time.

amazing that you dont even need to run the code to debug it. i have been running it like this and its been working but ill take your word with the delaymicroseconds and im only going to be calling it every 30 seconds anyways i just didnt know if it was a bad idea. ive been running it without delaymicroseconds or any delay besides the 30 seconds delay and its been working. does it really need the delay? what was the point of using it?

unsigned long WaterWait = 30000;
unsigned long Waterdelay = 0;

int trig = 12;
int echo = 11;

void setup()
{
  
Serial.begin(9600);
 pinMode(trig, OUTPUT);
 pinMode(echo, INPUT); 
}
void loop()
{
  long t = 0, h = 0, hp = 0;
  digitalWrite(trig, LOW);
 if (millis() - Waterdelay > WaterWait) {
  digitalWrite(trig, HIGH);
  digitalWrite(trig, LOW);
  t = pulseIn(echo, HIGH);
  h = t / 45;
  h = h - 5;  // offset correction
  h = 35 - h;  // water height, 0 - 50 cm
  hp = 2.7 * h;  // distance in %, 0-100 %
  Waterdelay = millis();
  Serial.print(F("The Water Level Is At "));
  Serial.print(hp);
  Serial.println(F("%"));
 }
}

The point of not using delay is that it allows your sketch to do other things instead of locking up the processor, twiddling its metaphoric thumbs.
This is fine on the order of tens of milliseconds to seconds or even hours, but at the level of small numbers of microseconds, it doesn't make much sense.