simple use of millis tutorial for output in time

lol, trying to understand the millis and use it for a purpose.
i have about 5 days of reading everything so no need to offer more..... :wink:
i like many others learn by doing and seeing functioning patterns
i like to think millis is like the flow of sand through an hour glass
i think my sketch is pretty close but the monitor is showing a cycle much faster than 1234
wrong or right i think this might be what some users are after......

                                                                       // assume a grain of sand is a millisecond
unsigned long sandMillis = 0;            // hour glass  empty on top
const long sandAmount = 1234;            // time in milliseconds to count or grains of sand
const int ledPin =  LED_BUILTIN;         // just because it is on the board = easy and now

void setup() {  
  Serial.begin(9600);                           // turn on the TV screen
  pinMode(ledPin, OUTPUT);                      // the output tool
}

void loop() {

   digitalWrite(ledPin, HIGH);                           // start with doing something that we are going to do for the period of time
   Serial.println("oh jeez here we go!"); 
  
  unsigned long currentMillis = millis();                // flipping over the  hour glass

  if (currentMillis - sandMillis >= sandAmount)          // reading if the hour glass is to or over the sand amount
      sandMillis = currentMillis;                        // flipping the hour glass back over to zero for next use

   digitalWrite(ledPin, LOW);                            // now that we reached the millis count or grains of sand we are starting to do functions
   Serial.println("my god we made it!");
  
}

You are missing the curly brackets to enclose the statements in the if block.

Very shortly after you turn the LED LOW the if exits and the LED is set HIGH again.

void loop() {

   digitalWrite(ledPin, HIGH);                           // start with doing something that we are going to do for the period of time
   Serial.println("oh jeez here we go!");
 
  unsigned long currentMillis = millis();                // flipping over the  hour glass

  if (currentMillis - sandMillis >= sandAmount)          // reading if the hour glass is to or over the sand amount
  { // **********curly to enclose statements
      sandMillis = currentMillis;                        // flipping the hour glass back over to zero for next use

   digitalWrite(ledPin, LOW);                            // now that we reached the millis count or grains of sand we are starting to do functions
   Serial.println("my god we made it!");
  } // **********curly to enclose statements
}

As it was, the only statement executed conditionally was the sandMillis = currentMillis; statement. The rest executed every time through loop (unconditionally).

 if (currentMillis - sandMillis >= sandAmount)          // reading if the hour glass is to or over the sand amount
      sandMillis = currentMillis;                        // flipping the hour glass back over to zero for next use

   digitalWrite(ledPin, LOW);                            // now that we reached the millis count or grains of sand we are starting to do functions
   Serial.println("my god we made it!");

It's in loop() so it loops. And the last two lines are NOT controlled by the if statement. Try a couple of {} to make a proper code block. And think about what sandMillis is equal to the first time round loop

Steve

ok ya i see that now
i fixed it and get no change though? :confused:

You fixed all those comments? So post the fixed program that still does the same thing.

Steve

i will update the first code as to not have too much to look at
it looks like the function is now working right
i just saw that the first print is cycling through fast and then once the sandAmount is true then the function runs

give me a minute..............

turbothis:
i will update the first code as to not have too much to look at

No! Don't do that!

Like TheMemberFormerlyKnownAsAWOL said, don't update the original post, make a new post with the updated code. Updating the original post makes all the subsequent posts look stupid. I don't like looking stupid (even when I am).

ok ok ok :slight_smile:

can you have a delay for the print only or does it delay the whole code still?

                                        // assume a grain of sand is a millisecond
unsigned long sandMillis = 0;            // hour glass  empty on top
const long sandAmount = 1234;            // time in milliseconds to count or grains of sand
const int ledPin =  LED_BUILTIN;         // just because it is on the board = easy and now

void setup() {  
  Serial.begin(9600);                           // turn on the TV screen
  pinMode(ledPin, OUTPUT);                      // the output tool
}

void loop() {

   digitalWrite(ledPin, HIGH);                             // start with doing function that we are going to do for the period of time
   
  unsigned long currentMillis = millis();                  // flipping over the  hour glass

  if (currentMillis - sandMillis >= sandAmount)           // reading if the hour glass is to or over the sand amount
   {   sandMillis = currentMillis;                        // flipping the hour glass back over to zero for next use
   Serial.println("here we go");                          // now that we reached the millis count or grains of sand we have started the functions
     delay (1000);                                        // not sure if you can delay the print function only or if it delays everything?
   Serial.println("my god we made it!");
   digitalWrite(ledPin, LOW);                             // start with doing function                         
  }
}

A call to delay() delays evverything

ok, well is this the simplest millis example to run a function for a given time without getting expert level crazy?

                                         // assume a grain of sand is a millisecond
unsigned long sandMillis = 0;            // hour glass  empty on top
const long sandAmount = 1234;            // time in milliseconds to count or grains of sand
const int ledPin =  LED_BUILTIN;         // just because it is on the board = easy and now

void setup() {  
  Serial.begin(9600);                           // turn on the TV screen
  pinMode(ledPin, OUTPUT);                      // the output tool
}

void loop() {

   digitalWrite(ledPin, HIGH);                            // start with doing function that we are going to do for the period of time = work
   
  unsigned long currentMillis = millis();                 // flipping over the  hour glass

  if (currentMillis - sandMillis >= sandAmount)           // reading if the hour glass is up to or over the sand amount
                                                          // NOW  that we reached the millis count or grains of sand we have started the functions   
   {   sandMillis = currentMillis;                        // flipping the hour glass back over to zero for next use
    Serial.println("my god we made it!");                 // some feedback on the monitor
    digitalWrite(ledPin, LOW);                            // doing function on the board                         
  }
}

A function will run until it returns.

Does it work the way that you want?

aaaaaaaaaahh
i should have said statement not function right?

yes it works 100%

ok are my notes accurate?

                                         // assume a grain of sand is a millisecond
unsigned long sandMillis = 0;            // hour glass  empty on top
const long sandAmount = 1234;            // time in milliseconds to count or grains of sand
const int ledPin =  LED_BUILTIN;         // just because it is on the board = easy and now

void setup() {  
  Serial.begin(9600);                           // turn on the TV screen
  pinMode(ledPin, OUTPUT);                      // the output tool
}

void loop() {

   digitalWrite(ledPin, HIGH);                            //statement// start with doing function that we are going to do for the period of time = work
   
  unsigned long currentMillis = millis();                 //variable// flipping over the  hour glass

  if (currentMillis - sandMillis >= sandAmount)           //condition// reading if the hour glass is up to or over the sand amount
                                                                     // NOW  that we reached the millis count or grains of sand we have started the functions   
   {   sandMillis = currentMillis;                        //statement// flipping the hour glass back over to zero for next use
    Serial.println("my god we made it!");                 //statement// some feedback on the monitor
    digitalWrite(ledPin, LOW);                            //statement// doing function on the board                         
  }
}
sandMillis = currentMillis;

Should besandMillis += sandAmount;

why adding the sandAmount?
i was thinking the sandMillis = currentMillis; would be needed to be able to start counting from 0 next time through the loop

It depends on how much jitter or slippage you want.

i dont know what that is

Imagine that the code that gets executed in the condition takes 2ms.
With your scheme, the first timer timeout occurs at 0 + 1234ms.
The second timeout will occur at 0 + 1234 + 2 + 1234 ms.
Most people would want/expect the second timeout at 0 + 1234 + 1234 ms