Published on: 21st August 2024
In part 1 I had created a series of effects for my Christmas lights but found that the firefly effect I had downloaded was a bit glitchy. Sometimes a star would flash rapidly rather than ‘pulse’ which wasn’t easy on the eye.
The first thing to do was go through the code and rename the variables. They had names that were something to do with the effect but sometimes a bit misleading. For example the stars that had been chosen to glow were in an array called flashing. That had confused me to begin with; were the lights meant to flash? Surely not.
The renaming helped me understand what was happening. It was all very clever:
Originally the script updated on a star-by-star loop of 0.005 seconds so that all ten star changed every 0.05 seconds (so a twentieth of a second). I was guessing at this point so tried changing the loop so that it changed all ten stars in the same loop but every 0.05 seconds. This should make no difference to the overall effect - but it did:
You can see a video of the effect below. See that star changing colour twice? That explained what was happening. The random selection of stars could include stars that had already been chosen. So the loop was sometimes updating the same star twice but probably in different colours.
The fix was easy. Rather than randomly selecting a star using an integer in the range 1-n where n was the number of stars, I predefined a list of [[1, 2, 3 … n]] and randomly chose from the list. After each selection the chosen number was removed from the list. When that star had finished its pulse, its number was returned to the list so it could be selected again in the future. Problem solved!