Timing LED challenge

Task Blink at least 3 LEDs at the same time. Each LED should blink at a different interval. For example the first LED blinks every 200ms, the second one every 500 and the third one every 900. Rules - delay() delay_ms() or any similar delay functions are NOT allowed. - millis(), micros() and similar are allowed. - the blinking has to be visible to the human eye. - No external libraries. (Arduino functions like millis() are allowed. External definitions like avr/io.h are also allowed.) If you wrote the library yourself then it's allowed. - No cheating by looking at code from previous winners! Bonus Challenge - No (or almost no) duplicate code for each LED. - Adding a LED requires changes to 3 or fewer lines of code. - Or Adding a LED requires changes to only one line of code. - Changing the interval of one LED requires changes to only one line of code. - Intervals of all LEDs can be set completely freely to any millisecond (or higher precision) value - Intervals/duty cycle of all LEDs can be adjusted completely independent of each other Stuff - Feel free to ping me for any questions. - Please ping me with your solution. - (If you post in this thread I will see it. No ping necessary.) - I recommend to use wokwi so you can easily share your solution. Winners - in pinned message https://discord.com/channels/420594746990526466/1205162164344848395/1207461901353750568
No description
214 Replies
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
pseud0
pseud02y ago
But 0x1 is the same as 1 and 2 characters shorter
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
pseud0
pseud02y ago
both should yield an integer constant, just written decimal or as hex I think it should work
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
PenPengu
PenPenguOP2y ago
edit it 😄 Nice solution. Just you can't completely freely choose the intervals. And it slightly bends my Bonus Challenges about only having to change limited number of lines to add LEDs or change intervals 😆 🥳
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
PenPengu
PenPenguOP2y ago
exactly I just added a new bonus challenge because of you
AnonEngineering
but should get "Most Obfuscated" award 🙂 (and that is meant as a compliment btw)
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
AnonEngineering
hey, it works! had to format it "properly" to even begin to figure it out 🙂
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
AnonEngineering
pretty soon the rules will be much longer than the codes
PenPengu
PenPenguOP2y ago
hehe
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
PenPengu
PenPenguOP2y ago
bing image creator AI
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
PenPengu
PenPenguOP2y ago
Does this have anything to do with this led challenge?
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
PenPengu
PenPenguOP2y ago
Yes, variables declared inside a function: - get created when the function is called - can get used inside the function - get completely destroyed when the function ends -> when the function is called a second time, the variables get created again. So their values don't "survive" between multiple function calls.
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
PenPengu
PenPenguOP2y ago
Exactly
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
PenPengu
PenPenguOP2y ago
you could think about just creating a class then. An object (class) is basically "Variables + functions that can use them in one neat package"
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
PenPengu
PenPenguOP2y ago
Sounds good 👍 feel free to ask more questions here (that come up as you work on the led challenge )
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
PenPengu
PenPenguOP2y ago
Sadly I learned the basics in school so I don't know about the internet. I can say though that for general C++ reference stuff Microsoft's C++ reference is way easier to read and less formal than cppreference. I don't know a good classes/object oriented tutorial right now
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
PenPengu
PenPenguOP2y ago
You know this syntax to increment a variable by one?
i++;
i++;
C++ was named c++ because it increments c. It's almost just a bunch of stuff added to c. Basically 99.9% of c is still "inside of"/included in c++. "Almost" because there are very few exceptions. Afaik when C++ was created it contained all of c. So it was just an extension and everything that you could do in c you could do in c++ exactly the same. But after the creation of C++ a few small things were added to C that weren't added to c++. You most likely won't stumble upon those exceptions. I'm not sure if there are other changes or things that got deprecated. Nowadays there are also things that you can still do in C++ (because you could do them in c) but where the old c way is discouraged in favour of a more modern C++ approach. Like c-style casts vs modern c++ casts.
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
PenPengu
PenPenguOP2y ago
Yes. But you have to include its entire source code here, as file in wokwi or as link Lmao how often I "have to" adjust the rules Would definitely be interesting to see how you wrote such a library
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
PenPengu
PenPenguOP2y ago
The first idea for this challenge was to teach people millis() or cooperative "multitasking" but now I'm also curious about various other solutions 😄 So sure use hardware timers 👍
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
PenPengu
PenPenguOP2y ago
you wired the LEDs to 1,2,3 but in your code I see 2,3,4 Not sure if the timer stuff is right. I always had to debug that myself with the simulator in atmel studio 😆
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
PenPengu
PenPenguOP2y ago
And I don't know what timers Arduino already uses
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
PenPengu
PenPenguOP2y ago
Oh really. Weird. Maybe wokwi doesn't simulate it right. Could you post a quick video? 😄
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
PenPengu
PenPenguOP2y ago
Nice 👍 🥳
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
PenPengu
PenPenguOP2y ago
exactly
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
PenPengu
PenPenguOP2y ago
👇 Winners 👇 Winners - AnonEngineering with a simple classless implementation https://discord.com/channels/420594746990526466/451155334845693952/1203776679823020063 and https://wokwi.com/projects/388825844131388417 - Me with an object oriented and slightly confusing solution https://wokwi.com/projects/389196557188321281 - Greeniac with a very short (and most obfuscated) solution https://discord.com/channels/420594746990526466/1205162164344848395/1205560932764356648 https://wokwi.com/projects/389274055915569153 - Lorbasta with a hardware timer/counter to create "ticks": https://discord.com/channels/420594746990526466/1205162164344848395/1207312024149692476 https://github.com/pastadozembow/SillyHardwareTIM-3/blob/main/main.cpp Video: https://discord.com/channels/420594746990526466/1205162164344848395/1207314770970148924 - jim lee using his own library LC_baseTools https://discord.com/channels/420594746990526466/1205162164344848395/1207380811083423774 https://wokwi.com/projects/389726049725583361 Library: https://github.com/leftCoast/LC_baseTools - Exodus with AVR Hardware timer 1 and its compare and overflow interrupt functionality: https://discord.com/channels/420594746990526466/1205162164344848395/1207547816923111534 https://wokwi.com/projects/389784331600601089 - Wasted with a neat nbdelay class in a separate file using a slightly different millis() calculation to reduce drift over time https://discord.com/channels/420594746990526466/1205162164344848395/1207665654438825994 https://wokwi.com/projects/389806431434073089 https://discord.com/channels/420594746990526466/1205162164344848395/1212028652729147402 https://discord.com/channels/420594746990526466/1205162164344848395/1208231810505379900 - dmitrii with a basic class-based solution that blinks the LEDs very quickly. video: https://discord.com/channels/420594746990526466/1205162164344848395/1211095126081015949 code: https://pastebin.com/dMSN9ZGV https://discord.com/channels/420594746990526466/1205162164344848395/1211096677285625866 - Amirreza Ipchi-e-Haq with a very short solution using a division and modulo "trick" https://discord.com/channels/420594746990526466/1205162164344848395/1240680083132256376 https://wokwi.com/projects/398052607831117825 - Icesythe 7 with maybe the shortest solution possible https://discord.com/channels/420594746990526466/863150029182206002/1253833268361691226 https://wokwi.com/projects/401341105758781441 - WeeGee with an actually even shorter solution https://discord.com/channels/420594746990526466/1205162164344848395/1264345669586649128 https://wokwi.com/projects/403969026122782721 - WeeGee again with bending the rules and adding external components. Shortest loop ever? https://wokwi.com/projects/404871112473916417 https://discord.com/channels/420594746990526466/1205162164344848395/1267960439057285174 - DungThrower https://discord.com/channels/420594746990526466/451155334845693952/1268738081813430273 - Marc with an array-based solution https://wokwi.com/projects/413146789837197313 https://discord.com/channels/420594746990526466/1205162164344848395/1301071179326554124 - zombean with a struct and function pointers https://wokwi.com/projects/413344853374775297 https://discord.com/channels/420594746990526466/1205162164344848395/1301848393685008385
PenPengu
PenPenguOP2y ago
reserved message more bing ai penguin
No description
Exodus
Exodus2y ago
ISR(TIMER1_COMPA_vect){
OCR1A += 100000;
PORTB=PORTB ^ 1 << 2 ;
}

ISR(TIMER1_COMPB_vect)
{
OCR1B += 100000/2;
PORTB=PORTB ^ 1 << 1 ;
}

ISR(TIMER1_OVF_vect) {
PORTB=PORTB ^ 1 << 0 ;
}

void setup() {
DDRB = B11111111;
TCCR1A = TCCR1B = 0;
OCR1A = 100000;
OCR1B = OCR1A /2;
TCCR1B |= B00000011;
TIMSK1 |= B00000111;
}

void loop() {
}
ISR(TIMER1_COMPA_vect){
OCR1A += 100000;
PORTB=PORTB ^ 1 << 2 ;
}

ISR(TIMER1_COMPB_vect)
{
OCR1B += 100000/2;
PORTB=PORTB ^ 1 << 1 ;
}

ISR(TIMER1_OVF_vect) {
PORTB=PORTB ^ 1 << 0 ;
}

void setup() {
DDRB = B11111111;
TCCR1A = TCCR1B = 0;
OCR1A = 100000;
OCR1B = OCR1A /2;
TCCR1B |= B00000011;
TIMSK1 |= B00000111;
}

void loop() {
}
https://wokwi.com/projects/389784331600601089
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
PenPengu
PenPenguOP2y ago
Might be hard to add a fourth LED to that but still very nice 😆
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
PenPengu
PenPenguOP2y ago
very nice! Made a wokwi for it: https://wokwi.com/projects/389806431434073089
Wokwi
Timing LED challenge by Wasted - Wokwi ESP32, STM32, Arduino Simulator
Run IoT and embedded projects in your browser: ESP32, STM32, Arduino, Pi Pico, and more. No installation required!
PenPengu
PenPenguOP2y ago
bool NBDELAY::nbd() {
if (millis()-tstart > delay) {
tstart += delay;
...
bool NBDELAY::nbd() {
if (millis()-tstart > delay) {
tstart += delay;
...
interesting variant of the blink without millis. The way you solved it doesn't cause drift if nbd() gets called a few milliseconds too late sometimes.
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
PenPengu
PenPenguOP2y ago
Very nice! thanks @WasTested Now I'm wondering for what application which approach would be better 🤔
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
PenPengu
PenPenguOP2y ago
I don't think you want previousMillis to be static 🤔 static variable = all objects of the class share this variable. You could think of a private static variable as a global variable that only objects of the class can access.
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
PenPengu
PenPenguOP2y ago
variables declared inside of a function get destroyed when the function is done variables inside of a class survive as long as the object survives. If your objects are global then all the variables inside the objects survive as long as the arduino stays running
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
PenPengu
PenPenguOP2y ago
Functions inside of a class can just change the variables of the class
class myClass{
private:
int var;//function doThing() changes this variable
public:
doThing(){
var = var +1;
}
};
...
myClass obj1;
myClass obj2;
void loop(){
obj1.doThing();//changes "var" inside object obj1
obj2.doThing();//changes "var" inside object obj2
class myClass{
private:
int var;//function doThing() changes this variable
public:
doThing(){
var = var +1;
}
};
...
myClass obj1;
myClass obj2;
void loop(){
obj1.doThing();//changes "var" inside object obj1
obj2.doThing();//changes "var" inside object obj2
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
AnonEngineering
use pastebin.com, paste it there, post the link it gives you here
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
PenPengu
PenPenguOP2y ago
👍 👍 Nice! Is it on purpose that they blink on so short? 😄
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
PenPengu
PenPenguOP2y ago
next question 😄 Is it on purpose that you don't update previousMillis in the else if? With the current code I think that timeTheLEDActuallyIsOn = delay - duration;. If I increase LEDduration to something large like 500 then LED5 never turns off. Even though it should be off for 461ms and on for 500.0 But over all very nice! You're definitely getting there! I bet you learned a lot that you can use in various things in the future! Otherwise the code works. Just some ideas, you don't actually have to implement them: 1. Every time you call .Blink you have to enter all 3 values, the pin, the delay and duration. It's always the same. You could consider storing those 3 in the class/object. Maybe make a constructor that takes those 3 values and stores them in the object. 2. You enter the BPM and calculate the delay. 5 times. In 5 lines. Maybe you could add that to the class? For example a function setBPM(int bpm)
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
PenPengu
PenPenguOP2y ago
nomination for most overengineered solution 😄 👍 👍 💯 orange is the new smart solution with minimum pulse width and blue is?
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
PenPengu
PenPenguOP2y ago
ah 👍
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
PenPengu
PenPenguOP2y ago
🥳 Link or it didn't happen 😝 😄 Very nice!
Unknown User
Unknown User16mo ago
Message Not Public
Sign In & Join Server To View
PenPengu
PenPenguOP16mo ago
wow that's short 👍 but what very interesting and nice actually 👍 I just don't really like the pinMode in the loop but 🤷‍♀️ 😄
AnonEngineering
AnonEngineering16mo ago
it is very nice, but let's not use it to replace "Blink" to demonstrate to noobs 🙂
Unknown User
Unknown User16mo ago
Message Not Public
Sign In & Join Server To View
PenPengu
PenPenguOP16mo ago
You can of course change things or have multiple solutions:D
Unknown User
Unknown User16mo ago
Message Not Public
Sign In & Join Server To View
WeeGee
WeeGee14mo ago
@Icesythe7 made me aware of this challenge, and I wondered where this solution stacks up in the runnings for the tiny/obfuscated class of solutions: https://wokwi.com/projects/403969026122782721 The entire code: void setup() { DDRD = 0xFF; } <- setup void loop() { PORTD = millis()>>8 & 0xFF; } <- loop it's def a "technically" but it works! -Blinks up to 8 LEDs at a time -all 8 LEDs guaranteed to blink at a different interval -no duplicate code for each LED; two lines for all -very visible blinking (wow I just realized this is a mega necropost, my bad ) I guess I was also spoiled by Icesythe7's answer, which gave me the idea of a binary counter
PenPengu
PenPenguOP14mo ago
I'm always happy when this continues! 😄 🥳
WeeGee
WeeGee14mo ago
Oh neat! So does this solution pass the requirements, as a double check to my work? A bonus challenge could be added for the duty cycle of each LED; mega bonus for being able to change the on and off time independent of each other
Unknown User
Unknown User14mo ago
Message Not Public
Sign In & Join Server To View
PenPengu
PenPenguOP14mo ago
added you to the winners! I absolutely love it! Very creative! 💯 I didn't add that bonus challenge (until now) because I didn't expect a solution like yours 😆 👍
WeeGee
WeeGee14mo ago
Ayy, glad to hear it's not completely outside the spirit of the challenge :P
PenPengu
PenPenguOP14mo ago
the spirit of the challenge is to have fun! and maybe learn something or try something out
WeeGee
WeeGee14mo ago
Oh I just had another interesting idea 👀
PenPengu
PenPenguOP14mo ago
do it! 😄
WeeGee
WeeGee14mo ago
here is a submission that goes for 2 specific bonus challenges (hidden for spoiler) How it works: this uses a maximal cycle length linear feedback shift register of length 8 to blink 8 LEDs in a "pseudo-random" cycle. -The blinking is visible to the eye -no use of delay but I bashed millis around to be a pseudo-delay, which might be outside the spirit of the challenge (it's in Setup though and only runs once, which might be OK?) -adding more LEDs requires no additional code at all, for a theoretically infinite number of LEDs! (bonus challenge 1 and 2, I think?) (but requires adding more XOR gate "taps" and 595 shift registers for the cycle to be max length) -LEDs blink pseudo-randomly (about as random as the RNG of many NES games--hint!) -After startup, at least 1 LED is always on, guaranteed! Notes about this specific solution: Since the 595 has a data register for the output, the taps (except for bit 7) needed to be offset to the bit 1 before The OR gate is simply there for the Uno to seed the initial required 1 bit into the register (state 0b00000000 is forbidden, and this cannot recover from it) Could simplify by using a Nano and setting the fuse for it to output it's clock onto a pin, and then use hardware (d-flip flops) to divide that down to a visible frequency-- would be "zero" code! I've not yet verified that the cycle length is maximal; i.e. that I made no mistakes in wiring. Could be verified in software by reading the 8 output bits of the 595 shift register with GPIO or smth and checking that all 255 states (not zero) are reached Inspired by the look of @ShadowsInTheSky 's solution above, which coincidentally looks a lot like the example on the Wikipedia article for linear feedback shift register (not the same underlying mechanism between these two though) Link to said wikipedia article: https://en.wikipedia.org/wiki/Linear-feedback_shift_register
Unknown User
Unknown User14mo ago
Message Not Public
Sign In & Join Server To View
WeeGee
WeeGee14mo ago
big fan of the ? operator in the digitalWrite line I'm curious how this handles the overflow of millis
Unknown User
Unknown User14mo ago
Message Not Public
Sign In & Join Server To View
PenPengu
PenPenguOP14mo ago
wtf
Unknown User
Unknown User14mo ago
Message Not Public
Sign In & Join Server To View
PenPengu
PenPenguOP14mo ago
gonna add a new bonus challenge: no external hardware 🤣 but actually no. External hardware is fun! 😆
WeeGee
WeeGee14mo ago
so no 595s to control more LEDs than GPIOs? darn :P (wouldn't an LED be "external hardware"? guess we gotta make-do with the RX, TX, and LED_BUILTIN :P)
PenPengu
PenPenguOP14mo ago
there are probably a lot of ways one could "trick" the challenge with external hardware 🤔
WeeGee
WeeGee14mo ago
simplest would be with those LEDs that have the built-in flasher chips; they tend to drift anyways
PenPengu
PenPenguOP14mo ago
lol
WeeGee
WeeGee14mo ago
so you'd get randomly blinking LEDs with just applying power
PenPengu
PenPenguOP14mo ago
that would be cheating though
WeeGee
WeeGee14mo ago
it would! reminds me of BigClive.com's Supercomputer panels, which are exactly this could exclude these from the challenge though because they techically have the entire set of code reqiured for each MCU in each LED, which I'd say counts as quite a few lines per LED :P -> duplicate code
PenPengu
PenPenguOP14mo ago
lmao well played
WeeGee
WeeGee14mo ago
this makes me think it'd be a neat bonus challenge; ensure that the timing for the LEDs is rock-solid stable even when millis or micros overflow
PenPengu
PenPenguOP14mo ago
atomic clock time
WeeGee
WeeGee14mo ago
I wonder if Wokwi would be fast enough to make an entire CPU out of effectively LUTs and D flip flops just make your own ASIC for blinking LEDs, lol
Unknown User
Unknown User14mo ago
Message Not Public
Sign In & Join Server To View
WeeGee
WeeGee14mo ago
What if interval is not a modulo of ULONG_MAX (or whatever that constant is)? Say that the modulo would have rolled over right at ULONG_MAX+1, but it doesn't because millis overflows? Wouldn't that mean that the delay there would be nearly 2 whole intervals? Ah forgot to ping, my bad. See above; what about odd modulo for the overflow?
Unknown User
Unknown User14mo ago
Message Not Public
Sign In & Join Server To View
WeeGee
WeeGee14mo ago
This shorter solution is definitely better if you just want to blink leds say, I strongly agree Some kinda tracking is required though if you want to keep consistent intervals, which sucks (Something like previous millis, or setting a "next millis" target by adding to millis, requiring either repetition or adding some kind of struct/OO thing Iirc blink without delay doesn't even fully handle it right since it adds the offset to current millis, not the target, so it can drift by a lot over time if the code consistently misses the "deadline" millis by 1 or 2 Don't you need to have a * - & pair in order to pass the previousMillis by reference? i.e. a * in the function def, and dereferences wherever it's used, plus an & in the function call to get the address of? Or am I failing a C/cpp syntax check here
Unknown User
Unknown User14mo ago
Message Not Public
Sign In & Join Server To View
WeeGee
WeeGee14mo ago
Or instrument it in simulation and run the simulation at 5000x realtime :P
Unknown User
Unknown User14mo ago
Message Not Public
Sign In & Join Server To View
WeeGee
WeeGee14mo ago
Ok fine-- 50,000x realtime (But yea the instrumentation step would be more time than that) Could just leave it as a warning to the user; "timings may be 2x the length every 5 months or so" May be up to*
Unknown User
Unknown User14mo ago
Message Not Public
Sign In & Join Server To View
WeeGee
WeeGee14mo ago
I've heard something like "90% of embedded development is easy; it's that last 10% that really gets ya" I think this specific challenge nails that 10% leftpad moment (although that's more an indictment(sp?) on npm than the og dev of leftpad)
PenPengu
PenPenguOP14mo ago
you can create your own IC in wokwi and program it 😆
WeeGee
WeeGee14mo ago
first solution second request
Unknown User
Unknown User14mo ago
Message Not Public
Sign In & Join Server To View
Unknown User
Unknown User14mo ago
Message Not Public
Sign In & Join Server To View
MaderDash
MaderDash14mo ago
on what type of board?
Unknown User
Unknown User14mo ago
Message Not Public
Sign In & Join Server To View
MaderDash
MaderDash14mo ago
I have some code for you to test on a scope please?
Unknown User
Unknown User14mo ago
Message Not Public
Sign In & Join Server To View
WeeGee
WeeGee14mo ago
Dang, even with direct port manipulation? I wonder what this is an artifact of; the timer being preset to a power of 2? something else bound to a timer?
Unknown User
Unknown User13mo ago
Message Not Public
Sign In & Join Server To View
jim lee
jim lee13mo ago
@WasTested I fear you're tring to force crayons to create fine art.
Unknown User
Unknown User13mo ago
Message Not Public
Sign In & Join Server To View
jim lee
jim lee13mo ago
Is "Was Tested" an omage to Adam Savage?
Unknown User
Unknown User13mo ago
Message Not Public
Sign In & Join Server To View
PenPengu
PenPenguOP13mo ago
btw I haven't forgotten about y'all. Will update winners and such soon 😄 And I always love new entries, discussions, ideas
Unknown User
Unknown User13mo ago
Message Not Public
Sign In & Join Server To View
WeeGee
WeeGee13mo ago
Fitting an entire malloc on an mcu with no mmu would be quite something
PenPengu
PenPenguOP13mo ago
Sounds interesting, so yes definitely allowed! 😄 And doing it just to blink two LEDs 😆
WeeGee
WeeGee13mo ago
Hey, it's a hard problem! Especially to so without preventing other things from functioning, or using up a ton of peripherals (timers, interrupts, etc.) Technically isn't an RTOS just the endgame solution for this challenge, anyways?
PenPengu
PenPenguOP13mo ago
I suppose yes 😄 but the other endgame would be making the solution as tiny as possible so no rtos
Unknown User
Unknown User13mo ago
Message Not Public
Sign In & Join Server To View
WeeGee
WeeGee13mo ago
That should all be laid out in some doc that goes into detail on the ABI, application binary interface iirc? It'd probably be easiest to find it searching for asm<->c interop documentation, things like register calling conventions, argument passing, caller/callee register saving, etc. etc.
PenPengu
PenPenguOP13mo ago
The implement of malloc on avr is actually pretty interesting imho
WeeGee
WeeGee13mo ago
If you make your own malloc, the easiest of course is just a straight slab allocator; helps with issues of memory fragmentation, but is a limiting paradigm to work with, and iirc doesn't play nice with naiive things that malloc teeny tiny things often?
PenPengu
PenPenguOP13mo ago
It's basically a linked list that is stored in the unallocated memory pointing to the next unalloyed memory block iirc
WeeGee
WeeGee13mo ago
It'd be interesting to see the full def of the nodes of that linked list I've messed with C structs trying to do unknown length members, but it seemed to go counter to most of C's guiding principles
PenPengu
PenPenguOP13mo ago
I think the node is just at the beginning of the unallocated block. Contains size of this block and pointer to the next node&block But I could be totally wrong I read that like last year and am on my phone now
WeeGee
WeeGee13mo ago
that sounds reasonable though since it's possible to do the whole append/insert/remove, and the linear time allocation doesn't matter too much as long as you're not allocating super tiny things
Unknown User
Unknown User11mo ago
Message Not Public
Sign In & Join Server To View
PenPengu
PenPenguOP11mo ago
Very cool! Thank you everyone for continuing to do this challenge!
Unknown User
Unknown User11mo ago
Message Not Public
Sign In & Join Server To View
Arduino Mod
Arduino Mod11mo ago
➕ Gave the role Trusted to zombean ➕ Gave the role Trusted to _wee_gee_
WeeGee
WeeGee10mo ago
wait why did I get the trusted role... in this channel? ._.
MaderDash
MaderDash10mo ago
caus your awesome? nad trustworthy?
WeeGee
WeeGee10mo ago
oh no I think I understand the trusted role, maybe, but it being given in this channel threw me lol
john roblox
john roblox10mo ago
Wokwi
LED Challenge - Wokwi ESP32, STM32, Arduino Simulator
Run IoT and embedded projects in your browser: ESP32, STM32, Arduino, Pi Pico, and more. No installation required!
john roblox
john roblox10mo ago
Honestly it was very easy Made another version in which you can adjust the delay of each individual LED with a potentiometer https://wokwi.com/projects/415791069211394049
PenPengu
PenPenguOP10mo ago
Never too late! I'm always happy about more submissions. Will look at it later when I'm not on the phone any more
WeeGee
WeeGee10mo ago
I see why so many people go for the nice object-oriented approach for this; the only real difference between the 3 LEDs' timer functions is the millis comparison, the toggle function, and the state variables like millis2 Feels like it'd be a good spot for a struct with function pointers or something tbh With the almost duplicate functions, I do wonder how much flash this'd take to blink as many LEDs as a Mega has gpio 🤔 Probably still insignificant honestly copy-pasting is easier to read though if you're not used to structs or such
john roblox
john roblox10mo ago
https://wokwi.com/projects/415872063559778305 Made another one, to be honest it is hard to work with pointers and references so I used something else
PenPengu
PenPenguOP10mo ago
Idea: Make a ranking or selection of winners in terms of "which solutions are the best for beginners to copy when they need to do multiple things simultaneously"
KLevi
KLevi10mo ago
@PenPengu I'd like to propose my solution. Wonder if you can find out how it works 😄
KLevi
KLevi10mo ago
KLevi
KLevi10mo ago
//Pins
#define RED 9
#define GREEN 8
#define BLUE 7

//Frequency of leds
#define rSet 5
#define gSet 30
#define bSet 100


uint8_t rCounter;
uint8_t gCounter;
uint8_t bCounter;

ISR(PCINT1_vect) {

rCounter++;
gCounter++;
bCounter++;

LEDs();
}


void setup() {
// put your setup code here, to run once:

PCMSK1 |= bit(PCINT8); // A0
PCIFR |= bit(PCIF2); // clear interrupts
PCICR |= bit(digitalPinToPCICRbit(A0)); // enable pin change interrupts for Analog pins

pinMode(RED, OUTPUT);
pinMode(GREEN, OUTPUT);
pinMode(BLUE, OUTPUT);
}

void loop() {
// put your main code here, to run repeatedly:
}

void LEDs() {
if (rCounter > rSet) {
digitalToggle(RED);
rCounter = 0;
}

if (gCounter > gSet) {
digitalToggle(GREEN);
gCounter = 0;
}

if (bCounter > bSet) {
digitalToggle(BLUE);
bCounter = 0;
}
}
//Pins
#define RED 9
#define GREEN 8
#define BLUE 7

//Frequency of leds
#define rSet 5
#define gSet 30
#define bSet 100


uint8_t rCounter;
uint8_t gCounter;
uint8_t bCounter;

ISR(PCINT1_vect) {

rCounter++;
gCounter++;
bCounter++;

LEDs();
}


void setup() {
// put your setup code here, to run once:

PCMSK1 |= bit(PCINT8); // A0
PCIFR |= bit(PCIF2); // clear interrupts
PCICR |= bit(digitalPinToPCICRbit(A0)); // enable pin change interrupts for Analog pins

pinMode(RED, OUTPUT);
pinMode(GREEN, OUTPUT);
pinMode(BLUE, OUTPUT);
}

void loop() {
// put your main code here, to run repeatedly:
}

void LEDs() {
if (rCounter > rSet) {
digitalToggle(RED);
rCounter = 0;
}

if (gCounter > gSet) {
digitalToggle(GREEN);
gCounter = 0;
}

if (bCounter > bSet) {
digitalToggle(BLUE);
bCounter = 0;
}
}
I want to tackle some of the bonus challanges aswell. Does my current solution break the rules of "no duplicate code"?
PenPengu
PenPenguOP10mo ago
A0 is floating? You use the AC noise as "clock" for the LEDs?
KLevi
KLevi10mo ago
Exactly 😄
KLevi
KLevi10mo ago
No description
PenPengu
PenPenguOP10mo ago
amazing idea
KLevi
KLevi10mo ago
I measured it and it was 1-2% accurate. Only drawback is that the smallest timestep it 10ms (50Hz AC, both triggers on falling and rising edge). Maybe a diode could be added to prevent negative voltages on A0
john roblox
john roblox10mo ago
Wow. I wouldn't have gone this far for this but that is innovative
KLevi
KLevi10mo ago
Only took me a couple minutes of coding. I already had the idea for the code in my had (And like 5 seconds to check the ports)
PenPengu
PenPenguOP10mo ago
or you could use INT which can trigger on falling or rising edge only or use PCINT and then inside the ISR, read the pin to figure out if it was triggered by rising or falling. But to get a fast clock, triggering on both edges seems actually good
PenPengu
PenPenguOP10mo ago
stupid idea that I had: Region lock DRM for your arduino sketch. Measure the frequency of a floating pin and then only allow the program to execute in north America where the frequency is 60Hz 😆
No description
KLevi
KLevi10mo ago
It could check the frequency for 5 seconds in the setup using a known timer like millis and then adjust to that. But yeah it could be used for what you said haha
Unknown User
Unknown User10mo ago
Message Not Public
Sign In & Join Server To View
PenPengu
PenPenguOP10mo ago
the main idea behind this challenge was teaching people how to use millis to multitask an arduino. 😄 But now people have found many more very creative solutions
Unknown User
Unknown User10mo ago
Message Not Public
Sign In & Join Server To View
WeeGee
WeeGee10mo ago
hey! mine used millis to great effect! I didn't need to store any funky variables, either :P (I wonder if a solution that uses millis and something like double-dabble could make a fast solution that uses negligible memory per new LED?) I might need to make that a thing after exams wow, even a regular integer division of bytes is painful on an atmega
Unknown User
Unknown User10mo ago
Message Not Public
Sign In & Join Server To View
jim lee
jim lee9mo ago
Here is a new challenge for you all.. Something I saw on the Arduino forum. Hookup a RGB LED. Can be in simulator. Using the same rules as the original challenge, run the LED from red to green and back. Overlapping 1/2 of each color with the next. Be able to vary the frequency while it's running. The light should look something like this..
No description
KLevi
KLevi9mo ago
The results of that working properly will be pretty!
john roblox
john roblox9mo ago
I actually made that once with an RPi and a sense hat but with 3 colours
Unknown User
Unknown User9mo ago
Message Not Public
Sign In & Join Server To View
john roblox
john roblox9mo ago
#define RED_LED 3
#define GREEN_LED 5

int8_t RED = 0;
int8_t GREEN = 127;


void setup() {
Serial.begin(115200);

pinMode(RED_LED, OUTPUT);
pinMode(GREEN_LED, OUTPUT);
}

void loop() {
analogWrite(RED_LED, abs(RED)*2);
analogWrite(GREEN_LED, abs(GREEN)*2);

Serial.print(abs(RED)*2);
Serial.print("\t");
Serial.println(abs(GREEN)*2);

RED++;
GREEN++;

delay(10); // Set this to vary the frequency
}
#define RED_LED 3
#define GREEN_LED 5

int8_t RED = 0;
int8_t GREEN = 127;


void setup() {
Serial.begin(115200);

pinMode(RED_LED, OUTPUT);
pinMode(GREEN_LED, OUTPUT);
}

void loop() {
analogWrite(RED_LED, abs(RED)*2);
analogWrite(GREEN_LED, abs(GREEN)*2);

Serial.print(abs(RED)*2);
Serial.print("\t");
Serial.println(abs(GREEN)*2);

RED++;
GREEN++;

delay(10); // Set this to vary the frequency
}
Took advantage of integer overflow
No description
PenPengu
PenPenguOP9mo ago
@ShadowsInTheSky @juan very cool! If you have wokwi (or similar) links for it, please post them 😄
PenPengu
PenPenguOP9mo ago
how did you do that graph?
john roblox
john roblox9mo ago
The serial plotter, when you output serial data it shows the button below the serial monitor
PenPengu
PenPenguOP9mo ago
oh lol I never tried that with wokwi 😆
Unknown User
Unknown User8mo ago
Message Not Public
Sign In & Join Server To View
PenPengu
PenPenguOP8mo ago
no In fact, if you have a creative solution that only works on a specific arduino then it could be even more interesting 😄
KLevi
KLevi7mo ago
Also... you might want to add a little explanation to it. Like that you are looking for help with your coding.
No description
KLevi
KLevi7mo ago
Still nothing. Also please remove the old links
Unknown User
Unknown User7mo ago
Message Not Public
Sign In & Join Server To View
KLevi
KLevi7mo ago
Still doesn't work for me
Unknown User
Unknown User7mo ago
Message Not Public
Sign In & Join Server To View
KLevi
KLevi7mo ago
So, now the link works Did you look at the requirements? You are not supposed to use delay, repeating code. Also in your hardware you have no current limiting LEDs. Plenty of ways to improve, you can look at the other's solutions
Unknown User
Unknown User7mo ago
Message Not Public
Sign In & Join Server To View
KLevi
KLevi7mo ago
Defeats the purpose of having requirements if you just ignore them. But if you don't want to improve, then sorry but we can't help you
Unknown User
Unknown User7mo ago
Message Not Public
Sign In & Join Server To View
KLevi
KLevi7mo ago
To improve your coding use millis() See files -> examples -> basics -> blink without delay
Unknown User
Unknown User7mo ago
Message Not Public
Sign In & Join Server To View
AnonEngineering
AnonEngineering7mo ago
those giant explosion symbols are fun though...
Unknown User
Unknown User7mo ago
Message Not Public
Sign In & Join Server To View
WeeGee
WeeGee7mo ago
afaik this is ongoing and open as a self-challenge dumb ideas welcome, so long as they try to meet the spirit of the challenge and aren't just someone else's code or something. Always up for interesting ideas the "spirit of the challenge" being "challenge yourself by making LEDs do the requested stuff without common coding pitfalls" with a bit of "see how much you can game the requirements and still be within them"
PenPengu
PenPenguOP7mo ago
timing led challenge is always open to new solutions! 👍 😄 WeeGee 100%
Unknown User
Unknown User7mo ago
Message Not Public
Sign In & Join Server To View
jim lee
jim lee7mo ago
You are so close, you should pack that into a "smart LED" class. The rate of flashing would give you an idea how fast the rest of your code is running.
KLevi
KLevi7mo ago
Cool, would be cooler if the current limiting resistors were not missing 😭
Unknown User
Unknown User4mo ago
Message Not Public
Sign In & Join Server To View
AnonEngineering
AnonEngineering4mo ago
You're a fast learner 😉
Unknown User
Unknown User4mo ago
Message Not Public
Sign In & Join Server To View
KLevi
KLevi4mo ago
Looks great, interesting and new approach! Have you thought about tackling the variable duty cycle and "single line add an LED" part?
Unknown User
Unknown User4mo ago
Message Not Public
Sign In & Join Server To View
KLevi
KLevi4mo ago
Yes you're right. It's the relationship between the length of a cycle and the portion that it's on for every cycle. (Almost like on - off ratio. 20% duty cycle means 20% on, 100-20= 80% off)
AnonEngineering
AnonEngineering4mo ago
PWM varies the amount of time the signal is HIGH, the more time a signal is high the more power is delivered to the load. In this way an "analog" output can be simulated, it isn't truly analog, the signal is only HIGH or LOW, but the power varies.

Did you find this page helpful?