i want the led to light at the beginning and stays like this for a while then turns off, so i wrote:
int led=13;
void setup() {
pinMode(led,OUTPUT);
}
void loop() {
digitalWrite(led,HIGH);
delay (5000);
digitalWrite (led,LOW);
}
as i understand it should start with high means on digitalWrite(led,HIGH)...and stays like this for 5000us delay (5000) then it should turn off with digitalWrite (led,LOW). but why its not working, it starts with off for afew seconds then stays on..what i understand wrong with these simple steps ????
3 Answers 3
It immediately loops, from the digital write low to digital write high. Add a second delay after the low, so that it waits before it loops to the top again.
-
thnx, can you please explain to me that loop and why it acts like that...as i understand it should end with digitalwrite off... because its the last step at the programmsumer fattoum– sumer fattoum2017年12月02日 22:34:34 +00:00Commented Dec 2, 2017 at 22:34
-
i added the timer after low..but what happens is a flash..it repeat itself...but i want it so start with on for a definte time then it ends with off and stays offsumer fattoum– sumer fattoum2017年12月02日 22:39:28 +00:00Commented Dec 2, 2017 at 22:39
The "loop()" will keep repeating itself, turning the led on and off forever. If you want turn the led on just once, put the commands for turn the led on in "setup()" section:
void setup() {
pinMode(led,OUTPUT);
digitalWrite(led,HIGH);
delay (5000);
digitalWrite (led,LOW);
}
void loop() {
}
Other method would be add a variable to count how much times the led should light. I copied the following sketch from here
int ledPin = 13; // LED connected to digital pin 13
int alreadyBlinked = 0;
// The setup() method runs once, when the sketch starts
void setup() {
pinMode(ledPin, OUTPUT); // initialize the digital pin as an output:
}
// the loop() method runs over and over again,
// as long as the Arduino has power
void loop()
{
if(alreadyBlinked == 0)
{
digitalWrite(ledPin, HIGH); // set the LED on
delay(5000); // wait for five seconds
digitalWrite(ledPin, LOW); // set the LED off
alreadyBlinked = 1;
}
}
When the variable "alreadyBlinked" is set to 1, the led will not turn on again.
-
thnx , i got it, i just wanted to understand the structure of the program and instructions, now for more understanding of the loop i put once void loop(){ digitalWrite(led,HIGH); digitalWrite(led,LOW);} and once high then low, but why on both cases the led always stays on ???sumer fattoum– sumer fattoum2017年12月03日 00:21:44 +00:00Commented Dec 3, 2017 at 0:21
-
Did you check the variables names? Is the led correctly connected? One pin of the led should be connected to the arduino output, and the other pin to the ground. In the first option, digitalwrite (led,LOW) is the last command, so, the led should stay off after setup. In the second option, digitalWrite(ledPin, LOW) is issued before alreadyBlinked = 1, and, since then, the led will not turn on again. I'll try this sketch tomorrow.mguima– mguima2017年12月03日 00:58:49 +00:00Commented Dec 3, 2017 at 0:58
-
yes, i checked..i got it nowsumer fattoum– sumer fattoum2017年12月04日 23:51:33 +00:00Commented Dec 4, 2017 at 23:51
That delay does not do anything but wasting 5 seconds...you need to do like:
void loop() {
digitalWrite(led,HIGH);
delay (2500);
digitalWrite (led,LOW);
delay (2500);
}
The code runs sequentially...in your code after turning off the led it immediately turns it on again and waits 5 seconds...this is so fat that you do not notice it.
-
i have applied this but it blinks, it doesnt stop ...i want it to stop after lighting for a few secondssumer fattoum– sumer fattoum2017年12月02日 22:49:20 +00:00Commented Dec 2, 2017 at 22:49
-
thnx for the explaining that its so fast so i wont noticesumer fattoum– sumer fattoum2017年12月03日 00:27:03 +00:00Commented Dec 3, 2017 at 0:27
-
The loop function as its name suggests will happen over and over again. You need to set a flag. e.g. An integer that counts how many time the loop has been executed. Then an if statement to check and operate accordinglyDumbo– Dumbo2017年12月03日 12:40:03 +00:00Commented Dec 3, 2017 at 12:40
void setup()
only once, then it runs all the code invoid loop()
and repeats running the code invoid loop()
over and over until you reset the arduino, or turn it off. .... if you want the arduino to do something only once then do not put the code invoid loop()