Hi all, well i'm new in arduino world, in my sketch:
Ping + Servo + Lcd Serial connected to my Arduino.
if the ping detected an object far then 40 cm, it will display on the second line of my lcd the duration, and the servo on 0 degree position, and if <40, the lcd display on the first line the distance and the 40 cm is map to my servo, it would be nice if some one can verify my code (it's working good) but i need to know if there's any simple or integrated version, thanks in advance, i will post a video soon ![]()
Regards,
Sam
/* Ping))) Sensor + servo + lcd
The circuit:
* +V connection of the PING))) attached to +5V
* GND connection of the PING))) attached to ground
* SIG connection of the PING))) attached to digital pin 7
* Led to pin 13
* lcd serial to TX
* servo to pin 9
*/
#include <Servo.h>
const int pingPin = 7;
int ledPin = 13; // LED connected to digital pin 13
Servo myservo;
void setup() {
myservo.attach(9);
Serial.begin(9600); // initialize serial communication:
Serial.print(0xfe, BYTE);
Serial.print(0x41, BYTE); // Backlight ON
pinMode(ledPin, OUTPUT); // Sets the digital pin as output
}
void loop()
{
// establish variables for duration of the ping,
// and the distance result in inches and centimeters:
long duration, cm;
// 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:
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(15);
digitalWrite(pingPin, LOW);
delayMicroseconds(20);
// The same 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.
pinMode(pingPin, INPUT);
duration = pulseIn(pingPin, HIGH);
// convert the time into a distance
cm = microsecondsToCentimeters(duration);
if (cm < 40){
Serial.print(0xfe, BYTE);
Serial.print(0x52, BYTE); // back light on
Serial.print(0xfe, BYTE);
Serial.print(0x51, BYTE); // clear screen
Serial.print(0xfe, BYTE);
Serial.print(0x45, BYTE); // cursor command
Serial.print(0x02, BYTE); // column one
Serial.print(0x01, BYTE); // Row 1
Serial.print("Dist est:");
Serial.print(cm);
Serial.print("cm");
Serial.println();
digitalWrite(ledPin, HIGH);
{
cm = map(cm, 1, 40, 0, 179);
myservo.write(cm);
delay(15);
}
}
else
{
Serial.print(0xfe, BYTE);
Serial.print(0x51, BYTE); // clear screen command
Serial.print(0xfe, BYTE);
Serial.print(0x45, BYTE); // cursor command
Serial.print(0x02, BYTE); // column 1
Serial.print(0x02, BYTE); // Row 2
Serial.print("IntV:");
Serial.print(duration);
Serial.print("ms");
Serial.println();
Serial.print(0xfe, BYTE);
Serial.print(0x48, BYTE);
delay (500);
digitalWrite(ledPin, LOW);
myservo.write(0);
delay(15);
}
delay(20);
}
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;
}
PS: info about lcd serial interface
http://tinyurl.com/2wz9h79