LED stays on

I am a new newbie. I wrote this very small code, but it doesn’t work. It turns the LED on, but it will not turn it off. Could someone please indicate why this happening? I am receiving no errors when uploading or checking the program. Thanks so much.

int led = 9;

void setup() {
// put your setup code here, to run once:
pinMode(9, OUTPUT);


void loop() {
digitalWrite(led, HIGH);
digitalWrite(led, LOW);


Your loop() function summarised :

turn LED on
wait 5 seconds
turn LED off
immediately turn LED on again with no delay
repeat for ever

Add a second delay() after turning the LED off

Umm... Ok UKhelibob, thank you so much for you totelage.

I assume that you have fixed it

No, I have not fixed it. I am doing research online to find out how to make it do what I want it to do. I am trying to make a laser detector trip wire. I would like for it to be on all the time, trip and send signal, and then immediately rearm.

Did you have trouble understanding the explanation in reply #1, or do you now have some other problem?

You can leave the laser on continuously and detect a break - or (better) modulate it with a square wave
turn LED on
(short delay say 2msec)
turn LED off
(short delay say 2msec)
repeat for ever

but that would be better done with an external circuit or the Arduino timer. Its the signal from the sensor you need to detect a break with.

Yes, aarg, I'm starting to understand what reply #1 meant. I see why that won't work. As for what johnerrington said, I'm starting to understand the bigger picture. The problem is I don't know, what I don't know... Thank you guys for your help. What I do understand is that I have a lot more reading to do!

I see why that won't work.

So, have you fixed it now ?

Yes, I have got it working. I cheated and copied some code off line. That got it doing what I wanted. Thanks to all.

You don’t really have to go online for that, check the “blink” example sketch that ships with the IDE. The kind folks at Arduino even made a tutorial for you:

I cheated and copied some code off line.

Do you understand the solution ?

Yes, I understand now how it works. aarg, I did not the blink program, the one I used wasn't explained like the link you provided. That link really clears up a lot for me. Thank you guys so much!