1

I have design a circuit with 2 led (red and UV) and 2 button.So, the circuit supposed to work like this: If i press the button for red led, the red led lights up and the uv led turn off, and if i press the button for uv led the uv led lights up and the red led turn off.

This what happen when i press the red button The Schematic

But the circuit is not work like that, when i upload the code, the uv led suddenly turn on without touching the button.The red led had no issue.The red led turn on when the button is push.And turn off when the button is unpress.But when i push the red button, the uv led does not turn off.It stay glow.The uv led turn on whatever i push the button or not.

Here is the code :

int red = 8;
int uv = 9;
int redbutton = 2;
int uvbutton = 3;
int redbutcondition = 0;
int uvbutcondition = 0;

void setup()
{
  pinMode (red, OUTPUT);
  pinMode (uv, OUTPUT);
  pinMode (redbutton, INPUT);
  pinMode (uvbutton, INPUT);
}

void loop()
{
  redbutcondition = digitalRead(redbutton);
  uvbutcondition = digitalRead(uvbutton);

  if (redbutcondition == HIGH)
  {
    digitalWrite(red, HIGH);
    digitalWrite(uv, LOW);
  }
  else if(uvbutcondition == HIGH);
  {
    digitalWrite(uv, HIGH);
    digitalWrite(red, LOW);
  }
}
2
  • Your Schematic seem to be wrong. Let me do correct it.
    – Hasan
    Mar 8, 2017 at 10:16
  • Are your buttons definitely plugged into pins 2 and 3? The schematic and the code look correct, but the picture looks like there is they're not plugged into neighbouring pins - it looks like there is a gap between them, but it is very hard to tell conclusively.
    – Holmez
    Jun 7, 2017 at 10:02

1 Answer 1

1

Try out this schematic. It will work.

Setup

So code will be :

int red = 8;
int uv = 9;
int redbutton = 2;
int uvbutton = 3;
int redbutcondition = 0;
int uvbutcondition = 0;

void setup()
{
  pinMode (red, OUTPUT);
  pinMode (uv, OUTPUT);
  pinMode (redbutton, INPUT_PULLUP);
  pinMode (uvbutton, INPUT_PULLUP);
}

void loop()
{
  redbutcondition = digitalRead(redbutton);
  uvbutcondition = digitalRead(uvbutton);

  if (redbutcondition == LOW)
  {
    digitalWrite(red, HIGH);
    digitalWrite(uv, LOW);
  }
  else if(uvbutcondition == LOW)
  {
    digitalWrite(uv, HIGH);
    digitalWrite(red, LOW);
  }
}
4
  • 2
    To make that work need to change pinMode (redbutton, INPUT); pinMode (uvbutton, INPUT); to pinMode (redbutton, INPUT_PULLUP); pinMode (uvbutton, INPUT_PULLUP); and invert the logic in if's Mar 8, 2017 at 16:58
  • @jwpat7 Yeap, you're right.
    – Hasan
    Mar 9, 2017 at 4:09
  • But im using a pull up resistor on the button.So, i didnt need INPUT_PULLUP right?I think my schematic and my code are wrong.Btw thanks for your help
    – deadguy88
    Mar 9, 2017 at 6:34
  • @deadguy88 May be you're right. But it is better to apply pull up through program because why you use extra circuitry if that things happens through program. Usually, I prefer this things. Try out my code and schematic. It will definitely work.
    – Hasan
    Mar 10, 2017 at 4:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.