HC-SR04 and LCD

Hello, I am trying to practice programming and I have attempted to combine two different pieces of code.
The first bit of code uses the ultrasonic sensor to measure distance and if the is nothing in front of the sensor a (green) led will show and if an object comes closer than a certain distance a (red) led will show.
I wanted to implement a basic LCD-LED code that would print “Activated” when the red led went on when an object comes close to the sensor. Then clear the screen or print nothing when the (green) led is on.

I managed to get the code to work but when I move my hand away from the sensor(turning back on the Green led) the “Activated fades away rather slowly”
which I do want the “activated” to go away when nothing is in front of the sensor and the green led is on but I don’t think it is supposed to be that slow.

Can anyone see where i can improve this?
Thanks

#include <NewPing.h>

#define TRIGGER_PIN  12  // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN     11  // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 400 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
unsigned int distance;
byte redLed = 8;
byte greenLed = 10;
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.

#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd = LiquidCrystal_I2C(0x27, 20, 4);
int ledPin=9;

void setup()
{
   Serial.begin(115200); // Open serial monitor at 115200 baud to see ping results.
   pinMode(redLed, OUTPUT);
   pinMode(greenLed, OUTPUT);

lcd.init();
  lcd.backlight();
}

void loop()
{
   delay(50);                     // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
   distance = sonar.ping_cm();
   Serial.print("Ping: ");
   Serial.print(distance); // Send ping, get distance in cm and print result (0 = outside set distance range)
   Serial.println("cm");

if (distance > 40) {
  // d > 40 
   lcd.clear();
  digitalWrite(greenLed, HIGH);
  digitalWrite(redLed, LOW);
} else
if (distance != 0) {
  // d inferior or equal to 40 but non 0
  
 lcd.setCursor(0,0);
 lcd.print("Activate");
  
  digitalWrite(redLed, HIGH);
  digitalWrite(greenLed, LOW);
} else {
  // d is 0
  
 lcd.setCursor(0,0);
 lcd.print("Activate");
  
  digitalWrite(redLed, HIGH);
  digitalWrite(greenLed, LOW);
}
}

any help please.
Thanks!

any help please.
Thanks!

A quick look through your code i would suspect that you are refreshing the screen each loop causing the issue you see.

I typically implement a state change method for dealing with this.

int lastState;

if(valChange != lastState){

if(valChange == 0){

  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Hello World");

}else if (valChange == 1){
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Hello World");

}
}

A better way so i have been told rather than clearing the screen each time is to set the cursor to the known position and only overwrite the area required without lcd.clear but i must be honest i don't do this.

I haven't used NewPing, but I took a quick look at the library. It looks to me like when you get out of range and get no echo it returns 0 as the distance. Look at what the serial monitor is printing out. Are you seeing a bunch of 0's in there when you aren't blocking the sensor? You have that set to print "Activated" to the screen as well. I probably wouldn't. No ping sensor is going to read 0 distance for real. It just wouldn't hear the ping, it can't switch over fast enough. So a 0 reading will almost always be an out of range either too far away or too close.

Delta_G:
I haven't used NewPing, but I took a quick look at the library. It looks to me like when you get out of range and get no echo it returns 0 as the distance. Look at what the serial monitor is printing out. Are you seeing a bunch of 0's in there when you aren't blocking the sensor? You have that set to print "Activated" to the screen as well. I probably wouldn't. No ping sensor is going to read 0 distance for real. It just wouldn't hear the ping, it can't switch over fast enough. So a 0 reading will almost always be an out of range either too far away or too close.

On my LCD screen it only prints one message--> "Activated" when something comes across the sensor.
then when there is nothing there it lights the greenLed and nothing is displayed on the screen.

tjones9163:
On my LCD screen it only prints one message--> "Activated" when something comes across the sensor.
then when there is nothing there it lights the greenLed and nothing is displayed on the screen.

Well you said it was fading out instead of just turning off. Usually that means it is being written and cleared over and over.

You do indeed have two places in your code where you have it write "Activate" to the screen, in the second if and in the else. Change one of them so that you know which is causing the problem.