LED's not turning off after delay

I am unable to get the led to turn off after the second if and else if statements. The first if works ok.
What am I doing wrong?

int sensor1;
int sensor2;

void setup(){

pinMode(2, INPUT_PULLUP);
pinMode(3, INPUT_PULLUP);

pinMode(10, OUTPUT);
pinMode(11, OUTPUT);
pinMode(12, OUTPUT);


void loop(){

sensor1 = digitalRead(2);
sensor2 = digitalRead(3);  

if (sensor1 == HIGH) {
    digitalWrite(10, HIGH);
    digitalWrite(10, LOW);

if (sensor1 == LOW && sensor2 == LOW ) { // do stuff if the condition is true
    digitalWrite(11, HIGH);
    digitalWrite(11, LOW);
else if (sensor1 == LOW && sensor2 == HIGH) { // do stuff only if the first condition is false 
           digitalWrite(12, LOW);
           digitalWrite(12, HIGH);



Use CTRL-T to auto format your code in the IDE. It makes it easier to read, lines up brackets and is just better.

Which LED is misbehaving? You have 3.

I do see a delay between turning on and LED and turning it off, but no delay after turning it off.
For your middle condition, where both sensors are low, it will look like the LED never turns off.
In reality it IS turning off, but only for a few, un-noticeable microseconds. Then it turns back on.

doh! I spent quite a while looking at the program and missed it. I knew it had to do with the fact that loop() loops (ie., re-executes repeatedly). I see it now. I believe he has the problem with all three LEDs, 2 never turn on and 1 never turns off.

OP: it is one of two issues.

A. You haven't assimilated the fact that loop() re-executes repeatedly. Loop() is not a one time execution, only setup is. Therefore as soon as the LED turns off, it will turn back on during the next loop() execution.


B. You know that but, like I did, forgot to evaluate what happens between the loop close bracket { and the if statement.

Try moving the code to setup. It will fire once and then be done. Or, add a delay(5000) just before the closed brace } for loop(). Then the LED will change states for 5 second intervals (5 on, 5 off)

I notice that the first two statements do a HIGH, wait 5 seconds, LOW sequence, while the third does a LOW, wait five seconds, HIGH sequence. Assuming the LEDs are wired all the same and port HIGH means the LED is on, that will make the third LED switch on 5 seconds after the buttons are pressed and not switch off until the same combination is detected.

You need to write out a truth table showing ALL the different input combinations and the output response you need in the three outputs.

Input 2 Input 3 Output 10 Output 11 Output 12
0 0
Flash 5s
0 1 Flash 5s
1 0 Flash 5s
1 1

What do you want ot happen in the other states?

Tom… :slight_smile:

Ok, what I'm trying to do should be simple but, since I'm new to this I am probably doing this all wrong.
What I'm trying to do is with sensor1 detect a part is in place,
light a blue led, wait for X amount of milliseconds,
then if sensor2 detects metal,
lights a green led for x amount of milliseconds, then turn off led.
then go back to the beginning of the loop.
Or if it doesn't detect metal, lights a red led for x amount of milliseconds, then turn off led
then go back to the beginning of the loop.

Thanks for all your help!!

Well you're using delay() in your sketch so when one LED is on, no further action is taken. So you won't see both LEDs go on in case a metal part is detected. Instead you'll have to use millis() based timing - see "blink without delay" on how that works. Then you can continue the loop() so if a part is detected the blue LED lights, and then either the green or the red one can switch on as well.

Here is the link to the project on tinkercad.
I used 2 sliding switches in place of 1 Capacitive (sensor1) proximity switch and 1 inductive proximity switch (sensor2)

Link broken.

First requires you to log in to autodesk (which I happened to have registered at before) and then drops me into some kind of tutorial.

I just checked it and the link works for me.
Thanks for trying.

It doesn't for me, it asks for me to log/sign in.

Please read the first post in any forum entitled how to use this forum.

And tinker what ever it is should let you export a jpg or png image to post in your message.

I am not an registered tinkercad user.
You are.

Tom.... :slight_smile:

I just checked it and the link works for me.
Thanks for trying.

Try your own link without being logged in or logging in using a different account. The thing is probably linked to your account. Or indeed post a jpg (at the worst you can take a screen shot).

Here is a picture of the project

OPs diagram..

Where are your current limit resistors for each of the LEDs?

Thanks.. Tom.. :slight_smile:

I am not concerned with the resistors at this time. I have attached a PDF explaining (I hope) a little more what I am trying to do.


Clamp Detector.pdf (117 KB)

I am not concerned with the resistors at this time. I have attached a PDF explaining (I hope) a little more what I am trying to do.


Oh yes you should be, when ever you turn an output ON to turn a LED ON, you will be OVERLOADING that output and may damage the controller.
Can you tell us your electronics, programming, Arduino, hardware experience?
Thanks… Tom… :slight_smile:

This is my first time using Arduino but what I meant was that is something I will change once I have the sketch done.

Make it a habit to include all components right away, especially essentials such as current limiting resistor, as otherwise you may break your Arduino and/or LEDs and/or other things. It’s very to forget you still have to add them later.