How to define ON/OFF on a pin to print on LCD as ON/OFF

Good Day fellow programmers,

I am new into the Arduino programming( as a hobby)

I did a few projects got some of the internet but I started to do my own as I go on,

So my latest project is a ultrasonic sensor that switches on a light bulb via a relay.
So that part I got it right and it is working perfectly.
I dug deeper and connected a LCD to display the distance what the ultrasonic sensor is picking up including when the light is on or off.

Now my problem is:
If I program the LCD to print ON/OFF I obviously get the error that none of those are defined in the scope.

I have no IDEA where to define that the digitalWrite on the pin must be equal to ON or OFF.

Can anyone please give me some ideas or as sits me on what I am doing wrong

Kind Regards.

See the attachment for the coding. I know the coding has a error, Its just to show what I am trying to accomplish.

Motion activated light with LCD.ino (3.01 KB)

// Use the HC-SR04 to detect movement after 13 CM to turn on a light for 15 seconds

#define trigPin 7                       //Define the HC-SE04 triger on pin 6 on the arduino
#define echoPin 6                       //Define the HC-SE04 echo on pin 5 on the arduino 
#define digitalWrite(HIGH) = ON
#define digitalWrite(LOW) = OFF
#include <LiquidCrystal.h> //Load Liquid Crystal Library
LiquidCrystal LCD(12, 11, 5, 4, 3, 2);  //Create Liquid Crystal Object called LCD

void setup() {

  Serial.begin (9600);                //Start the serial monitor
  pinMode(trigPin, OUTPUT);           //set the trigpin to output
  pinMode(echoPin, INPUT);            //set the echopin to input
  pinMode (8, OUTPUT);             //set the bulb on pin 9 to output
  LCD.begin(16, 2); //Tell Arduino to start your 16x2 LCD
  LCD.setCursor(0, 0); //Set LCD cursor to upper left corner, column 0, row 0
  LCD.print("Distance:");  //Print Message on First Row
  LCD.setCursor(10, 0); //Set LCD cursor to upper left corner, column 10, row 0
  LCD.print("Light:");  //Print Message on First Row
}

void loop() {

  int duration, distance;             //Define two intregers duration and distance to be used to save data
  digitalWrite(trigPin, HIGH);        //write a digital high to the trigpin to send out the pulse
  delayMicroseconds(500);             //wait half a millisecond
  digitalWrite(trigPin, LOW);         //turn off the trigpin
  duration = pulseIn(echoPin, HIGH);  //measure the time using pulsein when the echo receives a signal set it to high
  distance = (duration / 2) / 29.1;   //distance is the duration divided by 2 becasue the signal traveled from the trigpin then back to the echo pin, then devide by 29.1 to convert to centimeters

  LCD.setCursor(0, 1); //Set the cursor to the first column of the second row
  LCD.print("                "); //Print blanks to clear the row
  LCD.setCursor(0, 1);  //Set Cursor again to the first column of the second row
  LCD.print(distance); //Print measured distance
  LCD.print(" CM ");  //Print your units
  delay(100); //Pause to let things settle
  LCD.setCursor(10, 1);  //Set Cursor again to the first column of the second row
  LCD.print("ON/OFF"); //Print measured distance

  if (distance < 13)                 //if the distance is less than 13 CM
  {
    Light();                           //execute the Light subroutine below
  }

  Serial.print(distance);             //Dispaly the distance on the serial monitor
  Serial.println(" CM");              //in centimeters
  delay(500);                         //delay half a second
}

void Light()                            //Start the Light subroutine
{
  digitalWrite(8, HIGH);             //turn on the light(Display on LCD ON)
  delay (10000);                        //wait 10 seconds
  digitalWrite(8, LOW);              //turn off the light(Display on LCD OFF)

  LCD.setCursor(10, 1);  //Set Cursor again to the first column of the second row
  LCD.print("ON/OFF"); //Print measured distance

}

I am new to this forum as well, Must I just copy and paste the code in here?

is this the error message you get as well?

Arduino: 1.8.12 (Windows 10), Board: "Arduino Due (Programming Port)"

example1:26:29: error: macro "digitalWrite" passed 2 arguments, but takes just 1

digitalWrite(trigPin, HIGH); //write a digital high to the trigpin to send out the pulse

^

example1:28:28: error: macro "digitalWrite" passed 2 arguments, but takes just 1

digitalWrite(trigPin, LOW); //turn off the trigpin

^

example1:53:23: error: macro "digitalWrite" passed 2 arguments, but takes just 1

digitalWrite(8, HIGH); //turn on the light(Display on LCD ON)

^

example1:55:22: error: macro "digitalWrite" passed 2 arguments, but takes just 1

digitalWrite(8, LOW); //turn off the light(Display on LCD OFF)

^

Multiple libraries were found for "LiquidCrystal.h"
Used: C:\Users\Johan\Documents\Arduino\libraries\LiquidCrystal
Not used: C:\Program Files (x86)\Arduino\libraries\LiquidCrystal
exit status 1
macro "digitalWrite" passed 2 arguments, but takes just 1

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

It is a really bad idea to redefine a standard Arduino library function.

#define digitalWrite(HIGH) = ON

Please explain what you are really trying to do.

jremington:
It is a really bad idea to redefine a standard Arduino library function.

#define digitalWrite(HIGH) = ON

Please explain what you are really trying to do.

As jremington just mentioned: this error "digitalWrite" passed 2 arguments, but takes just 1" should tell you that you have defined a standard function twice

another tip for you: digitalWrite() - Arduino Reference

Thanks for all the replies,
What I am trying to achieve is that if digitalWrite(8, HIGH) Pin 8 is then (ON), So I want to achieve that on the LCD should it display ON if the pin is ON or OFF (when digitalWrite(8,LOW).
So when pin 8 is high then HIGH must be equal to ON so that the LCD print be ON.
And vice versa for pin 8 (LOW) = OFF.
I hope that this explains what I am trying to achieve

fnb111:
is this the error message you get as well?

Arduino: 1.8.12 (Windows 10), Board: "Arduino Due (Programming Port)"

example1:26:29: error: macro "digitalWrite" passed 2 arguments, but takes just 1

digitalWrite(trigPin, HIGH); //write a digital high to the trigpin to send out the pulse

^

example1:28:28: error: macro "digitalWrite" passed 2 arguments, but takes just 1

digitalWrite(trigPin, LOW); //turn off the trigpin

^

example1:53:23: error: macro "digitalWrite" passed 2 arguments, but takes just 1

digitalWrite(8, HIGH); //turn on the light(Display on LCD ON)

^

example1:55:22: error: macro "digitalWrite" passed 2 arguments, but takes just 1

digitalWrite(8, LOW); //turn off the light(Display on LCD OFF)

^

Multiple libraries were found for "LiquidCrystal.h"
Used: C:\Users\Johan\Documents\Arduino\libraries\LiquidCrystal
Not used: C:\Program Files (x86)\Arduino\libraries\LiquidCrystal
exit status 1
macro "digitalWrite" passed 2 arguments, but takes just 1

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

Yes I did get that errors but it made sense to that I can't assign ON or OFF to the HIGH value I just tried it. I did remove it, The code works, Except that when pin 8 is high there should stand ON on the LCD and OFF when low

Post your code as it is now.
In code tags.

Which pin tells you “ON” or “OFF”? Presumably you want something like…

if (digitalRead(8))
{
  LCD.print("ON");
}
else
{
  LCD.print("OFF");
}
LCD.print(digitalRead (8) == HIGH ? "ON : "OFF");

jremington:
It is a really bad idea to redefine a standard Arduino library function.

#define digitalWrite(HIGH) = ON

Please explain what you are really trying to do.

...or indeed to use an equals sign between the two parts of a #define.

@TheMemberFormerlyKnownAsAWOL , yes that's the compact way to do it.
But really confusing for a beginner. For them "if" is much more readable/understandable...

#define trigPin 7                      
#define echoPin 6                       
#define bulb 8                         
#include <LiquidCrystal.h> //Load Liquid Crystal Library
LiquidCrystal LCD(12, 11, 5, 4, 3, 2);  //Create Liquid Crystal Object called LCD

void setup()
  {
    Serial.begin (9600);               
    pinMode(trigPin, OUTPUT);           
    pinMode(echoPin, INPUT);            
    pinMode (bulb, OUTPUT);             
    
LCD.begin(16,2); 
LCD.setCursor(0,0); 
LCD.print("Distance:"); 
LCD.setCursor(10,0);  
LCD.print("Light:"); 
  }
  
void loop()

  {
    int duration, distance;            
    digitalWrite(trigPin, HIGH);       
    delayMicroseconds(200);            
    digitalWrite(trigPin, LOW);         
    duration = pulseIn(echoPin, HIGH); 
    distance = (duration/2) / 29.1;    

LCD.setCursor(0,1);
LCD.print("                ");
LCD.setCursor(0,1);  
LCD.print(distance); 
LCD.print(" CM "); 
LCD.setCursor(10,1); 
LCD.print("ON/OFF"); 

    
    if (distance < 10)                
    {        
     Light();                           
    }
    
    Serial.print(distance);            
    Serial.println(" CM");              
    delay(500);                         
  }
  
void Light()                           
  {
  digitalWrite(bulb, HIGH);             //turn on the light(LCD must display ON)
  delay (15000);                        
  digitalWrite(bulb, LOW);              //turn off the light(LCD must display OFF)
  }

This is the code currently. When the Light is on,
I want the LCD to print “ON”,
When the light is OFF the LCD must print “OFF”

void Light()                           
  {
  digitalWrite(bulb, HIGH);             //turn on the light(LCD must display ON)
//print "ON" to LCD here
  delay (15000);                       
  digitalWrite(bulb, LOW);              //turn off the light(LCD must display OFF)
//print "LOW" to LCD here
  }

pcbbc:
Which pin tells you "ON" or "OFF"? Presumably you want something like...

if (digitalRead(8))

{
 LCD.print("ON");
}
else
{
 LCD.print("OFF");
}

So I quickly tried this but the LCD print stays on OFF.
You're assumption is right, Pin 8 must tell me between ON or OFF

UKHeliBob:

void Light()                           

{
  digitalWrite(bulb, HIGH);            //turn on the light(LCD must display ON)
//print "ON" to LCD here
  delay (15000);                     
  digitalWrite(bulb, LOW);              //turn off the light(LCD must display OFF)
//print "LOW" to LCD here
  }

UKHeliBob, Thank you. So the ON part works, The OFF part doesn't work, When High is triggered it Show ON on the LCD, But when it is OFF, OFF show for a couple of milliseconds then disappear.

void Light()                           
  {
  digitalWrite(bulb, HIGH);             //turn on the light(LCD must display ON)
LCD.setCursor(10,1);   
LCD.print(digitalRead (8) == HIGH ? "ON" : "OFF");
  delay (15000);                        
  digitalWrite(bulb, LOW);              //turn off the light(LCD must diaply OFF)  
LCD.setCursor(14,1);   
LCD.print(digitalRead (8) == LOW ? "OFF" : "ON");
  }

Did it like this now and still the same, ON show but not OFF.

Please post your full program as it is now.

My guess is that you are clearing or initialising the LCD after writing "OFF"
Purely as an experiment put a delay(5000) after writing "OFF" to the LCD
NOTE : this is a test, NOT a solution

#define trigPin 7                      
#define echoPin 6                       
#define bulb 8                         
#include <LiquidCrystal.h> //Load Liquid Crystal Library
LiquidCrystal LCD(12, 11, 5, 4, 3, 2);  //Create Liquid Crystal Object called LCD

void setup()
  {
    Serial.begin (9600);               
    pinMode(trigPin, OUTPUT);           
    pinMode(echoPin, INPUT);            
    pinMode (bulb, OUTPUT);             
    
LCD.begin(16,2); 
LCD.setCursor(0,0); 
LCD.print("Distance:"); 
LCD.setCursor(10,0);  
LCD.print("Light:"); 
  }
  
void loop()

  {
    int duration, distance;            
    digitalWrite(trigPin, HIGH);       
    delayMicroseconds(200);            
    digitalWrite(trigPin, LOW);         
    duration = pulseIn(echoPin, HIGH); 
    distance = (duration/2) / 29.1;    

LCD.setCursor(0,1);
LCD.print("                ");
LCD.setCursor(0,1);  
LCD.print(distance); 
LCD.print(" CM ");     
    if (distance < 10)                
    {        
     Light();                           
    }
    
    Serial.print(distance);            
    Serial.println(" CM");              
    delay(500);                         
  }
  
void Light()                           
  {
  digitalWrite(bulb, HIGH);             //turn on the light(LCD must display ON)
LCD.setCursor(10,1);   
LCD.print(digitalRead (8) == HIGH ? "ON" : "OFF");
  delay (15000);                        
  digitalWrite(bulb, LOW);              //turn off the light(LCD must diaply OFF)  
  delay (5000);
LCD.setCursor(14,1);   
LCD.print(digitalRead (8) == LOW ? "OFF" : "ON");
  }

This is how the code is now, Still the same.