push button relay controller, need help with coding??

Hi, this is my very first attempt at programming an arduino sketch myself, im trying to control five relays using five pushbuttons and be given a visual display that the relay has been switched on or off, here is what i think is the finished sketch, any help would be much appreciated

//This sketch is for mains power relay switching control.
//This will be done via a keypad attacted the digital pins
//on the aruduino uno v3, this sketch will run a scipt that
//digially reads the output pins to know it one relay is high
//or low. if the on/off button is pressed when the output pins are high
//then the code will switch those pins to low turning the outputs off and
//visa versa. The status of the relays will be displayed on a LCD display
//matrix.

//developed and programmed by John McLaughlin (not an original
//idea but original programming and debugging)

// include the library code:
#include <LiquidCrystal.h>

const int relayOne = 6;
const int relayTwo = 7;
const int relayThree = 8;
const int relayFour = 9;
const int relayFive = 10;
const int relayswitchOne = A1;
const int relayswitchTwo = A2;
const int relayswitchThree = A3;
const int relayswitchFour = A4;
const int relayswitchFive = A5;
int buttonStateOne = 0;
int buttonStateTwo = 0;
int buttonStateThree = 0;
int buttonStateFour = 0;
int buttonStateFive = 0;

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup(){
    // set up the LCD's number of columns and rows: 
  lcd.begin(16, 2);
  
  // set up the relay pins as outputs:
  pinMode(relayOne, OUTPUT);
  pinMode(relayTwo, OUTPUT);
  pinMode(relayThree, OUTPUT);
  pinMode(relayFour, OUTPUT);
  pinMode(relayFive,OUTPUT);
  
  //set up the pushbutton switchs as  inputs:
  pinMode(relayswitchOne, INPUT);
  pinMode(relayswitchTwo, INPUT);
  pinMode(relayswitchThree, INPUT);
  pinMode(relayswitchFour, INPUT);
  pinMode(relayswitchFive, INPUT);
  
  //set up the relays as off by default.
  digitalWrite(relayOne, LOW);
  digitalWrite(relayTwo, LOW);
  digitalWrite(relayThree, LOW);
  digitalWrite(relayFour, LOW);
  digitalWrite(relayFive, LOW);
}

void loop(){
  //read the state of pushbutton 1-5 state
  buttonStateOne = digitalRead(relayswitchOne);
  buttonStateTwo = digitalRead(relayswitchTwo);
  buttonStateThree = digitalRead(relayswitchThree);
  buttonStateFour = digitalRead(relayswitchFour);
  buttonStateFive = digitalRead(relayswitchFive);
 
 //if pushbutton one is pressed.
 //if it is then buttonState is HIGH:
 if (buttonStateOne == HIGH) {
   //turn relay on.
   digitalWrite(relayOne, HIGH);
   //Print a message to the LCD.
   lcd.print("Relay One ON");
   delay(5000);
   lcd.clear();
 }
 else {
   //turn relay one off.
   digitalWrite(relayOne, LOW);
   //Print a message to the LCD.
   lcd.print("Relay One OFF");
   delay(5000);
   lcd.clear();
 }
 
 //if pushbutton two is pressed.
 //if it is then buttonState is HIGH:
 if (buttonStateTwo == HIGH) {
   digitalWrite(relayTwo, HIGH);
   lcd.print("Relay Two ON");
   delay(5000);
   lcd.clear();
 }
 else {
   digitalWrite(relayTwo, LOW);
   lcd.print("Relay Two OFF");
   delay(5000);
   lcd.clear();
 }
 
 //if pushbutton three is pressed.
 //if it is then buttonState is HIGH:
 if (buttonStateThree == HIGH) {
   digitalWrite(relayThree, HIGH);
   lcd.print("Relay Three ON");
   delay(5000);
   lcd.clear();
 }
 else {
   digitalWrite(relayThree, LOW);
   lcd.print("Relay Three OFF");
   delay(5000);
   lcd.clear();
 }
 
 //if pushbutton Four is pressed.
 //if it is then buttonState is HIGH:
 if (buttonStateFour == HIGH) {
   digitalWrite(relayFour, HIGH);
   lcd.print("Relay Four ON");
   delay(5000);
   lcd.clear();
 }
 else {
   digitalWrite(relayFour, LOW);
   lcd.print("Relay Four ON");
   delay(5000);
   lcd.clear();
 }
 
 //if pushbutton five is pressed.
 //if it is then buttonState is HIGH:
 if (buttonStateFive == HIGH) {
   digitalWrite(relayFive, HIGH);
   lcd.print("Relay Five ON");
   delay(5000);
   lcd.clear();
 }
 else {
   digitalWrite(relayFive, LOW);
   lcd.print("Relay Five OFF");
   delay(5000);
   lcd.clear();
 }
}

How are these wired?
pinMode(relayswitchOne, INPUT);
pinMode(relayswitchTwo, INPUT);
pinMode(relayswitchThree, INPUT);
pinMode(relayswitchFour, INPUT);
pinMode(relayswitchFive, INPUT);

Does it work? As a user I would find the 25 secnds to see the state of all 5 quite irritating.
Can you change the output to have a label on line 1 and the on/off state on line 2?
Relay 1 2 3 4 5
On Off Off On On

or similar to fit in the space of 16 characters.

Then you can make it much more responsive to changes.

hi, the push button switches are wired to the analogue pins, the pins are pulled to ground using a pulldown resistor, the other side of the push button is wired to vcc +5v, when the button is toggles the pind get pulled up to vcc.

the 5 second delay for the lcd is for visual conformation. i should be ale to change the delay to about 2.5 seconds, that would probably make to code a bit more responsive, what do yous think?

i never thought on displaying the relays numerically with the state on the second row of the lcd below... will have to concider that... thank you all for your help.

Have a look at using arrays - you should be able to make your sketch a lot smaller and simpler.

ive changed the delay down to 1 second, can i change this code for a serial activated lcd instead of a parallel one to free up spare digital pins? and how would i use an array for the arduinos? would it be something like const int relayarray = 6, 7, 8, 9, 10;

would it be something like

close

const int relayarray[] = {6, 7, 8, 9, 10};