Hello all viewers!
I'm currently working on a school engineering project that consist of a computer controlled (wall) outlet that the end result should be able to turn a lamp on and off at a specified time. I'm an intermediate/beginner with Arduino looking for assistance from someone with more knowledge than me! I currently have all the hardware that I want to use as seen in the attached photos (yes the inside looks like a rats nest but I did try to make it as neat as possible). As you can also see the "UI" consists of four buttons, a green and red indicator lights, and an I2c 16x2 LCD. On the inside I have 120V coming in through MC lite which all the hots go into the COM ports of the relay shield on top an UNO R3. Also theres a hot that is connected to a switch to power a modified "wall wart" to supply 5v to a general PCB to provide power to everything on the lid and then the Arduino as well as home to the pulldown resistors for the buttons. Also the outlets are separated so that there's two separate relay controlled outlets which the relays are "rated" for 8 amps at 120V AC (I'm skeptical about that kind of capacity on that small shield) and then 15 amps combined governed by the outlet but neither max will be tested.
As far as I've gotten with the coding is:
#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>
#define I2C_ADDR 0x3F
#define BACKLIGHT_PIN 3
#define En_pin 2
#define Rw_pin 1
#define Rs_pin 0
#define D4_pin 4
#define D5_pin 5
#define D6_pin 6
#define D7_pin 7
int n = 1;
int clockOut = 11;
int greenPin = 10;
int redPin = 9;
int dwnPin = 8;
int upPin = 2;
int leftPin = 11;
int rightPin = 12;
int dwnVal = 0;
int upVal = 0;
int leftVal = 0;
int rightVal = 0;
int vertChange = 0;
int horChange = 0;
int relay1 = 0;
int relay2 = 0;
int relay1Pin = 5;
int relay2Pin = 4;
LiquidCrystal_I2C lcd(I2C_ADDR,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin);
void setup()
{
Serial.begin(9600);
Serial.print("START");
lcd.begin (16,2);
lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE);
lcd.setBacklight(HIGH);
lcd.home (); // go home
pinMode(clockOut, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(redPin, OUTPUT);
pinMode(dwnPin, INPUT);
pinMode(upPin, INPUT);
pinMode(leftPin, INPUT);
pinMode(rightPin, INPUT);
pinMode(relay1Pin, OUTPUT);
pinMode(relay2Pin, OUTPUT);
}
void loop()
{
dwnVal = digitalRead(dwnPin);
upVal = digitalRead(upPin);
digitalWrite(greenPin, HIGH);
//down button pressed
if(dwnVal == HIGH){
vertChange--;
delay(150);
if(vertChange < 0) {
vertChange = 1;
}
}
//up button pressed
if(upVal == HIGH){
vertChange++;
delay(150);
if(vertChange > 1) {
vertChange = 0;
}
}
switch(vertChange){
//relay 1 case
case 0:
{
lcd.setCursor (0,0);
lcd.print("Relay 1 Status:");
leftVal = digitalRead(leftPin);
rightVal = digitalRead(rightPin);
//left button pressed
if(leftVal == HIGH){
relay1--;
delay(150);
if(relay1 < 0) {
relay1 = 1;
}
}
//right button pressed
if(rightVal == HIGH){
relay1++;
delay(150);
if(relay1 > 1) {
relay1 = 0;
}
}
switch(relay1){
//relay1 off
case 0:
{
lcd.setCursor (0,1);
lcd.print("OFF");
digitalWrite(relay1Pin, LOW);
}
break;
//relay1 on
case 1:
{
lcd.setCursor (0,1);
lcd.print("ON");
digitalWrite(relay1Pin, HIGH);
}
break;
}
}
break;
//relay 2 case
case 1:
{
lcd.setCursor (0,0);
lcd.print("Relay 2 Status:");
leftVal = digitalRead(leftPin);
rightVal = digitalRead(rightPin);
//left button pressed
if(leftVal == HIGH){
relay2--;
delay(150);
if(relay2 < 0) {
relay2 = 1;
}
}
//right button pressed
if(rightVal == HIGH){
relay2++;
delay(150);
if(relay2 > 1) {
relay2 = 0;
}
}
switch(relay2){
//relay2 off
case 0:
{
lcd.setCursor (0,1);
lcd.print("OFF");
digitalWrite(relay2Pin, LOW);
}
break;
//relay2 on
case 1:
{
lcd.setCursor (0,1);
lcd.print("ON");
digitalWrite(relay2Pin, HIGH);
}
break;
}
}
break;
}
}
One thing I was wondering if there was any way to make a cleaner version of what I want to accomplish? I'm just using switch cases within switch cases and I wasn't sure if thats a no-no or if that's correct. Also looking for some guidance on how to go about the clock situation, I'm aware that if you just code for counting millis is very inaccurate and not sure about dabbling in a RTC (real time clock) even though that might be what I have to resort to. Somewhere else I've seen someone using attachInterrupt() with using PWM pin 3 (UNO) which is interrupter 1 attached to the a0 pin and seeing as the PWM is actually really accurate with 490 Hz so counting those pulses would act as a good clock but I haven't been able to get that to work yet. Getting the clock to work also entails being able to set the clock without any external input besides that of my "UI" which is a can of worms in itself but I know it can be done.
ANY feedback would be great! Thank you in advance!

