Can negative number in delay function?

I found from a site. I have a question regarding to programming.. I am wondering what happened more than 151 negative number of distance? if 150 - 151(151 is convert distance number) will be negative 1. because it said delay(150 - distance); , so will it work if delay(-1) function?

here example code...

void buzzer(){

if (distance>1){

if(distance>100){ // continuous sound if the obstacle is too close

digitalWrite(11,HIGH);

}

else{ // beeps faster when an obstacle approaches

digitalWrite(11,HIGH);

delay(150-distance); // adjust this value for your convenience

digitalWrite(11,LOW);

delay(150-distance); // adjust this value for your convenience

}

}

else{ // off if there is no obstacle
digitalWrite(11,LOW);
}
}

The parameter to delay is unsigned long. Unsigned means it can't be negative. If you try to put -1 in there, it will be read as 4,294,967,295

So delay(-1) really means delay(4,294,967,295). I doubt that's what you want.

It certainly won't work as noted above. You'll get an exceptionally long delay, so why not just constrain the distance value to prevent negative delays?

You mean you can't reverse time by using a negative delay. Pity.... That would be so useful.... :slight_smile:

Regards,
Ray L.