Set Fault Light

I am trying to make a simple brake light circuit.
Brake Light is red.

Trouble Light is Yellow.

int Brakelight = 4;
int Troublelight = 5;


void setup()
	{
pinMode(Brakelight, OUTPUT);
pinMode(Troublelight, OUTPUT);

digitalWrite(Brakelight, HIGH);// set brake light on

	}

void loop()
{


	
if (digitalRead(Brakelight) == LOW)
{
  digitalWrite(Troublelight, HIGH);// set troublight on if brakelight is INOP
}



}

So what i am trying to do is if the Brake light goes bad(open) I want the yellow trouble light to come on to let me know.

Thanks for your help.

The problem here is that you are using digitalRead() with an OUTPUT pin.
digitalRead() is to be used only with INPUT PINs.

grant1842: Thanks for your help.

Did you intend to ask a question? Since you didn't, there isn't much we can do to help.

Thanks for your reply's. Yes I ment to ask a question.

My red light comes on but when I remove it from the bread board the yellow will not light up.

What could be the problem ?

I propose you use the schematic as in my attachment and decide the Yellow LED state upon the analog pin readings.

Do some tests with and without the red led there and see the results of analogRead(). Then you must define a margin value for deciding the state of the yellow led (on or off).

grant1842: My red light comes on but when I remove it from the bread board the yellow will not light up.

What could be the problem ?

I can see two problems.

  • You seem to be using the 'red light' as an output and an input. It can't do both things at once.
  • Your algorithm for detecting lamp failure seems unworkable to me. You need to detect whether there is current flowing through the lamp, not whether there is a voltage to it.

@ RicDias

Thanks for your input. I changed my breadboard circuit to match yours. Based on that I came up with the code below. It works well. If i pull out the red led the yellow lights up and if i put the red led back in the yellow led goes off. Is there something else I should be doing or is the circuit fine as is.

int Brakelight = 4;
int Troublelight = 5;


void setup()
{
    Serial.begin(9600);
pinMode(A0, INPUT);
pinMode(Brakelight, OUTPUT);
pinMode(Troublelight, OUTPUT);
digitalWrite(Brakelight, HIGH);// set brake light on

}

void loop()
{
    int TroublelightState = analogRead(A5);

    if(analogRead(TroublelightState)== LOW)
    {
    digitalWrite(Troublelight, HIGH);// set troublight on if brakelight is INOP
    }
    else
    {
    digitalWrite(Troublelight, LOW);
    }
    Serial.println(TroublelightState, DEC);
}

All so why did you use pull down resistors ?

Thanks for your help.

Well, your code is not completelly correct, although you say it work, may I suggest you some changes?

You should use

int TroublelightState = analogRead(A5);

So TroublelightState will have values between 0 and 1023.

Now you should test this value, based on some measurements (which I assume you did).
So imagine you measured values of 0,1,3 and 5 when there is no LED there. You can then test if TroublelightState < 10. If that’s TRUE, then you probably want to light up your Fault Light. Else, turn it off.

That said, I would change your condition:

if(analogRead(TroublelightState)== LOW)

to

if(TroublelightState <= 10)

To answer your question, I changed to pull-down resistors because they will give you near 0 (not necessarily 0, due to some possible noise) analog readings when there is no Red LED there to close the loop.
You could achieve the same with pull-up, but you’ll have to test for high values. It’s all up to you. :wink:

Thanks for all remarks.

Its nice that you guys help people completely new to Arduino.