Pages: 1 2 [3]   Go Down
Author Topic: millis() blink != 50/50  (Read 2212 times)
0 Members and 1 Guest are viewing this topic.
Des Moines, WA - USA
Offline Offline
God Member
*****
Karma: 25
Posts: 779
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Brevity is not clarity.

And stop acting like a child.  This wasn't aimed at you it's a common complaint I have with most programming forums.
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Right. Let's move on. All of the solutions posted have their merits. Let's not descend into a flame war.
Logged

Des Moines, WA - USA
Offline Offline
God Member
*****
Karma: 25
Posts: 779
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

And to this I agree with!!
Logged

Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 58
Posts: 4024
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Here is a user interactive version, it blinks the on-board led on my UNO.
Enter numbers from 0 to 999 through serial and that will be the millisecs/second the led stays on.

Code:
unsigned long waitUntil = 0UL; // because millis() returns UL
enum whatFlag { led13state, endOfLine, dataBegun };
byte  Flags = 0;

void setup()
{
  pinMode( 13, OUTPUT );
  digitalWrite( 13, bitRead( Flags, led13state )); 
  Serial.begin( 9600 );
  Serial.println( "Blink w/user input. Enter up to 1000 for led-on interval" );
  Serial.println();
}

void loop()
{
  static byte  c; // c for character buffer
//  static byte  endOfLineState = 0;
  static unsigned long timeSinceLastSerialRead = 0UL;
  static int enter = 0; // to get serial-entered number value.
  static int ledMillisOn = 100;  // must be 0-999
 
  // flash led and state changer
  if ( waitUntil - millis() >= 10000UL )  // when millis() passes waitUntil the _unsigned_ subtract
  {                                       // leaves very large positive result
    Flags ^= ( 1 << led13state ); // led13state T/F bit toggle

    if (bitRead( Flags, led13state ))  waitUntil = millis() + (unsigned long)(ledMillisOn);
    else                              waitUntil = millis() + (unsigned long)(999 - ledMillisOn);
    digitalWrite(13, bitRead( Flags, led13state )); 
  }
 
  // serial input
  if (Serial.available())
  {
    c = Serial.read();
//    Serial.print(".");
//    Serial.print( c );
    timeSinceLastSerialRead = millis();
    if ( c >= '0' && c <= '9' )
    {
      enter *= 10;  // decimal shifting whatever is in enter 1 place up
      enter += ( c - '0' ); // ascii 48 is '0'
      bitSet(Flags, dataBegun );  // entry is treated as finished
      if ( enter >= 100 ) // next digit being able to take the total > 999
      {
        bitSet(Flags, endOfLine );  // entry is treated as finished
      }
    }
    else if (( c == 13 || c == 10 ) && bitRead( Flags, dataBegun ))
    {
      bitSet( Flags, endOfLine );  // entry is treated as finished
    }
    else if (( millis() - timeSinceLastSerialRead >= 500 ) && ! bitRead( Flags, endOfLine ))
    {
      bitSet( Flags, endOfLine );  // entry is treated as finished
    }
  }
 
  // serial output
  if ( bitRead( Flags, endOfLine ) && bitRead( Flags, dataBegun )) // should be done and have data
  {
    Serial.print( ">> " );
    Serial.print( enter );
    Serial.println( " <<" );

    ledMillisOn = enter;
    enter = 0;
    bitClear( Flags, endOfLine );
    bitClear( Flags, dataBegun ); // clears bits, endOfLine and dataBegun
  }
}

Logged

Examples can be found in your IDE.

Pages: 1 2 [3]   Go Up
Jump to: