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

Try to stay close to the topic of this episode. Use the Processing forums for help with unrelated Processing projects (or hire me for help ;-)

To indicate that a word in your comment is code, use the `backtick`. Example
Do `float` and `int` smell similar?
To highlight code blocks, surround it with ``` code-fences ``` like this:
``` void setup() { size(600, 600); } ```