Multiple LED problem.

Good day. I have a problem, that try to build a switcher, with check leds. I mean, a green and a red led should show me, that the switcher is on or off. My idea, that the green led shine when the swither is off (LOW on pin2), and the red will change it, when the swither is on (HIGH on pin2).

The setup:
Single 1/0 swither, attached to gnd and pin2
Red led, attached to pin4, green led, attached to pin6.

My code:

int input = 2;
int green = 6;
int red = 4;
int var = 0;

void setup ()
{
pinMode(green, OUTPUT);
pinMode(red, OUTPUT);
pinMode(input, INPUT);

digitalWrite(green, LOW);
}

void loop()
{var = digitalRead(input);
if (var == HIGH)
{digitalWrite(red, HIGH);}
else
{digitalWrite(red, LOW);}
}

What could be the problem? Thanks for help!

You didn't say what's wrong, but I'm guessing you need to change this:

pinMode(input, INPUT);

to this:

pinMode(input, INPUT_PULLUP);

Then when the switch is closed, var will be LOW, and HIGH otherwise.

Another problem could be that your code never switches on the green led. Try this:

int input = 2;
int green = 6;
int red = 4;
int var = 0;

void setup () {
  pinMode(green, OUTPUT);
  pinMode(red, OUTPUT);
  pinMode(input, INPUT_PULLUP);
}

void loop() {
  var = digitalRead(input);
  if (var == HIGH) {
    digitalWrite(red, HIGH);
    digitalWrite(green, LOW);
  }
  else {
    digitalWrite(red, LOW);
    digitalWrite(green, HIGH);
  }
}

Also be aware that how you should post code.

Another possibility is that you have destroyed one or both leds by failure to use series resistors?

Here is a suggestion to achieve your goal with using no Arduino pins. In fact, no Arduino.
sw.png

Thank you for your reply. Yes, i made a mistake, forget to mention the problem, and it was definetly the constant green led :slight_smile: You are better that way I assume. Thanks, and next time I try to be more accurate :slight_smile:

Also, another question:

If i want to turn on the pin 7 when the swither is on (else conditions), and run it for 5 minutes, switch off it and immediately turn on the pin 9, run for 12 mins, and change back to pin 7 for another 9 mins, how can i do it? Nor delay or timer work, i spent few hours to try it. Im not programmer, hust try to write my own code :slight_smile:

You basically have three states that you describe. You can implement something with a switch statement

void loop()
{
  // the start time of the 'action'
  static unsigned long timerStarttime = 0;
  
  // the current state
  static byte currentState = 1;

  switch (currentState)
  {
    case 1:
      if (timerStarttime == 0)
      {
        // switch pin 7 on
        ...

        // set the start time
        timerStarttime = millis();
      }
      else
      {
        if (timerStarttime - millis() >= 300000UL)
        {
          // switch pin 7 off
          ...
          // reset the start time for the next step
          timerStarttime = 0;
          // 'goto' next step
          currentState = 2;
        }
      }
      break;
    case 2:
      if (timerStarttime == 0)
      {
        // switch pin 9 on
        ...

        // set the start time
        timerStarttime = millis();
      }
      else
      {
        if (timerStarttime - millis() >= 720000UL)
        {
          // switch pin 9 off
          ...

          // reset the start time for the next step
          timerStarttime = 0;
          // 'goto' next step
          currentState = 3;
        }
      }
      break;
    case 3:
      if (timerStarttime == 0)
      {
        // switch pin 7 on
          ...

        // set the start time
        timerStarttime = millis();
      }
      else
      {
        if (timerStarttime - millis() >= 540000UL)
        {
          // switch pin 7 off
          ...

          // reset the start time for the next step
          timerStarttime = 0;
          // 'goto' next step
          currentState = 1;
        }
      }
      break;
  }
}

This is just sample code to show how to approach your problem; it simply does something for 5 minutes, something else for 12 minutes and the something else again for 9 minutes. Because in the last state (step) the currentState is set to one, this will be a forever repeating sequence.

Now there is a lot left to be desired. First is the hard-coded numbers 1, 2 and 3. You can place the following near the top of the code

#define PIN7_5MINUTES 1
#define PIN9_12MINUTES 2
#define PIN7_9MINUTES 3

Replace PINX by something more sensible that reflects what actually happens.

Next is that the first code basically contains 3 repeating blocks doing the same thing, just different pins and different durations. You can place this in a function

/*
  do an action (pin on for given duration followed by pin off)
  in:
    delayTime: time that pin must be on
    pinNNumber: the pin to control
  returns:
    false while action in progress, true when action finished
*/
bool action(unsigned long delayTime, byte pinNumber)
{
  // the start time of the 'action'
  static unsigned long timerStarttime = 0;

  if (timerStarttime == 0)
  {
    // switch pin on
    digitalWrite(pinNumber, HIGH);

    // set the start time
    timerStarttime = millis();
  }
  else
  {
    if (timerStarttime - millis() >= delayTime)
    {
      // switch pin off
      digitalWrite(pinNumber, LOW);

      // reset the start time that we use this function
      timerStarttime = 0;

      // indicate that we're done with this
      return true;
    }
  }

  // indicate that the action is in progress
  return false;
}

And call that function in the different cases.

void loop()
{
  // the current state
  static byte currentState = 1;

  switch (currentState)
  {
    case 1:
      if (action(300000UL, 7) == true)
      {
        // 'goto' next step
        currentState = PIN9_12MINUTES;
      }
      break;
    case 2:
      if (action(720000UL, 9) == true)
      {
        // 'goto' next step
        currentState = PIN7_9MINUTES;
      }
      break;
    case 3:
      if (action(540000UL, 7) == true)
      {
        // 'goto' next step
        currentState = PIN7_5MINUTES;
      }
      break;
  }
}

Hope this gives you the idea.