You could probably simplify it by calling millis() twice and not having a variable, it wouldn't make much difference.
It's elegant, and I like it, but probably is the most benefit as a tutorial on how to achieve a lot in a few lines of code.
You could probably simplify it by calling millis() twice and not having a variable, it wouldn't make much difference.
It's elegant, and I like it, but probably is the most benefit as a tutorial on how to achieve a lot in a few lines of code.
int beat = 13;
void setup() {
pinMode(beat, OUTPUT);
}
void loop() {
unsigned long x = (millis() / 80) % 12;
digitalWrite(beat, 1 * ((x == 0) || (x == 3) ? 1 : 0));
}
What is the difference between what this
digitalWrite(beat, 1 * ((x == 0) || (x == 3) ? 1 : 0));
does and what that
digitalWrite(beat, ((x == 0) || (x == 3)) );
does?
Maybe by the time the compiler gets done there is none?
Yours has no "plaque".
Edit: ... except for the extra space after the 2 closing parenthesis.
I value easy on the eyes matching up of braces, parenthesis and structure more highly than packing lines. That extra keystroke now and then saves me time searching for matches and ends.
Perhaps this happens more and more past about age 35, as the eyes start to go.
digitalWrite(beat, 1 * ((x == 0) || (x == 3) ? 1 : 0));
If we are going to have that extra stuff at all I would prefer:
digitalWrite(beat, 1 * ((x == 0) || (x == 3) ? HIGH : LOW));
Gotta luv programming, where maths and poetry mix!
Had some LED arrhythmia happening while coming up with this ... (edited)
int beat = 13;
void setup() {
pinMode(beat, OUTPUT);
}
void loop() {
digitalWrite(beat, ! (millis() & 640));
}
Very nice, but what is "word" doing there? millis() & 640 is already constrained to not exceed 0x280.
Thanks Nick, code updated...I always seem to leave some extra stuff in there!
Based on original research by dlloyd I offer up this two-liner:
void setup() { pinMode(13, OUTPUT); }
void loop() { digitalWrite(13, !(millis() & 640)); }
I still haven't quite worked out how you did it but the bit pattern in 640 is relevant, I am sure:
1010000000
Seven trailing zeroes gives us a period of 128 mS, which is the observed gap between the fast pulses.
You have 10 significant bits there so you would expect things to repeat in 2^10 milliseconds, which it does. The whole pattern repeats every 1.024 seconds.
Yes, just looking at bits 8,9,10 the millis is a 3-bit 128ms counter.
seconds 0.128 0.256 0.384 0.512 0.640 0.768 0.896 1.024
mask 101 101 101 101 101 101 101 101
millis 000 001 010 011 100 101 110 111
AND & 000 001 000 001 100 101 100 101
bool 0 1 0 1 1 1 1 1
invert ! 1 0 1 0 0 0 0 0
Actually, with this method one could set a unique pattern and configure the length and repeat frequency.
Could go higher frequencies using micros().
dlloyd:
Actually, with this method one could set a unique pattern and configure the length and repeat frequency.
Could go higher frequencies using micros().
A bit off topic maybe, but could this be used to synthesize arbitary waveforms using sigma/delta?
dlloyd:
int beat = 13;
void setup() {
pinMode(beat, OUTPUT);
}
void loop() {
unsigned long x = (millis() / 80) % 12;
digitalWrite(beat, 1 * ((x == 0) || (x == 3) ? 1 : 0));
}
Wonderful piece of code and very usefull fo my project!
Thanks mate!
From Adrian, YO3HJV 73!