Learn creative coding writing simple programs

72. Create visual rhythms using modulo

With the modulo function we can trigger events at regular intervals. If we have a variable "n" that increases each time draw() is called, we can write if(n % 11 == 0) { ... } to do something every 11th frame. The 0 value can be any value between 0 and 10. If you want to trigger just one event, that number is not important. But if you want to trigger two consecutive events, then you can for example use n % 11 == 4 for the first event, and n % 11 == 5 for the second event. For all the other values (0, 1, 2, 3, 6, 7, 8, 9, 10) nothing would happen. Don't worry if you don't understand how it works yet. Try watching the video and writing the example programs. We will keep working on it on coming episodes.

Tags: modulo, rhythm, animation, timing, events

Code editor

You can make changes to the code below. Then
Questions and comments are welcome! :) But please try to stay close to the topic of this episode. Use the Processing forums for help with your Processing projects (or hire me for help ;-)