Struggeling with 2 inputs & 3 outputs

Hi

I’m struggeling with a simple code with 2 inputs and 3 outputs. My code does something weird.
Output pins: 2,3,4
Input pins 5,6

Pseudo code:

All pins are initialli LOW

If pin 5 changes from LOW to HIGH (toggle switch)

Switch pin 2 to HIGH
wait 5s
switch pin 2 to LOW
wait 5s
switch pin3 to HIGH
wait 5s
switch pin3 to LOW

If pin 6 changes from LOW to HIGH (toggle switch)

Switch pin 2 to HIGH
wait 5s
switch pin 2 to LOW
wait 5s
switch pin4 to HIGH
wait 5s
switch pin4 to LOW

Here is to real code:

int outputPin2 = 2;
int outputPin3 = 3;
int outputPin3 = 4;
int inputPin5 = 5;
int inputPin6 = 6;

int reading_inputPin5;
int reading_inputPin6;

// the follow variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long time = 0;         // the last time the output pin was toggled
long debounce = 200;   // the debounce time, increase if the output flickers

void setup()
{
  pinMode(outputPin2, OUTPUT);
  pinMode(outputPin3, OUTPUT);
  pinMode(outputPin4, OUTPUT);
  pinMode(inputPin5, INPUT);
  pinMode(inputPin6, INPUT);
}

void loop()
{
  reading_inputPin5 = digitalRead(inputPin5);
  reading_inputPin6 = digitalRead(inputPin6);

  if (reading_inputPin5 == HIGH && millis() - time > debounce) {
    time = millis();
    digitalWrite(outputPin2, HIGH);
    delay(5000);      // pauses for 5s
    digitalWrite(outputPin2, LOW);
    delay(5000);      // pauses for 5s
    digitalWrite(outputPin3, HIGH);
    delay(5000);      // pauses for 5s
    digitalWrite(outputPin3, LOW);
    }

    else if (reading_inputPin6 = HIGH && millis() - time > debounce) {
    time = millis();
    digitalWrite(outputPin2, HIGH);
    delay(5000);      // pauses for 5s
    digitalWrite(outputPin2, LOW);
    delay(5000);      // pauses for 5s
    digitalWrite(outputPin4, HIGH);
    delay(5000);      // pauses for 5s
    digitalWrite(outputPin4, LOW);
}
else {
}

}

Thanks for your help

How are the inputs wired ?

Any pulldown resistors in place to keep them in a known state or are they floating, sometimes HIGH, sometimes LOW due to picking up stray signals ?

else if (reading_inputPin6 = HIGH

You probably mean

else if (reading_inputPin6 == HIGH

In the Arduino IDE, use Ctrl T or CMD T to format your code.

Do not use delay() in your sketches, search blink without delay.

FYI

This is completely useless when you have delay(5000) in your code.

@ Deva_Rishi

Thanks, that did the trick. Sometimes you just dont see the error, even though it’s that simple.

I only increased the delay to 5000ms for debugging. Will be arround 200ms normally. But I will have a look at the alternative code which works without the delay function.

If your delay() will be returned to 200ms with several in a row and you have long debounce = 200; how does your use of millis() - time > debounce every make any sense :wink: .


You also need to study the state machine programming technique.

I will. Just started with arduino, had to deal with NPN and PNP transistors / MOSFETs first to to my first circuit…

FYI