Loop inside "if else"

I was doing a project on something like a solar irradiance meter. I should have 2 buttons, one for displaying Voltage and Current and one for display solar irradiance and temperature. I am using sensor for these parameters, and the sensor value varies from time to time, so my display values should be keep updating themselves.

Now I am using "if else" for each button in the void loop, but the data wont update unless I press the button again. Please help.

Please post your full sketch.

If possible, you should always post code directly in the forum thread as text using code tags:

  • Do an Auto Format (Tools > Auto Format in the Arduino IDE or Ctrl + B in the Arduino Web Editor) on your code. This will make it easier for you to spot bugs and make it easier for us to read.
  • In the Arduino IDE or Arduino Web Editor, click on the window that contains your sketch code.
  • Press “Ctrl + A”. This will select all the text.
  • Press “Ctrl + C”. This will copy the selected text to the clipboard.
  • In a forum reply here, click on the reply field.
  • Click the </> button on the forum toolbar. This will add the forum’s code tags markup to your reply.
  • Press “Ctrl + V”. This will paste the sketch between the code tags.
  • Move the cursor outside of the code tags before you add any additional text to your reply.
  • Repeat the above process if your sketch has multiple tabs.

This will make it easy for anyone to look at it, which will increase the likelihood of you getting help.

If the sketch is longer than the 9000 characters maximum allowed by the forum, then it’s OK to add it as an attachment. After clicking the “Reply” button, you will see an “Attachments and other settings” link.

When your code requires a library that’s not included with the Arduino IDE please post a link (using the chain links icon on the forum toolbar to make it clickable) to where you downloaded that library from or if you installed it using Library Manger (Sketch > Include Library > Manage Libraries in the Arduino IDE or Libraries > Library Manager in the Arduino Web Editor) then say so and state the full name of the library.

#include <LiquidCrystal.h>

// initialize the pin for voltage sensor
int analogInput = A1;
float vout = 0.0;
float vin = 0.0;
float Iin = 0.0;
float R1 = 30000.0; //
float R2 = 7500.0; //
int value = 0;
int buttonVI = 8;    ///    | = Button to display Voltage & Current
int buttonTemp = 9;  ///    | = Button to display Temperature & Solar Irradiance
int ldrPin = 7;      /////    |
//int value = 0;       /////    | = Temperature sensor
float voltage = 0;   /////    |
float temp = 0;      /////    |

// initialize the library by associating any needed LCD interface pin
// with the arduino pin number it is connected to
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

void setup() {
  pinMode(analogInput, INPUT);
  pinMode(buttonVI, INPUT);
  pinMode(buttonTemp, INPUT);
  digitalWrite(buttonVI, HIGH); //activate arduino internal pull up
  digitalWrite(buttonTemp, HIGH); //activate arduino internal pull up
  Serial.print("DC VOLTMETER");

  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  // Print a message to the LCD.
  lcd.print("Irradiance Meter");

void loop() {

  // read the value at analog input
  value = analogRead(analogInput);
  vout = (value * 5.0) / 1024.0; // see text
  vin = vout / (R2 / (R1 + R2));
  // R=30ohm
  Iin = vin / 30;

  Serial.print("INPUT V= ");
  Serial.println(vin, 5);

  Serial.print("INPUT I= ");
  Serial.println(Iin, 5);

  ///// Temperature sensor loop
  value = analogRead(A0);      // sensor output to arduino analog A0 pin
  voltage = value * 0.00488;
  temp = voltage * 100;


  if (digitalRead(buttonVI) == LOW) {
    lcd.setCursor(0, 0);
    lcd.print("I= ");
    lcd.print(Iin * 1000, 4);
    lcd.print("mA     ");
    //set the cursor to column 0, line 1
    // (note: line 1 is the second row, since counting begins with 0):
    lcd.setCursor(0, 1);
    lcd.print("V= ");
    lcd.print(vin, 4);

  if (digitalRead(buttonTemp) == LOW) {
    lcd.print("Temp= ");
    lcd.print(temp, 2);


Now I am using "if else" for each button in the void loop,

You don't have an if/else in the program that you posted

Almost all of the time, the Arduino is doing this


and ignoring button pushes.

Study the Blink Without Delay example to avoid this.

Your code is testing to see if the button is pressed, and then only displaying the data on the LCD if it is pressed, so of course it doesn't update when the button is not pressed.

Instead of displaying the data on the LCD when a button is pressed, use the button press to set a variable that tells you which data to display on the LCD, then each time through the loop check to see what that variable is set to, and display the appropriate information.

Basic logic would be:

If buttonVI pressed
set displaytype = VI

If buttonTemp pressed
set displaytype = Temp

If displaytype = VI
display voltage and current on LCD
else //displaytype = Temp
display temp on LCD

You have a delay() statement in the code that will limit the display update to once each 500mS, if that is too often you can increase the delay, or use millis() and set up a timer for how often to do the update, or simply store the values you are displaying, and only update the display when the value changes.

but the data wont update unless I press the button again.

The lcd display has an internal memory and the way your program is written, the display will show the last screen if no button is being pressed.

as jremmington suggests, put the display on a millis() timer, and use the buttons to switch between which one screen should be active. You will want to switch screens when a button becomes pressed instead of while it is held down. Take a look at the "State Change Detection" example in the 02 Digital section of the IDE examples.