Go Down

Topic: Flashing LED with Modulo? (Read 1 time) previous topic - next topic

aarg

FWIW
Code: [Select]
void setup()
{
  pinMode(LED_BUILTIN, OUTPUT);     
}

void loop()
{
  digitalWrite(LED_BUILTIN, millis()&2048);
}
  ... with a transistor and a large sum of money to spend ...
Please don't PM me with technical questions. Post them in the forum.

TonyWilk

FWIW
Code: [Select]
void setup()
  digitalWrite(LED_BUILTIN, millis()&2048);

Almost, but no cigar.
The parameter has to be a zero or nonzero byte; you could make that truthy: (millis&2048)==0

Yours,
 TonyWilk

aarg

Almost, but no cigar.
The parameter has to be a zero or nonzero byte; you could make that truthy: (millis&2048)==0

Yours,
 TonyWilk

Didn't you mean
Code: [Select]
(millis()&2048)==0
;)
  ... with a transistor and a large sum of money to spend ...
Please don't PM me with technical questions. Post them in the forum.

westfw

Quote
So much processing burnt on blinking a LED
well, with a little adjustment it could become pretty good...
If the idea is to just "flash the led once in a while to show we're still alive", you can get a way with optimizations:

Code: [Select]
void loop() {
  if ((millis() & 1023) == 0) digitalWrite(13, HIGH);  // turn on at the ~ one-second marks
  if ((millis() & 1023) == 100) digitalWrite(13, LOW); // turn off at the ~1.1s marks
}


(bitwise AND with one less than a power of two is equivalent to modulus with the power of two.)


wilfredmedlin

If the idea is to just "flash the led once in a while to show we're still alive", you can get a way with optimizations:
I was about to complain that code doesn't work, when I realised I hadn't pinMode'ed as OUTPUT ;) . I like to have proof of life in sketches, so that is now my way to do it.

Keep Calm and call 0118 999 88199 9119725...... 3

Kiwi_Bloke

Well I have learned so many options to try, but the code I found did something at bit level, inverting 0 and 1 somehow.

Thank you every one that has commented and posted ideas, it has been great to see all the suggestions and thought processes working to help.

westfw

Code: [Select]
static byte ledstate = 0;
 :
if ((millis() & 1023) == 0) digitalWrite(13, (ledstate^=1));  // invert the LED every ~1 sec


Or

Code: [Select]
if ((millis() & 1023) == 0) digitalWrite(13, ! digtialRead(13));   // invert the LED every ~1 sec


TonyWilk

Didn't you mean
Code: [Select]
(millis()&2048)==0
;)
touché :)

Yours,
 TonyWilk

Go Up