My sonic II

With this version of the previous project, I have added a measurement test. This test is to check on the target being within 5-15 centimeters.


The code is listed below and I tried to add comments to make each step clear. Let me know if it is not, or if you have any other questions.

/****************************************
* use a HC-SR04 ultrasonic sensor and
* print the measurement to a LCD IM161 display
****************************************/

/*

The LCD circuit:
* LCD RS pin to digital pin 12
* LCD Enable pin to digital pin 11
* LCD D4 pin to digital pin 5
* LCD D5 pin to digital pin 4
* LCD D6 pin to digital pin 3
* LCD D7 pin to digital pin 2
* LCD R/W pin to ground
* 10K resistor:
* ends to +5V and ground
* wiper to LCD VO pin (pin 3)

*/

/*

The ultrasonic circuit:
* VCC (+5)
* TRIG pin 9
* ECHO pin 8
* GND (ground)

*/

/******************************
include libraries and declare initialize
commands before void setup()
******************************/

// include the library code:
//Library for LCD display
//http://arduino.cc/en/Reference/LiquidCrystal
#include //Library for ultrasonic sensor
//https://code.google.com/p/arduino-new-ping/
#include

// Arduino pin tied to trigger pin on the ultrasonic sensor.
#define TRIGGER_PIN 9
// Arduino pin tied to echo pin on the ultrasonic sensor.
#define ECHO_PIN 8
//Maximum distance we want to ping for (in centimeters).
//Maximum sensor distance is rated at 400-500 cm.
#define MAX_DISTANCE 400

// initialize the library with the numbers of the interface pins
//http://arduino.cc/en/Reference/LiquidCrystalConstructor
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

// NewPing setup of pins and maximum distance.
//http://playground.arduino.cc/Code/NewPing
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);

int led=13; //pin for inrange LED
int okClose=0; //ok not too close 0=too close, 1 = ok
int okFar=0; //ok not too far 0= too far, 1 = ok
int light=0; //light the led 0=off 1=on
int A=0; //centimeters for testing
int toFar=15; // too far away centimeter
int toClose=5; // too close centimeter
int both=0; //final test value 0 or 1 = out of range, 2 = in range

void setup() {

Serial.begin(115200); //setup serial communications for monitoring
pinMode(led,OUTPUT); // setup inrange light

// set up the LCD’s number of columns and rows:
lcd.begin(16, 2);
lcd.clear();
lcd.print(“CM”);

}

void loop() {
// set the cursor to column 0, line 1
// (note: line 1 is the second row, since counting begins with 0):

// Wait 100ms between pings (about 10 pings/sec). 29ms should be the shortest delay between pings.
delay(100);

//Send ping, get median (from 5) ping time in microseconds (uS).
unsigned int uS = sonar.ping_median(5); //http://playground.arduino.cc/Code/NewPing#Methods

// Calculate the CM for testing range
A=(uS / US_ROUNDTRIP_CM);

// test for too close. shows a compact format for the if else statment.
if (A >= toClose)okClose=1; //not too close
else okClose=0; // too close!

//test for too far away. 1 = not too far, 0 = too far away. shows altenate way to format a if else test.
if (A <= toFar)
{okFar=1;}
else
{okFar=0;}

both=0; //clear the ok range test
both=okClose+okFar; // set the ok range test, 0 or 1 is not ok 2 is inrange

light=0; // set led off
if(both==2) light=1;

digitalWrite(led,light); //turn on/off the inrange led

// print labels
lcd.clear();// clear LCD for next measurement
lcd.setCursor(0, 0); //cursor to top left
lcd.print(“CM”); // Centimeter label
lcd.setCursor(8, 0); //cursor to top center
lcd.print(“IN”); //Inch label

// print measurments
lcd.setCursor(4, 0); //cursor to top position 4
lcd.print(uS / US_ROUNDTRIP_CM); //Print centimeters
lcd.setCursor(12, 0); //cursor to top postion 12
lcd.print((uS / US_ROUNDTRIP_CM)*0.393701); //print Inches

// this is diagnostic printing of the led setting process. It can be removed after all is well
lcd.setCursor(0, 1); //cursor to bottom postion 0
lcd.print(okClose); //Print okClose
lcd.setCursor(2,1); //cursor to bottom postion 2
lcd.print(okFar); //print okFar
lcd.setCursor(4,1); //cursor to bottom postion 4
lcd.print(both); //print sum of okClose and okFar
lcd.setCursor(6,1); //cursor to bottom postion 6
lcd.print(light); //print in range indicator

Serial.print(“Ping: “);
// Convert ping time to distance in cm and print result (0 = outside set distance range)
Serial.print(uS / US_ROUNDTRIP_CM);
Serial.println(“cm”);

}

About jcoffey

https://profiles.google.com/jerry.coffey/posts
This entry was posted in commentary. Bookmark the permalink.