I am running a program code in my Arduino with two inputs and on/delay cyclic timers. I like to add one more output independent with three different inputs, tried the below written code, but only my 1st loop is working, 2nd one with inputs pin-5, 6, 7, output pin-9 is not working. Can someone make correction and identify the mistake to me. Thanks.
For 2nd loop input pin5 must be HIGH, input pin6 & Pin8 must be low for Output "ON"at pin 9.
Input pin5 must be HIGH, input pin6 & pin7 must be high for out "OFF" at pin 9, in any condition if pin5 input is LOW output at pin 9 must be remain OFF. (1st loop with input pins 3,4 output pin 8 and timers is still working perfectly)
// Pin numbers aren't negative, right? So no `int´ but unsigned int's
const uint8_t relayPin = 8;
const uint8_t sensorPin = 3;
const uint8_t switchPin = 4;
const uint8_t tankempty = 5;
const uint8_t ohtanklow = 6;
const uint8_t ohtankfull = 7;
const uint8_t motorUpPin = 9;
bool flip = false;
int motorState = 2;
uint32_t previousMillis = 0;
const uint32_t offTime = 60000;
const uint32_t onTime = 90000;
void setup()
{
// All pins are set to inputs by default
pinMode(relayPin, OUTPUT);
digitalWrite(relayPin, LOW);
Serial.begin(9600);
pinMode(sensorPin, INPUT_PULLUP);
pinMode(switchPin, INPUT_PULLUP);
pinMode(motorUpPin, OUTPUT);
digitalWrite(motorUpPin, LOW);
pinMode(tankempty, INPUT_PULLUP);
pinMode(ohtanklow, INPUT_PULLUP);
pinMode(ohtankfull, INPUT_PULLUP);
}
void loop()
{
// Always update the ezButton object states
// Remember the current motor state as well (defaults to false)
// Turn the motor OFF if the float switch is active (LOW)
int Switch = digitalRead(switchPin);
int Sensor = digitalRead(sensorPin);
int tankempty = digitalRead(tankempty);
int ohtanklow = digitalRead(ohtanklow);
int ohtankfull = digitalRead(ohtankfull);
Serial.print("Switchpin 4 is:...");
Serial.println(Switch);
Serial.print("Sensorpin 3 is:...");
Serial.println(Switch);
if (Switch == HIGH && Sensor == HIGH) {
motorState = 2; // not pressed.
counterFunction(motorState);
} else if (Switch == HIGH && Sensor == LOW) {
motorState = 0;//turns on.
counterFunction(motorState);
} else if (Switch == LOW && Sensor == HIGH) {
motorState = 1;//turns on.
counterFunction(motorState);
}
if (Switch == LOW && Sensor == LOW) {
motorState = 0;//turns the motor off when the tank fill's up but water is comming
}
Serial.println(motorState);
if (tankempty == LOW && ohtankfull == HIGH) {
motorState = 3;//turns off.
counterFunction(motorState);
}
if (tankempty == LOW && ohtanklow == LOW) {
motorState = 4;//turns on.
counterFunction(motorState);
}
if (tankempty == HIGH && ohtanklow == LOW ){
motorState = 3;// not pressed.
counterFunction(motorState);
}
Serial.println(motorState);
}
void counterFunction(int overide) {
//on loop.
if (overide == 2) {
if (millis() >= (previousMillis + onTime) && flip == true) {
previousMillis = millis();
flip = false;
overide = 0; //off
}
if (millis() >= (previousMillis + offTime) && flip == false) {
previousMillis = millis();
flip = true;
overide = 1; //on
}
}
if (overide == 0) {
digitalWrite(relayPin, LOW);
Serial.println("Motor is LOW:...");
} else if ( overide == 1) {
digitalWrite(relayPin, HIGH);
Serial.println("Motor is HIGH:...");
}
if (overide == 3) {
digitalWrite(motorUpPin, LOW);
Serial.println("Motor is LOW:...");
} else if ( overide == 4) {
digitalWrite(motorUpPin, HIGH);
Serial.println("Motor is HIGH:...");
}
}