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();
}
}