Need some help figuring out my code

Hey there,

I'm making a Christmas tree that shoots snow over itself. There is a buzzer and 12 LEDs hooked up to my Arduino, what I'm trying to achieve is playing a Christmas song using the Arduino while making a white red white red all on… effect with my LEDs. I just can't seem to make this code look pretty without just putting the LED changes inside the song. (the program starts once a button is pressed and keeps looping indefinitely until the button is pressed again.) Can someone direct me to some articles that could solve my problem?
Thanks in advance!

Kind regards, Onno Jacobs.

What code would that be?

Considering all I got to work was putting the LED changes inside the song nothing really, I think I need to use interrupts, but I don't really understand them. The article I found on them confused me quite a bit.

As one may guess from your post you are playing the sound in Loop().

You could either integrate a if (millis() - Waittime > lastmillis) (as stated below do not use: millis() > lastmillis + Waittime) type statement where you switch the LEDs color/state or use a separate timer routine.

Have a look at and use Timer 1 (as it is suitable for longer time periodes).

(Please also recognize the friendly hidden hints that your problem description is quite vague for a reasonable assistance :wink: ).

OK, so if I understand correctly, I need to use interrupts to interrupt the song I'm playing and change the states of the LEDs? Can I also use this to check once every 200ms or so if a button is pressed, or will this make my song buggy?

Yeah, I'm sorry. About the vagueness, I'm a 16-year-old high school student who's trying to get the ropes on Arduino since I usually only use it for simple projects. I have never been great at putting what I'm thinking into words :confused:

I suggest you check into color organs. They blink with the music but rely on an audio input.

An Arduino can only do one thing at a time, so it is best to use an interrupt for this then.
For example a Raspberry Pi you can easily start multiple tasks (functions), and you can write your code in Python, which can be a bit easier to get started with programming.

I know a decent amount of Python, but this project is space restricted, and a raspberry just isn't a good fit for it. Plus it's meant to serve as a gift to someone (as per the task we were given by the teacher) making a raspberry a little expensive to use.

I don't think this is what I'm looking for. I need a solid pattern and not one that depends on the music. Thanks for the help, though!

We're an hour and ten posts in.
Are we missing something?

You should investigate (and test with short examples) how to use external interrupts to read changes at e.g. pins 2 or 3 . There is plenty of helpful material about this topic available.

Playing songs from loop() is not "professional" but acceptable if your controller does not have to fulfill realtime relevant issues. Don't worry, but keep that in mind for the future (it is like learning guitar the wrong way, it may sound alright but is hard to overcome when you go to the next level.)

Reading buttons is also something as mentioned above, using interrupts is future proof, looks and feels also "more elegant!, you will see!

No offence regarding your post, also something to grow ...

Alright, I understand what you're trying to say and think I get it, I'll have to check more on it tomorrow as it's getting rather late.

Thanks a lot for the advice!

Kind Regards.

Good luck!

For external interrupts you may refer to this website External Interrupts


Never use future time stamps (except in some really unusual circumstances. To handle integer overflow you need to:

if (millis() - lastmillis >= Waittime)

Something like that?

Not an Destructable!
Particularly not one that recommends

attachInterrupt(0, blink, FALLING);

Close, but not quite. In this video it is using the LEDs as note indicators, these LEDs are most likely triggered in the song itself rather than separately. Here the creator is just changing led states per note. I, too, could use this, but it would look very messy, and I don't like messy code.

Sorry, you are right... too quick answer ...:wink: I will correct it in my post...

At least they are referring to arduino - attachInterrupt where the safe use is described ...

attachInterrupt(digitalPinToInterrupt(pin), ISR, mode)

So what you - and the two enthusiastic interjectors - don't understand is that interrupts are not appropriate. :face_with_raised_eyebrow:

And apparently are not knowledgeable about "millis()" either.