Led on

Hi everyone, I am very new in this, and Im looking for some help. I have a code to turn on a led with a distance sensor; it works great, but the thing is that the led turns of as soon as I move my hand away from the sensor, but I need that the led turns on after the sensor detecs an object or whatever, and to remain on for a few seconds even if there is nothing in front of the sensor.

This is the code I use:

const int signalPin= 0; //yellow wire connects to analog pin A0 const int LEDPin= 13; //LED connects to digital pin 13 int signal;//this variable, signal, will hold the analog value read by the arduino

void setup() { Serial.begin(9600); //sets the baud rate for data transfer in bits/second pinMode(signalPin, INPUT); //the infrared sensor signal line will be an input to the arduino pinMode(LEDPin, OUTPUT); //the LED serves an output in the circuit }

void loop() { signal= analogRead(signalPin); //arduino reads the value from the infrared sensor Serial.println(signal); //prints out analog value delay(500); //delays the next analog reading by 500 ms or a half a second

if(signal > 200){ //if the analog value is less than 200, the object is within a few inches digitalWrite(LEDPin, HIGH); } else{ digitalWrite(LEDPin, LOW); } }

I hope someone can help me. Thanks in advance.

Please use code tags. Use the </> icon in the posting menu. [code] Paste sketch here. [/code]
Use CTRL T to format your sketches.

The placement of delay() is important to delay events, and delay(500); is only 1/2 a second.

Time to look a using the BlinkWithoutDelay example to avoid delay().


It can help to work first in "pseudo-code"... someting closer to the language you've used til now...


{various stuff... including... Turn LED off }


if sensor sees hand... then....

{ Turn LED on wait 2 seconds Turn LED off }

==== That's all what you told us you want to do actually NEEDS... it won't be the world's most efficient or beautiful code... but you're just getting started.

(One of the uglinesses: When there is no hand in front of the sensor, it will read the sensor many, MANY times a second.)

A flaw: If you leave hand in front of sensor for about 1.8 seconds, it will go off about 0.2 seconds after you remove hand.

So... second attempt (to deal only with "flaw"... and, oh by the way, programming often proceeds like this, from a first idea, and then onward when flaw spotted...

setup as before

loop() {

if hand in front of sensor.... then.... { repeat....

(do nothing until hand NOT in front of sensor

wait two seconds } // end of if hand in front... then...

=== Not "pretty", and not exactly how YOU would do it... but find something that "works" FOR YOU.

Diagrams with boxes and lines on a piece of paper can also be helpful.

Be sure you know IN DETAIL what you want it to do before you add the extra burden of trying to express what you want in "proper" Arduino code.

Of course, it is a bit of a chicken and egg thing, as you have to be sure that you decide on something that you want which is also something that CAN (as second step) be expressed in Arduino code.

But if it was easy, everyone would be doing it, and where would the fun be?