Arduino Duemilanove and HC-SR04 used with a bar graph LED

I am new here and trying to get my head wrapped around this software. I found an example of the sketch I could use but, It wont compile in Arduino 1.0
Can anyone offer advice on this code? Thanks in advance. It wont take me long to catch on but I need a little direction…


  • HCSR04Ultrasonic/examples/UltrasonicDemo/UltrasonicDemo.pde
  • SVN Keywords

  • $Author: cnobile $
  • $Date: 2011-09-17 02:43:12 -0400 (Sat, 17 Sep 2011) $
  • $Revision: 29 $


#include <Ultrasonic.h>

#define TRIGGER_PIN 12
#define ECHO_PIN 13

Ultrasonic ultrasonic(TRIGGER_PIN, ECHO_PIN);

void setup()
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
pinMode(11, OUTPUT);

void loop()
float cmMsec, inMsec;
long microsec = ultrasonic.timing();

cmMsec = ultrasonic.convert(microsec, Ultrasonic::CM);
inMsec = ultrasonic.convert(microsec, Ultrasonic::IN);
Serial.print("MS: “);
Serial.print(”, CM: “);
Serial.print(”, IN: ");

if ((cmMsec) < 10) { digitalWrite(11, HIGH); }
if ((cmMsec) > 10) { digitalWrite(11, LOW); }
if ((cmMsec) < 20) { digitalWrite(10, HIGH); }
if ((cmMsec) > 20) { digitalWrite(10, LOW); }
if ((cmMsec) < 30) { digitalWrite(9, HIGH); }
if ((cmMsec) > 30) { digitalWrite(9, LOW); }
if ((cmMsec) < 40) { digitalWrite(8, HIGH); }
if ((cmMsec) > 40) { digitalWrite(8, LOW); }
if ((cmMsec) < 50) { digitalWrite(7, HIGH); }
if ((cmMsec) > 50) { digitalWrite(7, LOW); }
if ((cmMsec) < 60) { digitalWrite(6, HIGH); }
if ((cmMsec) > 60) { digitalWrite(6, LOW); }
if ((cmMsec) < 70) { digitalWrite(5, HIGH); }
if ((cmMsec) > 70) { digitalWrite(5, LOW); }
if ((cmMsec) < 80) { digitalWrite(4, HIGH); }
if ((cmMsec) > 80) { digitalWrite(4, LOW); }
if ((cmMsec) < 90) { digitalWrite(3, HIGH); }
if ((cmMsec) > 90) { digitalWrite(3, LOW); }
if ((cmMsec) < 100) { digitalWrite(2, HIGH); }
if ((cmMsec) > 100) { digitalWrite(2, LOW); }


  if ((cmMsec) < 10)  { digitalWrite(11, HIGH); }
  if ((cmMsec) > 10)  { digitalWrite(11, LOW);  }

Did you mean

  digitalWrite(11, cmMsec < 10 );


I found an example of the sketch I could use but, It wont compile in Arduino 1.0

perhaps You should Post the Error mesSages that the Compiler showeD you.

Thanks Groove, I have not made it to that line yet. I am hung trying to get past the first couple of lines. Having trouble with Ultrasonic.h I found this code somewhere last night. I copied and pasted it into the editor and ran a check/compile. It spits up right away.

It spits up right away.

Perhaps you need to give it some Pepto-Bismal.

Or post the error messages.

Ultrasonic ultrasonic(TRIGGER_PIN, ECHO_PIN);

Error: ultrasonic does not name a type

I posted your code into the Arduino 1.0 IDE. Compiling shows these errors:

sketch_dec31a.cpp:12:24: error: Ultrasonic.h: No such file or directory
sketch_dec31a:16: error: 'Ultrasonic' does not name a type
sketch_dec31a.cpp: In function 'void loop()':
sketch_dec31a:36: error: 'ultrasonic' was not declared in this scope
sketch_dec31a:38: error: 'Ultrasonic' has not been declared
sketch_dec31a:39: error: 'Ultrasonic' has not been declared

You can't skip errors you don't understand. The first one listed here is the most important one. Solve that, and the others will go away.

Thanks Paul,

I am green enough in this that I don't yet recognize whats happening or how to interpret what it says. I need a few days of messing with this to get involved. I appreciate your comments and I will spend some time reading.. I did not write the code but trying to de bug it on the fly.... I am going looking for a programming book at the electronics store now. And a breadboard.



While you are looking for stuff, look for the Ultrasonic library. Download and install it. First problem handled.

Hi Paul,

I have a breadboard, LED's, Resistors and jumpers. I am on my way. I could not find a book at the shop. I did find the library, installed it and re ran the code... You got me fixed! Its uploaded to the board and I am ready to put it in action. Thank you for the instruction and for checking that code.

I like the idea of this Arduino. I have been trying to build this the hard way and this is like the Holy Grail All I needed was a shove in the right direction.

Thanks again,


Does your code work as expected?

if ((cmMsec) < 10)  { digitalWrite(11, HIGH); }
if ((cmMsec) > 10)  { digitalWrite(11, LOW);  }

What if cmMsec is 10, 20, 30, 40 or … so on?


Hi Lextek,

take a look at how this guy here controls the LEDs:

I know that with just 10 LEDs it’s easy to write how to control them but you might wanna consider using a loop, like he does.