How to turn on/off LCD screen through pushbutton?

I tried several ways but to no avail need help please :slight_smile:

This is my code:
void aaronProgram()
{

digitalWrite(trigPin, LOW);
delayMicroseconds(2);

digitalWrite(trigPin, HIGH);
delayMicroseconds(10);

digitalWrite(trigPin, LOW);
delayMicroseconds(2);

duration = pulseIn(echoPin, HIGH);

//Calculate the distance (in mm) based on the speed of sound.
distance = (duration/2) / 29.1;

if (distance >= maximumRange || distance <= minimumRange){
/* Send a negative number to computer and Turn LED ON
to indicate “out of range” */
// set the cursor to column 0, line 1
// (note: line 1 is the second row, since counting begins with 0):
lcd.clear();
lcd.setCursor(0, 1);
// print the number of seconds since reset:
lcd.print(“Not In Range!”);
digitalWrite(redPin, HIGH);
digitalWrite(greenPin, LOW);
currentState = 1;
}

else {
/* Send the distance to the computer using Serial protocol, and
turn LED OFF to indicate successful reading. */
// set the cursor to column 0, line 1
// (note: line 1 is the second row, since counting begins with 0):
lcd.clear();
lcd.setCursor(0, 1);
lcd.print(distance);
lcd.print(“cm”);
lcd.setCursor(1,0);
lcd.print(“Pull-Ups:”);
digitalWrite(redPin,LOW);
digitalWrite(greenPin, HIGH);
currentState = 0;
}

if(currentState != previousState){
if(currentState == 0){

pullUp = pullUp + 1;
lcd.setCursor(12,0);
lcd.print(pullUp);
}

previousState = currentState;
}

proximity=map(distance, 0, 45, 8, 0);
if (proximity <= 0){
proximity=0;
}
else if (proximity >= 3 && proximity <= 4){
tone(buzzer, 4000, 200);
delay(50);
}
else if (proximity >= 5 && proximity <= 6){
tone(buzzer,5000, 200);
delay(300);
}

else if (proximity >= 7 && proximity <= 8){
tone(buzzer, 1000, 200);
delay(600);
}

noTone(buzzer);
}

A couple of things.

First, please post your code using the code buttons.

Second, an Arduino requires the following logic.

void setup() {
  // put your setup code here, to run once:

}

void loop() {
  // put your main code here, to run repeatedly:

}

Third, I see variable names with no declaration like:

digitalWrite(trigPin, LOW);   
 delayMicroseconds(2);

Where do you define trigPin?

I already declared all the pins necessary.
Sorry I shall post the entire code here and also attached the arduino file for it:

#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 10, 5, 4, 3, 2);
const int buttonPin = 51;
const int ledPin = 53;
const int buttonPin1 = 43;
const int buzzerPin = 41;
const int buttonPin2 = 49; //XXX
const int ledPin2 = 47; //XXX
const int buttonPin3 = 48;

int photocellPin = 0; // the cell and 10K pulldown are connected to a0
int photocellReading; // the analog reading from the sensor divider
int LEDpin = 45; // connect Red LED to pin 11 (PWM pin)
int LEDbrightness; //

int ledState = HIGH;
int buttonState;
int lastButtonState = LOW;
int buzzerState = HIGH;
int buttonState1;
int lastButtonState1 = LOW;
int ledState2 = HIGH; // XXX
int buttonState2; // XXX
int lastButtonState2 = LOW; // XXX
int buttonState3;
int lastButtonState3 = LOW;

unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 50;
unsigned long lastDebounceTime1 = 0;
unsigned long debounceDelay1 = 50;
unsigned long lastDebounceTime2 = 0; // XXX
unsigned long debounceDelay2 = 50; // XXX
unsigned long lastDebounceTime3 = 0;
unsigned long debounceDelay3 = 50;

int backLight = 13; //pin 13 for Background-light
int currentState = 0;
int previousState = 0;
#define echoPin 7 // Echo Pin
#define trigPin 8 // Trigger Pin
#define redPin 9 //Pin for reed LED
#define greenPin 6 //Pin for green LED
#define buzzer A1
int pullUp = 0; //used to count pullups
int maximumRange = 150; // Maximum range needed
int minimumRange = 0; // Minimum range needed
long duration, distance; // Duration used to calculate distance
int proximity=0;

void setup() {

pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);
pinMode(buttonPin1, INPUT);
pinMode(buzzerPin, OUTPUT);
pinMode(buttonPin2, INPUT); //XXX
pinMode(ledPin2, OUTPUT); //XXX
pinMode(buttonPin3, INPUT);
Serial.begin(9600); // XXX

digitalWrite(ledPin, ledState);
digitalWrite(buzzerPin, buzzerState);
digitalWrite(ledPin2, ledState2); // XXX

Serial.begin(9600);
pinMode(buzzer, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(trigPin, OUTPUT);
pinMode(redPin, OUTPUT);
pinMode(greenPin,OUTPUT);

pinMode(backLight, OUTPUT);
digitalWrite(backLight, HIGH); // turn backlight on. Replace ‘HIGH’ with ‘LOW’ to turn it off.

// set up the LCD’s number of columns and rows:
lcd.begin(16, 2); // lcd rows and columns
// title of sorts

}

void loop() {

thomasProgram();
aaronProgram();

}

void thomasProgram()
{
{
photocellReading = analogRead(photocellPin);

Serial.print("Analog reading = ");
Serial.println(photocellReading); // the raw analog reading

// LED gets brighter the darker it is at the sensor
// that means we have to -invert- the reading from 0-1023 back to 1023-0
photocellReading = 1023 - photocellReading;
//now we have to map 0-1023 to 0-255 since thats the range analogWrite uses
LEDbrightness = map(photocellReading, 0, 1023, 0, 255);
analogWrite(LEDpin, LEDbrightness);

delay(100);

int reading = digitalRead(buttonPin);

if (reading != lastButtonState) {
lastDebounceTime = millis();
}

if ((millis() - lastDebounceTime) > debounceDelay) {

if (reading != buttonState) {
buttonState = reading;

if (buttonState == HIGH) {
ledState = !ledState;
}
}
}

digitalWrite(ledPin, ledState);

lastButtonState = reading;

int reading1 = digitalRead(buttonPin1);

if (reading1 != lastButtonState1) {
lastDebounceTime1 = millis();
}

if ((millis() - lastDebounceTime1) > debounceDelay1) {

if (reading1 != buttonState1) {
buttonState1 = reading1;

if (buttonState1 == HIGH) {
buzzerState = !buzzerState;
}
}
}

digitalWrite(buzzerPin, buzzerState);

lastButtonState1 = reading1;

int reading2 = digitalRead(buttonPin2);

if (reading2 != lastButtonState2) {
lastDebounceTime2 = millis();
}

if ((millis() - lastDebounceTime2) > debounceDelay2) {

if (reading2 != buttonState2) {
buttonState2 = reading2;

if (buttonState2 == HIGH) {
ledState2 = !ledState2;
}
}
}

digitalWrite(ledPin2, ledState2);

lastButtonState2 = reading2;
}

}

void aaronProgram()
{

digitalWrite(trigPin, LOW);
delayMicroseconds(2);

digitalWrite(trigPin, HIGH);
delayMicroseconds(10);

digitalWrite(trigPin, LOW);
delayMicroseconds(2);

duration = pulseIn(echoPin, HIGH);

//Calculate the distance (in mm) based on the speed of sound.
distance = (duration/2) / 29.1;

if (distance >= maximumRange || distance <= minimumRange){
/* Send a negative number to computer and Turn LED ON
to indicate “out of range” */
// set the cursor to column 0, line 1
// (note: line 1 is the second row, since counting begins with 0):
lcd.clear();
lcd.setCursor(0, 1);
// print the number of seconds since reset:
lcd.print(“Not In Range!”);
digitalWrite(redPin, HIGH);
digitalWrite(greenPin, LOW);
currentState = 1;
}

else {
/* Send the distance to the computer using Serial protocol, and
turn LED OFF to indicate successful reading. */
// set the cursor to column 0, line 1
// (note: line 1 is the second row, since counting begins with 0):
lcd.clear();
lcd.setCursor(0, 1);
lcd.print(distance);
lcd.print(“cm”);
lcd.setCursor(1,0);
lcd.print(“Pull-Ups:”);
digitalWrite(redPin,LOW);
digitalWrite(greenPin, HIGH);
currentState = 0;
}

if(currentState != previousState){
if(currentState == 0){

pullUp = pullUp + 1;
lcd.setCursor(12,0);
lcd.print(pullUp);
}

previousState = currentState;
}

proximity=map(distance, 0, 45, 8, 0);
if (proximity <= 0){
proximity=0;
}
else if (proximity >= 3 && proximity <= 4){
tone(buzzer, 4000, 200);
delay(50);
}
else if (proximity >= 5 && proximity <= 6){
tone(buzzer,5000, 200);
delay(300);
}

else if (proximity >= 7 && proximity <= 8){
tone(buzzer, 1000, 200);
delay(600);
}

noTone(buzzer);
}

AaronAndThomas.ino.ino (6.04 KB)

I shall post the entire code

but I won't use code tags.

There are two programs inside this code. One is mine(Aaron's program) and one is Thomas. I tried adding:

if (reading3 != lastButtonState3) { lastDebounceTime3 = millis(); }

if ((millis() - lastDebounceTime3) > debounceDelay3) {

if (reading3 != buttonState3) { buttonState3 = reading3;

if (buttonState3 == HIGH) { buzzerState3 = !buzzerState3; } } } I am not sure what is wrong as it works with my friend's Thomas program but not with mine (Aaron's program)

I am not sure what is wrong as it works with my friend's Thomas program but not with mine

It does work with yours. It just doesn't do what you expect. So, clearly your expectations are wrong. In order for us to tell you how they are wrong, you have to tell us what the code actually does WITH YOUR HARDWARE and what you expect it to do. Then, we can explain why your expectations are wrong, and perhaps suggest what to change so that your expectations are valid.

And what's wrong with you! After two people telling you to use code tags still no code tags!

Variable names with a number = arrays ;)

And if you find debouncing hard (or have done it once), just grab a library. Why do people find it no problem to use libraries for servo's and LCD's but keep trying to do the debouncing themselves. Just make things easy and grab a library like Bounce2 and be done with it.