Go Down

Topic: work two LEDs simultaneously (Read 428 times) previous topic - next topic

lamees_ghassan

Feb 20, 2018, 10:34 pm Last Edit: Feb 21, 2018, 06:57 am by lamees_ghassan
how can I write a code in Arduino using for loops and if statements to make two LEDs work simultaneously, the first one should stay on for 10 sec then 9 ,8 and so on, and stay off for the same period and the second one should stay on for 1 sec then 2,3 ...,10,also stay off for the same period

CtrlAltElite

I wouldn't bother with for loops, unless you've been told you have to.
Take a look at the blink without delay example, and remember that one LED stays ON for x seconds, and the other remains ON for 10 - x seconds.
Has there been an outbreak of the stupid virus, and I didn't get the memo?

Robin2

Have a look at the demo Several Things at a Time.  It manages 3 LEDs.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

Delta_G

With a for loop?  OK, if you insist.  This has a for loop.  This handles one LED.  You can write a similar one for the other led.  

at global scope
Code: [Select]

int t = 10;
boolean ledState = LOW;
int firstLedPin = 13;


Then in loop:

Code: [Select]



for  (static unsignedd long p=millis(); millis()-p>=(t * 1000ul);p = millis()){
    ledState = !ledState;
    digitalWrite(firstLedPin, ledState);
    if(ledState == LOW){
      t = t -1;
      if(t == 0){
        t = 10;
      }
    }
  }
|| | ||| | || | ||  ~Woodstock

Please do not PM with technical questions or comments.  Keep Arduino stuff out on the boards where it belongs.

Delta_G

Here is the same question on SO  https://stackoverflow.com/questions/48894638/work-two-leds-simultaneously-with-different-rates

If you're just going to ask the whole internet to do your homework for you, please at least show enough consideration to link the questions together so I don't waste my time talking here when someone on SO has already done your homework for you. 

Or better yet, do your own homework.  You might learn something. 
|| | ||| | || | ||  ~Woodstock

Please do not PM with technical questions or comments.  Keep Arduino stuff out on the boards where it belongs.

lamees_ghassan

I can't use millis(), it's only have to be done using if statements and for loops.
I'm new to Arduino, I am learning actually, and its not a homework, I tried several times to solve it but the two LEDs keep working after each other not simultaneously.

LesserMole

I can't use millis()..... and its not a homework,
Well if it's not homework set by someone else, why give yourself artificial constraints?


I can't use millis() .... I am learning actually
Arguably THE most important thing you can learn, is the use of millis()-based, delay()-less time management.


LesserMole

And I'm confused by your timing description, although I'm sure it makes sense to you.

How about you present us a timeline: draw two horizontal lines, one for each led, one under the other, with t=0 at the left and seconds marked off across to the right.

Then on each line, mark off in thicker pen the "on" periods for each led.


Robin2

I can't use millis(), it's only have to be done using if statements and for loops.
Why?

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

Delta_G

Why?

...R
He must be lying about it not being homework.  The only way such a crazy restraint is there is because some teacher put it there to try to force students to learn some other method.  Otherwise that constraint is just stupid. 
|| | ||| | || | ||  ~Woodstock

Please do not PM with technical questions or comments.  Keep Arduino stuff out on the boards where it belongs.

ardly

I can't use millis(), it's only have to be done using if statements and for loops.
I'm new to Arduino, I am learning actually, and its not a homework, I tried several times to solve it but the two LEDs keep working after each other not simultaneously.
Your are not making sense. If you want the LED to stay on or off for a period of time you are going to need to know how much time has elapsed and the way to do that is using millis().

The reason that your LEDs will be working after each other is probably that you will have written code to control LED1 which has to complete before the code you have written for LED2 is reached.

Post your code within the </> tags.
"Facts do not cease to exist because they are ignored" - Aldous Huxley

Go Up