For and if statement problem

I need to blink two LEDs with two different PWM signals. I wan to run them with lower power for 30sec and switch to higher power after that and run for 10min +.
It seems that duration of my sketch does not depend on i number. I can set i to 100000, 500,000 or 1000000 but the sketch always complete second loop in roughly 6min.

void setup() {  
   pinMode(10, OUTPUT); 
   pinMode(8, OUTPUT);
}

void loop() {
  for (int i=0;i<=1600000; i++){
    if (i<3000) {
   digitalWrite(10, HIGH); 
   digitalWrite(8, HIGH); 
   delayMicroseconds(1000); 
   digitalWrite(10, LOW); 
   digitalWrite(8, LOW);     
   delayMicroseconds (11000); 
}  

   else {
   digitalWrite(10, HIGH); 
   digitalWrite(8, HIGH); 
   delayMicroseconds(1350); 
   digitalWrite(10, LOW); 
   digitalWrite(8, LOW); 
   delayMicroseconds(10000); 
   }
  }
 }

If you are using a platform with a 16 bit integer, then 1600000 is bigger than the max value.

Use unsigned long instead of int for typing the index i.

one problem is that the max value for an int is 32737.

It wouldn’t hurt if you formatted your code nicely… TOOLS-AUTO FORMAT

another approach avoiding the for loop

const byte PinLed0 = 13;
const byte PinLed1 = 12;
const byte PinLed2 = 10;

const unsigned long T0 = 3 * 1000;

const unsigned long T1 = 1000;
const unsigned long T2 =   20;
const unsigned long T3 =  900;

      unsigned long t;

enum { Off = HIGH, On = LOW };

// -----------------------------------------------------------------------------
void setup ()
{
    pinMode (PinLed0, OUTPUT);
    pinMode (PinLed1, OUTPUT);
    pinMode (PinLed2, OUTPUT);
    digitalWrite (PinLed2, Off);
}

void loop ()
{
    if (millis () < T0)
        t = 100;
    else
        t = 800;

    digitalWrite      (PinLed0, On);
    digitalWrite      (PinLed1, On);
    delayMicroseconds (t);

    digitalWrite      (PinLed0, Off);
    digitalWrite      (PinLed1, Off);
    delayMicroseconds (T1 - t);
}

I suspected something like that. Thank you!

Thank you! I could never understand how mills works.

Sure you can understand how millis() and non-blocking timing works
It just depends on how good does the presented information fit to your personal knowledge-level.

If you find it hard to understand the reason is always:
The knowledge-level of the presented information does NOT match with your personal knowledge-level.

@gcjr

Are you sure you wanted to code it this way?

what would you suggest to meet his "stated" requirement?

millis() simply returns the # of msec since power up. it's a 64 bit value that won't wrap for ~49 days.

a more common approach is to capture a timestamp and compare the elapsed time to some period

    if (millis() - msecTimeStamp >= MsecPeriod)  {
        //some action, possibly resetting the timestamp
        msecTimeStamp = millis();
    }

from the text

I would assume that the pwm-signal would be different.

When looking into the code

shows both IO-pins ( = LEDS are switched on/off together.
Which is somehow contradictionary to the textual description
==> needs clarification

As the for-loop is inside function loop()
this points into the direction of this behaviour of

Shall be repeated. Still the textual description is not precise enough to be sure.

@aiasnikov

As you can see from my posting you need to write down a much more precise description of what you want the LEDs to do.

So I try to describe it in my own words and you should confirm or correct my description

Two LEDs shall light up in total synchronisation. Both LEDs light up at a lower-power
for 30 seconds.

After these 30 seconds the two LEDs shall light up with higher power for exact 10 minutes

After these 10 minutes the cycle shall repeat

Your code works this way

variable i counts up from 0 to 1.600.000.

for i = 0 up to 3000 the summarised delayMicroseconds is
delayMicroseconds(1000);
delayMicroseconds (11000);

sums up to 12000 microseconds per iteration

This means 3000 iterations delaying 12000 means
executing this part of the code needs

3000 * 12000 microseconds = 36.000.000 microseconds which is
36 seconds (not 30 seconds)

for-loop counts up for i = 3000 up to 1.600.000

the summarised delay per iteration is
delayMicroseconds(1350);
delayMicroseconds (11000);

12350 microseconds

looping 1.597.000 times with a delay of 12350 needs

(1.600.000 - 3.000) * 12.350 / 1.000.000 = 19.722,95 seconds

19.722,95 seconds / 60 = 328,7 minutes

328,7 minutes / 60 = 5,48 hours

This is just confusing through the lack of explanations and even some confusing words like
"possibly" again through the lack of explaining it.

all in all:
compare actual time with a former taken timestamp beeing greater than a certain value

    if (millis() - msecTimeStamp >= MsecPeriod)  {

millis(): actual time in milliseconds

msecTimeStamp: time stamp holding a number that is a point in time some time in the past

calculate difference between actual time and timestamp

millis() - msecTimeStamp 

check if this calculated difference is bigger than value hold inside variable MsecPeriod

">= MsecPeriod"

    if (millis() - msecTimeStamp >= MsecPeriod)  {
        //some action
        msecTimeStamp = millis(); // UPDATING the timestamp (not "resetting" whatver this exactly means)
    }

@gcjr

What do you think: would it make sense to you to take the posting of a thread-opener and trying to estimate the knowledge-level of the thread-opener
and only in case you come to the conclusion: "seems to have a pretty advanced knowledge" which seems to be minimum 80% of gcjrs knowledge to post one of your usual codes?

In case you come to a different conclusion not posting at all

or posting with intensive explanations of all the things you were too blind to see because to you yourself these details are so common that you no longer think this could be an problem to understand?

i assume the OP has a half a brain which you seem to assume he doesn't

why don't you give the OP a chance to think about things and ask a question?

The question is: what is the knowledge-level that is above the actual knowledge-level
not too much above / not too near

So it will be the right amount of challenging. Not too much. Not too less.

You have a very strong tendency of assuming too much knowledge by other users.

If the concept of non-blocking timing is really new to somebody it is very likely to think about non-blocking timing beeing similiar to delay() which of course it really isn't.

A different example:

Do you know life-kinetics?
I assume: yes

please perform the exercise of
"exercise "1-2-3-4" combined with "reverse alphabet"

Not written: If you do not understand any detail ask questions.

If you should happen to know these life-kinetics exercises
please combine the above with "slackline" and "parallel throw-cross-catch"

Uh ? You don't know what I am talking about ?! Well these are all even very common words and you don't understand them?

Well life kinetics is practiced for more than 10 years now and you still don't know about it?

If you have found out what it is all about you will see: (conclusion not written)

please get off your soapbox and give the OP a chance to learn and ask questions

1 Like