Regards,
Here are trying to explain and re-write an existing project.
Framed in red working with the code attached
const int buttonPin = A0; // the number of the pushbutton pin
const int relayPin1 = 13; // the number of the LED pin for testing
const int relayPin2 = 12; // the number of the LED pin for testing
const int relayPin3 = 11;
const int relayPin4 = 10;
const int BUTTON1 = 1;
const int BUTTON2 = 2;
const int BUTTON3 = 3;
const int BUTTON4 = 4;
const int BUTTON1LOW = 461;
const int BUTTON1HIGH = 563;
const int BUTTON2LOW = 288;
const int BUTTON2HIGH = 352;
const int BUTTON3LOW = 162;
const int BUTTON3HIGH = 198;
const int BUTTON4LOW = 118;
const int BUTTON4HIGH = 144;
// Variables will change:
int relayState = LOW; // the current state of the output pin
int buttonState; // the current reading from the input pin
int lastButtonState = LOW; // the previous reading from the input pin
int reading;
// the following variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long lastDebounceTime = 0; // the last time the output pin was toggled
long debounceDelay = 50; // the debounce time; increase if the output flickers
void setup() {
pinMode(buttonPin, INPUT);
pinMode(relayPin1, OUTPUT);
pinMode(relayPin2, OUTPUT);
pinMode(relayPin3, OUTPUT);
pinMode(relayPin4, OUTPUT);
Serial.begin(9600);
}
void loop() {
// read the state of the switch into a local variable:
int tmpButtonState = LOW; // the current reading from the input pin
int reading = analogRead(buttonPin);
if(reading>BUTTON4LOW && reading<BUTTON4HIGH){
//Read switch 4
tmpButtonState = BUTTON4;
}else if(reading>BUTTON3LOW && reading<BUTTON3HIGH){
//Read switch 3
tmpButtonState = BUTTON3;
}else if (reading > BUTTON2LOW && reading < BUTTON2HIGH) {
//Read switch 2
tmpButtonState = BUTTON2;
} else if (reading > BUTTON1LOW && reading < BUTTON1HIGH) {
//Read switch 1
tmpButtonState = BUTTON1;
} else {
//No button is pressed;
tmpButtonState = LOW;
}
// check to see if you just pressed the button
// (i.e. the input went from LOW to a buttonState), and you've waited
// long enough since the last press to ignore any noise:
// If the switch changed, due to noise or pressing:
if (tmpButtonState != lastButtonState) {
// reset the debouncing timer
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
// whatever the reading is at, it's been there for longer
// than the debounce delay, so take it as the actual current state:
buttonState = tmpButtonState;
Serial.println(buttonState, LOW);
}
// save the reading. Next time through the loop,
// it'll be the lastButtonState:
lastButtonState = tmpButtonState;
// set the LED using the state of the button for testing:
switch (buttonState) {
case BUTTON1:
digitalWrite(relayPin1, buttonState > 0);
break;
case BUTTON2:
digitalWrite(relayPin2, buttonState > 0);
break;
case BUTTON3:
digitalWrite(relayPin3, buttonState>0);
break;
case BUTTON4:
digitalWrite(relayPin4, buttonState>0);
break;
default:
digitalWrite(relayPin1, LOW);
digitalWrite(relayPin2, LOW);
digitalWrite(relayPin3, LOW);
digitalWrite(relayPin4, LOW);
}
}
Framed blue is a supplement that should implement it into existing code
SW2 attached to pin2 (LOW-Manual mode, HIGH-Auto mode)
if SW2 - LOW -> do Read analog pinA0 (framed in red) and pin3, pin4, pin5, pin6, pin7 not read
if SW2 - HIGH -> do Read pin3 and pinA0, pin4, pin5, pin6, pin7 not read
SW1 attached to pin3 (LOW-STOP, HIGH-START)
if SW1 - LOW -> set pin4, pin5, pin6, pin7 and pinA0 to LOW
and pin10 to HIGH until pin7 read HIGH then pin10 to LOW
and pin12 to HIGH until pin5 read HIGH then pin12 to LOW
if SW1 - HIGH -> start auto mode
switches LEFT, RIGHT, UP, DOWN is Reed Switch - picture is attached
LEFT attached to pin4
RIGHT attached to pin5
UP attached to pin6
DOWN attached to pin7
relay2 attached to pin13
relay1 attached to pin12
relay4 attached to pin11
relay3 attached to pin10
===== start auto mode ======
SW1 - HIGH ->
do pin13 to HIGH until pin4 read HIGH then pin13 to LOW
then pin11 to HIGH for 3s or pin6 and pin5 read HIGH then
if pin6 and pin5 read HIGH then pin10 to HIGH until pin7 read HIGH then pin10 to LOW
and pin12 to HIGH until pin5 read HIGH then pin12 to LOW
else
then pin12 to HIGH until pin5 read HIGH then pin12 to LOW
then pin11 to HIGH for 3s or pin6 and pin5 read HIGH then
if pin6 and pin5 read HIGH then pin10 to HIGH until pin7 read HIGH then pin10 to LOW
and pin12 to HIGH until pin5 read HIGH then pin12 to LOW
else
repeat until pin6 and pin5 read HIGH then pin10 to HIGH until pin7 read HIGH then pin10 to LOW
and pin12 to HIGH until pin5 read HIGH then pin12 to LOW



