chipKIT® Development Platform

Inspired by Arduino™

Delay(100) in C++?

Created Mon, 20 Jun 2011 21:13:52 +0000 by Mr_Fixit


Mon, 20 Jun 2011 21:13:52 +0000

A brief delay is often required when dealing with ultrasonic sensors. In all the demo programs, this is handled with the delay(100) command. However this command does not work well when you create your own C++ based libraries. Anyone know a good equiv. that WILL work within the arduino environment?

So far, I have encapsulated my sensors in C++ classes, which really helps manage the code, but this is starting to kick my tail.


Tue, 21 Jun 2011 03:23:43 +0000

Why doesnt delay work? I see no reason why it should not?



Tue, 21 Jun 2011 06:18:23 +0000

It is not part of the C/C++ library. It works when its part of the sketch, but if you use Delay in a C/CPP/H file, then it does not work. I tried to use Sleep also, but that is not supported either even if you include dos.h.

Since I wanted to have my sketch fairly simple (Much like you do for your Main in standard C/C++), I encapsulated several routines in classes and added those as library files. This works great if I comment out the Delay(100) line. However in actual use, the delay must be there to give the sonar module enough time to receive echos.

Darth Maker

Tue, 21 Jun 2011 08:47:35 +0000

Do you have "#included <WProgram.h>" at the top of the library file?


Tue, 21 Jun 2011 16:56:33 +0000

Do you have "#included <WProgram.h>" at the top of the library file?

Beautiful! :) Knew there had to be a simple solution to this. Worked like a charm! Now... if they could only get I2C to work on the Uno32 I would be a very happy camper indeed!

Thanks for the tip Darth :D

Darth Maker

Wed, 22 Jun 2011 07:23:47 +0000

No problem. It's an easy mistake to make.

Just a note, beginning in the Arduino 1.0 Beta, WProgram.h is being replaced with Arduino.h. I don't know if the ChipKIT crew is planning to also convert everything over.


Wed, 22 Jun 2011 18:24:30 +0000

Doesn't the Arduino build (UNO, anyway) put in

#include "WProgram.h"

along with proper C/C++ prototypes of all your functions when it creates a .CPP working copy of your .PDE file in the temp directory?

Alan KM6VV


Wed, 22 Jun 2011 19:37:07 +0000

Hi Alan,

No, when creating an Arduino library, you have to add the include for WProgram.h at the top of the .cpp file for the library.