Really new. Need help please

im trying to add an else/if statement into my sketch. alone the code works, but once added to my project it doesnt work. its supposed to work, once the button is pressed the backight turns on for whatever amount of time then shuts off, im about 2 weeks new to this and this has had me stumped, please help me :frowning:

code by itself works flawlessly. Backlight turns on for 10 seconds after button press then shuts off

#include <LiquidCrystal.h>
//Liquid crystal pins
LiquidCrystal lcd(7, 8, 9, 10, 11 , 12);

 void setup() {
 
  pinMode(2, INPUT);    // set the switch pin to be an input use a 10k resistor
  pinMode(6, OUTPUT);   // set the backlight 
// try to use a value instead of "high" to change brightness if defualt is to bright
}


void loop() {
   // read the switch input:
   if (digitalRead(2) == HIGH) {
     // if the switch is closed:
     digitalWrite(6, HIGH);    // turn on the backlight
   delay(10000);
   digitalWrite(6, LOW);          // turn off backlight
   }
   else {
     // if the switch is open:
     digitalWrite(6, LOW);     // turn off the backlight
   
   }
 }

code added to master project code. When button is pressed the backlight does not turn on.
Is there a certian order the else/if needs to be injected into the code?

I have no errors popping up. Just no light turning on…

#include <OneWire.h>
#include <DallasTemperature.h>
#include <LiquidCrystal.h>

//Liquid crystal pins
LiquidCrystal lcd(7, 8, 9, 10, 11 , 12);

// Data wire is plugged into pin 2 on the Arduino
#define ONE_WIRE_BUS 2

// Setup a oneWire instance to communicate with any OneWire devices 
// (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);

// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);

//int pins
//main power for fan signal is pin 3
int mainfanPin = 3;


void setup(void)
{
  // start lcd screen
  lcd.begin(16, 2);

  // try to use a value instead of high
  lcd.print("Starting up ");

  delay(2000);
  lcd.clear();
  delay(1000);
  // Start up the library
  sensors.begin();
  //Setup fan wire
  pinMode(mainfanPin, OUTPUT);
  pinMode(2, INPUT);    // button
  pinMode(6, OUTPUT);   // backlight



}


void loop(void)

{
  // read the switch input:
  if (digitalRead(2) == HIGH) {
    // if the switch is closed:
    digitalWrite(6, HIGH);    // turn on the backlight 
delay(10000);
    digitalWrite(6, LOW);          // turn off backlight
  }
  else {
    // if the switch is open:
    digitalWrite(6, LOW);     // turn off the backlight
  }

  // call sensors.requestTemperatures() to issue a global temperature
  // request to all devices on the bus
  lcd.print("Loading");
  sensors.requestTemperatures(); // Send the command to get temperatures
  lcd.print("...");
  delay(200);


  // set the cursor to column 0, line 1
  // (note: line 1 is the second row, since counting begins with 0):
  lcd.setCursor(0, 1);
  // print the number of seconds since reset:
  lcd.print("Temp ");
  lcd.print(sensors.getTempCByIndex(0));




  digitalWrite(mainfanPin, HIGH);
  delay(15000);
  digitalWrite(mainfanPin, LOW);
  delay(7000);

  lcd.clear();
  delay(1000);




  // needs improvemnt ^^^^^


  //--------------------------------------------------------
  //------------------------------------------------------ stuff to add
  //fan pwm speed as per tempurate

  //main fan power off, when tempurate is less then 40 celcius

  //main fan power on, when above 40 but less then 50, 25% duty

  //fan speed 50% when temp is over 50 but less then 60

    //fan speed 75% when temp is over 60 but less then 70

    //fan speed 100% when temp is over 70

    //turn on backlight for 1min (in progress)

  //rpm sense




}

Hi,

Welcome to the Forum

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html
then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

then come back to your posts and edit it (lower right of your post screen) add the code tags and make us happy.

Thanks.. Tom.... :slight_smile:

i edited it, sorry in my haste i dident read the rules,

im happy thats some coding i was able to do lol

Does the program work without the ‚Äúif()‚ÄĚ statement? Also, one thing to note is that your code format could be more consistent, which would make it easier for not only us trying to help you, but also yourself when double checking.

For example:

if (digitalRead(2) == HIGH) {
    // if the switch is closed:
    digitalWrite(6, HIGH);    // turn on the backlight
delay(10000);
    digitalWrite(6, LOW);          // turn off backlight
  }
  else {
    // if the switch is open:
    digitalWrite(6, LOW);     // turn off the backlight
  }

Would be better written as:

if(digitalRead(2) == HIGH) //if the switch is closed
{
    digitalWrite(6, HIGH); //turn backlight on
    delay(10000);
    digitalWrite(6, LOW); //turn backlight off
} else
{
    digitalWrite(6, LOW); //turn backlight off
}

Why don’t you test the program by replacing the whole if statement by putting /* in front and */ at the end, with a simple digitalWrite() and see if that works. If it does, the problem is the if statement. If not, the If statement is not the problem and bug is somewhere else in the code that we can help you decipher together. Update me by noon and I’ll help you out more.

Also let us know what error you are getting and more importantly which line it is on! Don’t come to the doctor and ask him what is wrong without giving the symptoms…

Cheers!
Tyler

void loop() {
   // read the switch input:
   if (digitalRead(2) == HIGH) {
     // if the switch is closed:
     digitalWrite(6, HIGH);    // turn on the backlight
   delay(10000);
   digitalWrite(6, LOW);          // turn off backlight
   }
   else {
     // if the switch is open:
     digitalWrite(6, LOW);     // turn off the backlight
   
   }
 }

From the code I can see you are switching of the pin after a delay of 10000ms. In that case the led or what ever connected to pin 6 will automatically switched off after 10000ms delay. It will not wait for the next button press.

If you 1) are using a UNO board (or similar), and 2) hook up your switch like this: pin --> switch --> gnd, does this code turn your on-board LED on and off, just like you want the back-light to work? If so, check your LCD connection. If not, check your switch, ground and other connections.

const byte ledPin = 13;
const byte button = 2;

void setup() {
  pinMode(button, INPUT_PULLUP); // pin  -->  button  -->  gnd
  pinMode(ledPin, OUTPUT);   // built in led
}

void loop() {

  // wait 10 seconds, run this code - repeat every 10 seconds
  if (digitalRead(button) == 0) {  // if the switch is closed:

    // turn on the led, wait patiently for 10 seconds, then turn it off
    digitalWrite(ledPin, HIGH);
    delay(10000);
    digitalWrite(ledPin, LOW);
    
  } else {  // if the switch is open:

    // just leave the LED off
    digitalWrite(ledPin, LOW);
  }
}

Hey there Arduino noob, I've got many YouTube videos designed for people who need some guidance when starting out, so check them out and see if you like what you see. URL in the footer of this video.

Amongst other things in my videos I do emphasise about starting off slowly and getting each tiny bit of a project working independently before trying to recreate NASA in your lounge or bedroom. It's a premise I follow to this day, both when using an Arduino and in my day job. It just makes sorting out problems a zillion times easier.

Let me know if you find the videos of use! :slight_smile:

If you want a program that responds to button presses you need to remove all the delay()s. Have a look at how millis() is used to manage timing in Several Things at a Time and in Planning and Implementing a Program

...R

The problem isent the else/if

The problem is when i add the else/if statment to my bigger (actual project) the else/if doesent work.

So

The else/if is supposed to turn the backlight on my lcd screen on for 10 seconds then shut off. When i add it to my main code it doesent do anything. I dont know how to add it into my void loop properly.

I dont know how else to explain this and im getting super frustrated at this project.

// Data wire is plugged into pin 2 on the Arduino
#define ONE_WIRE_BUS 2
 pinMode(2, INPUT);    // button

Try moving your push button, or one wire bus to a different pin.

How did i not see that -.-

Still does not turn on the backlight in the main code.

Please post the current code. How is the button switch wired to the Arduino?

With all the delays in your program, you will need to hold the switch down for approximately 30 seconds for loop to get around to read the input.

current code

#include <OneWire.h>
#include <DallasTemperature.h>
#include <LiquidCrystal.h>

//Liquid crystal pins
LiquidCrystal lcd(7, 8, 9, 10, 11 , 12);

// Data wire is plugged into pin 2 on the Arduino
#define ONE_WIRE_BUS 2

// Setup a oneWire instance to communicate with any OneWire devices 
// (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);

// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);

//int pins
//main power for fan signal is pin 3
int mainfanPin = 3;


void setup(void)
{
  // start lcd screen
  lcd.begin(16, 2);

  // try to use a value instead of high
  lcd.print("Starting up ");

  delay(2000);
  lcd.clear();
  delay(1000);
  // Start up the library
  sensors.begin();
  //Setup fan wire
  pinMode(mainfanPin, OUTPUT);
  pinMode(4, INPUT);    // button
  pinMode(6, OUTPUT);   // backlight



}


void loop(void)

{
  // read the switch input:
  if (digitalRead(4) == HIGH) {
    // if the switch is closed:
    digitalWrite(6, HIGH);    // turn on the backlight 
delay(10000);
    digitalWrite(6, LOW);          // turn off backlight
  }
  else {
    // if the switch is open:
    digitalWrite(6, LOW);     // turn off the backlight
  }

  // call sensors.requestTemperatures() to issue a global temperature
  // request to all devices on the bus
  lcd.print("Loading");
  sensors.requestTemperatures(); // Send the command to get temperatures
  lcd.print("...");
  delay(200);


  // set the cursor to column 0, line 1
  // (note: line 1 is the second row, since counting begins with 0):
  lcd.setCursor(0, 1);
  // print the number of seconds since reset:
  lcd.print("Temp ");
  lcd.print(sensors.getTempCByIndex(0));




  digitalWrite(mainfanPin, HIGH);
  delay(15000);
  digitalWrite(mainfanPin, LOW);
  delay(7000);

  lcd.clear();
  delay(1000);




  // needs improvemnt ^^^^^


  //--------------------------------------------------------
  //------------------------------------------------------ stuff to add
  //fan pwm speed as per tempurate

  //main fan power off, when tempurate is less then 40 celcius

  //main fan power on, when above 40 but less then 50, 25% duty

  //fan speed 50% when temp is over 50 but less then 60

    //fan speed 75% when temp is over 60 but less then 70

    //fan speed 100% when temp is over 70

    //turn on backlight for 1min (in progress)

  //rpm sense




}

ground> 10k resisor> wire to pin 4 on uno>switch,
5v>switch

ground> 10k resisor> wire to pin 4 on uno>switch,
5v>switch

Are you wired diagonally across the switch, and can confirm with a simple test sketch that digitalRead(4) is HIGH when pressed, and LOW when not pressed.

i updated the post above yours with an attachemnt

and yes, when i use just the code to light the backlight, with no other code, it does work flawlessly,

i know the code works, thats not my issue, my issue is adding the if/else statment into my main code, my main code that controls everything.

all i want to do is turn the backlight on for 10 seconds, so i can read it,

im using the arduino to control fans, based on temp, so its going to be doing whatever, so i want to push a button and have the backlight on for 10 seconds, so its not always on being bright in my room when im trying to sleep

i updated the post above yours with an attachemnt

That wiring arrangement could be incorrect. Have you confirmed the function of the button with a simple test sketch that just prints out the value of digitalRead(4) in a loop. Confirm that its HIGH when pressed and LOW when not pressed?

Yes.
In my original post the first code was the test and the second code is my main code

My problem is adding it into my main code it doesent work

Not trying to sound rude but is my question being understood or do i need to state my problem more clearly?

Is there a better way to turn on and off the backlight of the lcd then a else/if?
Is there a way to do that all in void setup or does it neee to exsist in the loop?

Is there a way to do that all in void setup or does it neee to exsist in the loop?

If you want the light to go on any time you push the button, you can not do it in setup() as that only occurs once. You need to read the button in loop.

and yes, when i use just the code to light the backlight, with no other code, it does work flawlessly,

One issue that I see between the backlight test code and the integrated code is that there are major delays in the program and it could take some time for the loop to come around to reading the button. If you are expecting response to a quick press like in the simple code, it won’t happen.

What happens if you hold the button down for 25 or 30 seconds? Does the backlight go on?

Hi,
Can you do me a favour and try this.
Turn your project ON, then after it has booted, HOLD the button dowsn for at least 30Seconds, yes 30Seconds and tell us what happens to the backlight.

What you do not realise is that your code takes over 20Seconds to complete 1 Loop of the code.
Because you did not have any human interactive input in your code originally you were not aware of the total effect the function delay() has.
A delay function STOPS the code, in the case of 5000, it is stopped for 5Seconds, the code does NOTHING in that 5Seconds, it doesn't update displays, it doesn't look at inputs, it doesn't LOOK FOR BUTTON presses.
Add all the delays in your code up, not the 10000 in the if/else as that is only used in the code when the code DETECTS a button push.
You code looks to see if the button is pushed ONCE every 20Seconds or more.
.
Tom.... :slight_smile: