Stap 2:
De code om het Hex-adres kan worden gevonden via de link hier gegeven
https://brainy-bits.com/tutorials/Connect-a-charac...
Daarna moeten we het gebruik van die code in de belangrijkste code hier
#include <Wire.h><br>#include <LCD.h> #include <LiquidCrystal_I2C.h> #include <NewPing.h> #define I2C_ADDR 0x27 // Add your address here. #define Rs_pin 0 #define Rw_pin 1 #define En_pin 2 #define BACKLIGHT_PIN 3 #define D4_pin 4 #define D5_pin 5 #define D6_pin 6 #define D7_pin 7 #define ECHO_PIN 11 // Arduino pin tied to echo pin on the ultrasonic sensor. #define TRIGGER_PIN 12 // Arduino pin tied to trigger pin on the ultrasonic sensor. #define MAX_DISTANCE 500 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400500cm.
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.<br>LiquidCrystal_I2C lcd(I2C_ADDR,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin); void setup() { pinMode(7,OUTPUT); pinMode(6,OUTPUT); lcd.begin (16,2); // <<-- our LCD is a 20x4, change for your LCD if needed // LCD Backlight ON lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE); lcd.setBacklight(HIGH); lcd.home (); // go home on LCD lcd.print("Range Finder"); } void loop() { digitalWrite(TRIGGER_PIN, HIGH); delayMicroseconds(1000); digitalWrite(TRIGGER_PIN,LOW); unsigned int uS = sonar.ping(); // Send ping, get ping time in microseconds (uS). unsigned int dist = sonar.convert_cm(uS); // Convert into centimeters
if(dist>=200 || dist<=0){ lcd.clear(); lcd.setCursor (0,1); // go to start of 2nd line lcd.print("Distance:"); lcd.setCursor (0,3); // go to start of 4th line lcd.print("Ping: "); lcd.print("Out of Range"); }else if(dist >= 0 && dist <=50){ lcd.clear(); beepFast(); lcd.setCursor (0,1); // go to start of 2nd line lcd.print("Distance:"); lcd.setCursor (0,3); // go to start of 4th line lcd.print("Ping: "); lcd.print(dist); lcd.print(" cm "); } else{ lcd.clear(); Silent(); lcd.print(dist); lcd.print(" cm "); } delay(1000); } void beepFast(){ tone(8,440,200); digitalWrite(7,HIGH); delay(50); noTone(8); digitalWrite(7,LOW); delay(300); }
void Silent(){ noTone(8); digitalWrite(6,HIGH); delay(300); digitalWrite(6,LOW); }