Misc-small-projects/School/vg1/Dataelektronikk/Ultrasonic Distance.ino

39 lines
807 B
Arduino
Raw Normal View History

2016-04-14 09:00:08 +02:00
#include <NewPing.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
2016-04-14 10:07:54 +02:00
// Our LCD Screen uses 0x3F, but replace this for your LCD's adress
2016-04-14 09:00:08 +02:00
LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
2016-04-14 10:07:54 +02:00
// Set the first number to the trigger pin, second to echo
NewPing sonar(12,11);
2016-04-14 09:00:08 +02:00
void setup()
{
2016-04-14 10:07:54 +02:00
// Initializes LCD screen to a 16x2 screen
2016-04-14 09:00:08 +02:00
lcd.begin(16,2);
2016-04-14 10:07:54 +02:00
// Blinks LCD three times, leavig the screen on at the end, easy to see start/restart happened
2016-04-14 09:00:08 +02:00
for(int i = 0; i< 3; i++)
{
lcd.backlight();
delay(250);
lcd.noBacklight();
delay(250);
}
lcd.backlight();
2016-04-14 10:07:54 +02:00
// Sets the cursor to top left
2016-04-14 09:00:08 +02:00
lcd.setCursor(0,0);
lcd.print("Distance in cm");
}
char cm[3];
2016-04-14 09:00:08 +02:00
void loop()
{
lcd.setCursor(0,1);
sprintf(cm, "%.3i", sonar.ping_cm());
lcd.print(cm);
2016-04-14 09:00:08 +02:00
delay(50);
}