Im a total noob on Arduino staff so any suggestions for improvement are very welcome.
I have created the follwing scetch that displays and evaluates data derived from an ultrasonic Sensor HC-SR04. I have also attached a fritizing drawing. I wish this might help someone.
/*
Aronis Projects
This Project Uses Arduino, an Ultrasonic (HC-SR04) sensor, a servo motor
and a LCD 16x02.
Data from the HC-SR04 sensor is translated via ultrasonic library and displayed on the LCD (again using the LCD library).
If the distance from target is equal or below 3 cm then the servo motor moves 180 degrees. When the distance gets greater than
3 cm then the motor moves to its starting position.
The circuit:
- LCD RS pin to digital pin 2
- LCD Enable pin to digital pin 3
- LCD D4 pin to digital pin 4
- LCD D5 pin to digital pin 5
- LCD D6 pin to digital pin 6
- LCD D7 pin to digital pin 7
- LCD R/W pin to ground
- 10K variable resistor:
- ends to +5V and ground
- wiper to LCD VO pin (pin 3)
- Servo motor signal terminal connects to arduino digital pin 11.
- HC-SR04 Trigger pin hooks to digital pin 12
- HC-SR04 Echo pin to digital pin 13
*/
// include the library code:
#include <LiquidCrystal.h>
// include the ultrasonic library
#include "Ultrasonic.h"
// ultrasonic trig pin at digital pin 12 and echo pin at digital 13
Ultrasonic ultrasonic(12,13);
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(2, 3, 4, 5, 6, 7);// include the servo library
#include <Servo.h>
Servo myservo; // create servo object to control a servo
int pos = 0; // define position as integer
int change = 0; // variable to avoid constant use of the servovoid setup() {
myservo.attach(11); // attaches the servo on pin 11 to the servo object
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.print("ARONIS PROJECTS!");
lcd.setCursor(0,1); // Go to the second line
lcd.print("****************");
delay(3000); // wait 3 secs
lcd.clear(); // Clear screen
// Print standard message
lcd.setCursor(0, 0);
lcd.print("Distance from");
lcd.setCursor(0,1);
lcd.print("target=");}
void loop() {
pos = ultrasonic.Ranging(CM); // Get the distance from sensor
lcd.setCursor(8,1);
lcd.print(ultrasonic.Ranging(CM));
lcd.print("cm ");
delay(100);if (change == 0 && pos<= 3) {
myservo.write(170);
change = 1;
}
if (change == 1 && pos> 3) {
myservo.write(1);
change = 0;
}
}
Lcd_servo_ultrasonic.fzz (9.64 KB)