Advanced Arduino example to light 3 LEDs depending on how close sensed distance is to a target value using an HC-SR04 ultrasonic range sensor.
1 x HC-SR04 ultrasonic range sensor
3 x LEDs (pick the same color)
1 x 200 ohm resistor
1 x Arduino Uno
1 x breadboard
jumper wires
/**
* Sketch uses an HC-SR04 ultrasonic range finder to light
* 3 LEDs depending on how close the sensed distance is to
* a targeted value.
**/
// settings
int targetDistance = 32; // targeting this distance (cm)
int errorTolerance = 2; // allowed to be over/under by this amount (cm)
// track status
boolean targetUnder; // true if distance is less than target
boolean targetSatisfied; // true if distance is target (within tolerance)
boolean targetOver; // true if distance is greater than target
// LEDs used to indicate status
int ledTargetUnder = 10;
int ledTargetSatisfied = 11;
int ledTargetOver = 12;
// set up the range finder, HC-SR04
int rangeTriggerPin = 2;
int rangeEchoPin = 3;
long currentDistance; // (cm)
/**
* initialization code, happens once
**/
void setup()
{
// for printing
Serial.begin( 9600 );
// initialize status variables
targetUnder = false;
targetSatisfied = false;
targetOver = false;
// set up led pins
pinMode(ledTargetUnder, OUTPUT);
pinMode(ledTargetSatisfied, OUTPUT);
pinMode(ledTargetOver, OUTPUT);
// set up the range finder
pinMode(rangeTriggerPin, OUTPUT);
pinMode(rangeEchoPin, INPUT);
// check LEDs status - set all lights on
digitalWrite(ledTargetUnder, HIGH);
digitalWrite(ledTargetSatisfied, HIGH);
digitalWrite(ledTargetOver, HIGH);
delay(200);
}
/**
* loops after setup
**/
void loop()
{
// first, determine distance to closest object and update status
determineDistanceAndUpdateStatus();
// now, update output
updateOutput();
// wait a little before next iteration
delay( 500 );
}
/**
* Uses the range finder to determine distance to closest
* object. Updates status boolean variables.
**/
void determineDistanceAndUpdateStatus()
{
// get current distance
currentDistance = senseCurrentDistance();
// print it out
Serial.println( currentDistance );
int targetCutoffLo = targetDistance - errorTolerance;
int targetCutoffHi = targetDistance + errorTolerance;
// update status
// if we're too close
if ( currentDistance < targetCutoffLo )
{
targetUnder = true;
targetSatisfied = false;
targetOver = false;
}
// if we're too far
else if ( currentDistance > targetCutoffHi )
{
targetUnder = false;
targetSatisfied = false;
targetOver = true;
}
// otherwise, we're good!
else
{
targetUnder = false;
targetSatisfied = true;
targetOver = false;
}
}
/**
* Update output (light LEDs according to status).
**/
void updateOutput()
{
// under status
lightLED( ledTargetUnder, targetUnder );
// satisfied status
lightLED( ledTargetSatisfied, targetSatisfied );
// over status
lightLED( ledTargetOver, targetOver );
}
/**
* Light the LED based on the boolean.
**/
void lightLED( int ledPin, boolean on )
{
if ( on )
digitalWrite( ledPin, HIGH );
else
digitalWrite( ledPin, LOW );
}
/**
* Use the range finder to determine distance to the
* closest object.
**/
long senseCurrentDistance()
{
// The PING is triggered by a HIGH pulse of 2 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
digitalWrite(rangeTriggerPin, LOW);
delayMicroseconds(2);
digitalWrite(rangeTriggerPin, HIGH);
delayMicroseconds(5);
digitalWrite(rangeTriggerPin, LOW);
// A different pin is used to read the signal from the PING: a HIGH
// pulse whose duration is the time (in microseconds) from the sending
// of the ping to the reception of its echo off of an object.
long duration = pulseIn(rangeEchoPin, HIGH);
return microsecondsToCentimeters( duration );
}
/**
* Determine the distance based on how long it took to echo back.
**/
long microsecondsToCentimeters(long microseconds)
{
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance travelled.
return microseconds / 29 / 2;
}