DIsplay status of a PIn in LCD

Hello All,

I am trying to use push button to turn on/off 4 digital pins and would like the display the status of the pin in a LCD ( 16 x 2) as "CH1:ON /OFF". Unfortunately I am not able to get it work. Can someone help with the issue.

Thanks for the help

#include <Wire.h> 
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2);  // set the LCD address to 0x27 for a 16 chars and 2 line display
int input0 = A0; // Define the 3 analog inputs: A0, A1, A2
int input1 = A2;
int LEDR = 10;  //Define the 3 PWM digital outputs
int LEDG = 11;
int value0=0; // Create the 3 integral value type to store the analog reading values
int value1=0;

const int pushButton[] ={2,3,4,5};// define push button inputs 
const int relayPin[]={9,8,7,6};// output pins where 4 relays will be connected
String relayNames[] ={"CH1", "CH2","CH3","CH4"};// Just put name for 4 relays
int pushed[] ={0,0,0,0};// status of each buttons
int relayStatus[] ={HIGH,HIGH,HIGH,HIGH};// initial status of relay

void setup ()
{
Serial.begin(9600); //Initiate the communication to display on the screen the information received from the board
pinMode(LEDR,OUTPUT); // the digital pins will be output pins
pinMode(LEDG,OUTPUT);
lcd.init();
lcd.backlight();
  for(int i=0; i<4; i++)
  {
    pinMode(pushButton[i], INPUT_PULLUP); 
    pinMode(relayPin[i], OUTPUT);   
    digitalWrite(relayPin[i], HIGH);// initial relay status to be OFF
  }
}

void loop()
{
int value0 = analogRead(input0); // Reads value of potentiometer number 0
int value1 = analogRead(input1); // Reads value of potentiometer number 1
analogWrite(LEDR, value0/4); // Because the analog inputs have a maximum resolution of 1024 and the PWM has a 256 resolution, you have to divide the value of the analog input by 4 in order to make the analog input reading proportional to the PWM digital output intensity
analogWrite(LEDG, value1/4);

  for(int i=0; i<4; i++)
  {
    int  val = digitalRead(pushButton[i]);   
    if(val == HIGH && relayStatus[i] == LOW){
    pushed[i] = 1-pushed[i];
    delay(70);
    }// if   

  relayStatus[i] = val;
  if(pushed[i] == HIGH){
  digitalWrite(relayPin[i], LOW);
          lcd.setCursor(0,0);
          lcd.print("CH1:OFF");
          lcd.setCursor(9,0);
          lcd.print("CH2:OFF");
          lcd.setCursor(0,1);
          lcd.print("CH3:OFF");
          lcd.setCursor(9,1);
          lcd.print("CH4:OFF");}
     
   else{
   digitalWrite(relayPin[i], HIGH);
            lcd.clear();
            lcd.setCursor(0,0);
            lcd.print("CH1:ON");}
            lcd.setCursor(9,0);
            lcd.print("CH2:ON");
            lcd.setCursor(0,1);
            lcd.print("CH3:ON");
            lcd.setCursor(9,1);
            lcd.print("CH4:ON");
   }// else   

  }// for 

In what way does it fail to work ?
Start simple. What do you see if you print the value of val after each digitalRead() ? Are the values what you expect ?

please explain in detail what you WANT to get and what you receive instead.

Please show us your schematic of your setup including real pictures of your wiring.

This line of code:

would need that you are using external pulldown resistors for your switches. But we don't see what you have done.
Furthermore I would expect that you LCD flickers due to your lcd.clear() in the loop.
Only update the parts of the display which are needed.

consider

#include <Wire.h>

#define N_BUT   4

#undef MyHW
#ifdef MyHW
struct LiquidCrystal_I2C  {
    LiquidCrystal_I2C (int a, int b, int c)  { };
    void init      (void)  { };
    void backlight (void)  { };
    void setCursor (int a, int b)  { Serial.println (); };
    void print     (char *s)       { Serial.print (s); };
};

const int butPin   [N_BUT] = { A1, A2, A3, A4 };
const int relayPin [N_BUT] = { 10, 11, 12, 13 };

#else
#include <LiquidCrystal_I2C.h>

const byte butPin   [N_BUT] = {2,3,4,5};// define push button inputs
const byte relayPin [N_BUT] = {9,8,7,6};// output pins where 4 relays will be connected
#endif

LiquidCrystal_I2C lcd (0x27,16,2);


byte butState   [N_BUT];
byte relayState [N_BUT];

byte updateFlag;

enum { Off = HIGH, On = LOW };

// -----------------------------------------------------------------------------
void setup ()
{
    Serial.begin (9600);

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

    for (unsigned i=0; i < N_BUT; i++) {
        pinMode (butPin [i], INPUT_PULLUP);
        butState [i] = digitalRead (butPin [i]);

        pinMode      (relayPin [i], OUTPUT);
        digitalWrite (relayPin[i], relayState [i] = Off);
    }
}

// -----------------------------------------------------------------------------
void loop ()
{
    for (unsigned i=0; i < N_BUT; i++) {
        byte  but = digitalRead (butPin [i]);
        if (butState [i] != but)  {
            butState [i] = but;
            delay (10);         // debounce

            if (LOW == but)  {
                relayState [i] = ! relayState [i];
                digitalWrite (relayPin[i], relayState [i]);
                updateFlag = 1;
            }
        }
    }

    if (updateFlag)  {
        updateFlag = 0;

        char s [40];

        sprintf (s, "CH%d:%-3s CH%d:%-3s",
            0, On == relayState [0] ? "On" : "Off",
            1, On == relayState [1] ? "On" : "Off" );
        lcd.setCursor (0,0);
        lcd.print (s);

        sprintf (s, "CH%d:%-3s CH%d:%-3s",
            2, On == relayState [2] ? "On" : "Off",
            3, On == relayState [3] ? "On" : "Off" );
        lcd.setCursor (0, 1);
        lcd.print (s);
    }
}

That works. Thanks a lot!!!
But if include the control to PWM signals for LED I get the error "not declared in the scope!!"
Can you please help me with that as well.

@ Others: I am just trying to turn on/Off four relay with 4 different push buttons, simultaneously displaying their status in LCD. Plus the first part us used to drive two LEDS with PWM signals. Thanks.

my hardware has only 3 buttons and 4 LEDs so i removed the code to read the pots. you'll need to add that back in.

Thanks once again

I included the PWM in your code as follows

`int value0 = analogRead(input0); // Reads value of potentiometer number 0
int value1 = analogRead(input1); // Reads value of potentiometer number 1

analogWrite(LEDR, value0/4); // Because the analog inputs have a maximum resolution of 1024 and the PWM has a 256 resolution, you have to divide the value of the analog input by 4 in order to make the analog input reading proportional to the PWM digital output intensity
analogWrite(LEDG, value1/4);`

But, I get the error "LED1 not declared in the scope"

Between how can i include a text "welcome" when I start and declare the status of the four channels. I tired including them in the Void setup(), but did it displayed both the welcome and the status text.

T

don't see LED1 in what you posted. post the entire code

pull the code i posted that displays the relay states into a sub-function and call it both in setup() and loop()

if you want to display "welcome", you'll probably need a delay to keep it displayed until the relay status is displayed

sorry, here is the full code

#include <Wire.h>

#define N_BUT   4

#undef MyHW
#ifdef MyHW

struct LiquidCrystal_I2C  {
    LiquidCrystal_I2C (int a, int b, int c)  { };
    void init      (void)  { };
    void backlight (void)  { };
    void setCursor (int a, int b)  { Serial.println (); };
    void print     (char *s)       { Serial.print (s); };
};

const int butPin   [N_BUT] = { A2, A3, A4, A5 };
const int relayPin [N_BUT] = { 9, 8, 7, 6 };
int input0 = A0; // Define the  analog inputs: A0, A1
int input1 = A2;
int LED1 = 10;  //Define the 2 PWM digital outputs
int LED2 = 11;
int value0 = 0;
int value1 = 0;

#else
#include <LiquidCrystal_I2C.h>

const byte butPin   [N_BUT] = {2,3,4,5};// define push button inputs
const byte relayPin [N_BUT] = {9,8,7,6};// output pins where 4 relays will be connected
#endif

LiquidCrystal_I2C lcd (0x27,16,2);


byte butState   [N_BUT];
byte relayState [N_BUT];

byte updateFlag;

enum { OFF = HIGH, ON = LOW };

// -----------------------------------------------------------------------------
void setup ()
{
    Serial.begin (9600);
    pinMode(LED1,OUTPUT); // the digital pins will be output pins
    pinMode(LED2,OUTPUT);
    
    lcd.init ();
    lcd.backlight ();
    lcd.cursor(5,0);
    lcd.print("Welcome!");
    delay(2000);

    for (unsigned i=0; i < N_BUT; i++) {
        pinMode (butPin [i], INPUT_PULLUP);
        butState [i] = digitalRead (butPin [i]);

        pinMode      (relayPin [i], OUTPUT);
        digitalWrite (relayPin[i], relayState [i] = OFF);
    }
}

// -----------------------------------------------------------------------------
void loop ()
{
    int value0 = analogRead(input0); // Reads value of potentiometer number 0
    int value1 = analogRead(input1); // Reads value of potentiometer number 1

    analogWrite(LED1, value0/4); // Because the analog inputs have a maximum resolution of 1024 and the PWM has a 256 resolution, you have to divide the value of the analog input by 4 in order to make the analog input reading proportional to the PWM digital output intensity
    analogWrite(LED2, value1/4);
    
    for (unsigned i=0; i < N_BUT; i++) {
        byte  but = digitalRead (butPin [i]);
        if (butState [i] != but)  {
            butState [i] = but;
            delay (80);         // debounce

            if (LOW == but)  {
                relayState [i] = ! relayState [i];
                digitalWrite (relayPin[i], relayState [i]);
                updateFlag = 1;
            }
        }
    }

    if (updateFlag)  {
        updateFlag = 0;

        char s [40];

        sprintf (s, "CH%d:%-3s CH%d:%-3s",
            0, ON == relayState [0] ? "ON" : "OFF",
            1, OFF == relayState [1] ? "ON" : "OFF" );
        lcd.setCursor (0,0);
        lcd.print (s);

        sprintf (s, "CH%d:%-3s CH%d:%-3s",
            2, ON == relayState [2] ? "ON" : "OFF",
            3, ON == relayState [3] ? "ON" : "OFF" );
        lcd.setCursor (0, 1);
        lcd.print (s);
    }
}

LED1 was added under the ifdef for MyHW.

you need to add them under the #else case

Thanks a lot once again. Seems to work

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.