ok ive taken a look and the code has a switch in the wrong place which is probably messing with the relay outputs
#include <Wire.h>
#include <LiquidCrystal.h>
// Initialize the library with the numbers of the interface pins
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
#define RELAY1 7
#define RELAY2 6
#define RELAY3 5
#define RELAY4 4
#define z1 1
#define z2 2
#define z3 3
#define z4 4
//States for the menu.
int currentMenuItem = 0;
int lastState = 0;
int z1_set = 25; //setpoint (25% on boot) consider adding to eeprom memory
int z2_set = 25; //setpoint
int z3_set = 25; //setpoint
int z4_set = 25; //setpoint
int z1_reading;//readings from analog sensor
int z2_reading;//readings from analog sensor
int z3_reading;//readings from analog sensor
int z4_reading;//readings from analog sensor
byte edit_mode = 0;//used to detect when in edit
byte screen;//the current screen in use
unsigned long previousMillis = 0; //part of timer
unsigned long interval = 30000; //30 seconds timer
void setup() {
//Set the characters and column numbers.
lcd.begin(16, 2);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Colins Water v.2");
Serial.begin (9600); // set the serial monitor tx and rx speed
// Initialise the Arduino data pins for OUTPUT
pinMode(RELAY1, OUTPUT);
pinMode(RELAY2, OUTPUT);
pinMode(RELAY3, OUTPUT);
pinMode(RELAY4, OUTPUT);
pinMode(z1_reading, INPUT);
pinMode(z2_reading, INPUT);
pinMode(z3_reading, INPUT);
pinMode(z4_reading, INPUT);
}
void loop() {
mainMenu(); //Call the main menu.
}
void mainMenu() {
//State = 0 every loop cycle.
int state = 0;
//Refresh the button pressed.
int x = analogRead (0);//keypad
int z1_reading = analogRead (A1);//to be added soil moisture detector
int z2_reading = analogRead (A2);//to be added soil moisture detector
int z3_reading = analogRead (A3);//to be added soil moisture detector
int z4_reading = analogRead (A4);//to be added soil moisture detector
//used to reset screen and kick out of edit after 30 seconds
if ((screen != 0) || (edit_mode != 0)) {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis > interval) {
screen == 0;
edit_mode == 0;
previousMillis = currentMillis;
}
}
z1_reading = map (z1_reading, 0, 1023, 0, 100); //converts raw input to 0-100%
z2_reading = map (z2_reading, 0, 1023, 0, 100); //converts raw input to 0-100%
z3_reading = map (z3_reading, 0, 1023, 0, 100); //converts raw input to 0-100%
z4_reading = map (z4_reading, 0, 1023, 0, 100); //converts raw input to 0-100%
/*
//Check analog values from LCD Keypad Shield original
if (x < 50) {
//Right
} else if (x < 195) {
//Up
state = 1;
} else if (x < 380) {
//Down
state = 2;
} else if (x < 555) {
state = 4;
} else if (x < 790) {
//Select
state = 3;
}
*/
//Check analog values from LCD Keypad Shield
if (x < 50) {
//Right
state = 4;
} else if (x < 195) {
//Up
state = 1;
} else if (x < 380) {
//Down
state = 2;
} else if (x < 555) {
state = 0;
//Left
} else if (x < 790) {
//Select
state = 3;
}
//If we have changed Index, saves re-draws.
if (state != lastState) {
if (state == 1) {
//If Up
//same up/down buttons are used with 5 diffrent results depending on edit mode and screen number
if (edit_mode == 0) {
screen++;
}
else {
switch (screen) {
case 1:
z1_set++;
break;
case 2:
z2_set++;
break;
case 3:
z3_set++;
break;
case 4:
z4_set++;
break;
}
}
} else if (state == 2) {
//If Down
if (edit_mode == 0) {
screen--;
}
else {
switch (screen) {
case 1:
z1_set--;
break;
case 2:
z2_set--;
break;
case 3:
z3_set--;
break;
case 4:
z4_set--;
break;
}
}
} else if (state == 3) {
//If Selected
edit_mode = 1;
}
//added this so you can hit left button to get out of edit. saves waiting for 30 seconds
else if (state == 4) {
//If Selected
edit_mode = 0;
}}
//Save the last State to compare.
lastState = state;
if (screen < 0 || screen > 4) {
screen = 0;
}
lcd.clear();
lcd.setCursor(0, 0);
if (edit_mode == 0) {
switch (screen) {
case 0:
lcd.print("Colins Water v.2");
lcd.setCursor(0, 1);
//lcd.print ("blar blar blar");
break;
case 1:
lcd.print ("-> ZONE 1");
lcd.setCursor(0, 1);
Serial.println (z1_reading);
lcd.print (" %");
break;
case 2:
lcd.print ("-> ZONE 2");
lcd.setCursor(0, 1);
Serial.println (z2_reading);
lcd.print (" %");
break;
case 3:
lcd.print ("-> ZONE 3");
lcd.setCursor(0, 1);
Serial.println (z3_reading);
lcd.print (" %");
break;
case 4:
lcd.print ("-> ZONE 4");
lcd.setCursor(0, 1);
lcd.print (z4_reading);
lcd.print (" %");
break;
}
} else
switch (screen) {
case 0:
lcd.print("select zone");
lcd.setCursor(0, 1);
lcd.print ("first");
break;
case 1:
lcd.print ("ZONE 1 set");
lcd.setCursor(0, 1);
lcd.print (z1_set);
lcd.print (" %");
break;
case 2:
lcd.print ("ZONE 2 set");
lcd.setCursor(0, 1);
lcd.print (z2_set);
lcd.print (" %");
break;
case 3:
lcd.print ("ZONE 3 set");
lcd.setCursor(0, 1);
lcd.print (z3_set);
lcd.print (" %");
break;
case 4:
lcd.print ("ZONE 4 set");
lcd.setCursor(0, 1);
lcd.print (z4_set);
lcd.print (" %");
break;
}
if (z1_reading < z1_set) {
digitalWrite(RELAY1,LOW); // Turns ON Relays 1
}
else {
digitalWrite(RELAY1,HIGH); // Turns Off Relays 1
}
if (z2_reading < z2_set) {
digitalWrite(RELAY2,LOW); // Turns ON Relays 2
}
else {
digitalWrite(RELAY2,HIGH); // Turns Off Relays 2
//close water?
}
if (z3_reading < z3_set) {
digitalWrite(RELAY3,LOW); // Turns ON Relays 4
//open water?
}
else {
digitalWrite(RELAY3,HIGH); // Turns Off Relays 3
//close water?
}
if (z4_reading < z4_set) {
digitalWrite(RELAY4,LOW); // Turns ON Relays 4
//open water?
}
else {
digitalWrite(RELAY4,HIGH); // Turns Off Relays 4
//close water?
}
//Small delay
delay(5);
}
this should fix it. (never noticed the switch just before the delay so it was stuck in a if statement)
now you said you would like to hold a button but im afraid that's not easy on a keypad as holding one button disables every other button.
You need to draw on paper what screens you want and the order they are to be accessed. Just remember that buttons can not do more than one job in any set mode. So up/down can either change the screen or the set point they can not change both with out changing mode as you can not hold a button.
Also whats up with the moisture sensor. Have you tried making a sketch just to test the sensor and print the result to the serial port?