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);
}
}
-
Your Schematic seem to be wrong. Let me do correct it.Hasan– Hasan2017年03月08日 10:16:41 +00:00Commented 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– Holmez2017年06月07日 10:02:28 +00:00Commented Jun 7, 2017 at 10:02
1 Answer 1
Try out this schematic. It will work.
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);
}
}
-
2To 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'sJames Waldby - jwpat7– James Waldby - jwpat72017年03月08日 16:58:30 +00:00Commented Mar 8, 2017 at 16:58
-
-
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 helpdeadguy88– deadguy882017年03月09日 06:34:05 +00:00Commented 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– Hasan2017年03月10日 04:12:51 +00:00Commented Mar 10, 2017 at 4:12