The lcd part works but I cant get the leds to work.I think it has something to do with the If Else statements, but I cant find the problem. Here is the code I have written.
Code: Select all
//DistanceMeter with LEDs
//Made by Pieterdekunder
#define TRIGGER_PIN 12
#define ECHO_PIN 11
#define MAX_DISTANCE 200
#include <NewPing.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);
void setup() {
Serial.begin(115200);
lcd.begin(16,2);
for(int i = 0; i< 3; i++)
{
lcd.backlight();
delay(250);
lcd.noBacklight();
delay(250);
}
lcd.backlight();
}
void loop() {
lcd.setCursor(0,0);
unsigned int uS = sonar.ping();
lcd.print("Distance:");{
lcd.setCursor(10,0);
lcd.print(" ");
lcd.print(uS / US_ROUNDTRIP_CM);
lcd.println("cm ");
}
if (uS / US_ROUNDTRIP_CM > 50)
{
digitalWrite(13, HIGH);
}
else if (uS / US_ROUNDTRIP_CM >= 100)
{
digitalWrite(13, HIGH);
digitalWrite(8, HIGH);
}
else
{
digitalWrite(13, HIGH);
digitalWrite(8, HIGH);
digitalWrite(7, HIGH);
}
}
Im sorry if I posted this in the wrong category, because I couldnt find a coding category.