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.
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.
`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.