Modulus programming uses

Post test messages here

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
GhostStrype
 
Posts: 18
Joined: Fri Aug 08, 2014 6:20 pm

Modulus programming uses

Post by GhostStrype »

Beyond the obvious remainder use for modulus I notice it gets used to sort of set up patterns in LED array programming. It sort of acts as a way to jitter the array if that makes sense.
Basically just looking for a deeper explanation on why / how that is used as a sort of shortcut effect on LED arrays or strips. Or also if there is some term for using modulus in this way (It has been an elusive thing to find and seems like a sort of programming trick dealing with modulus remainder patterns).
I think I recently saw it in the Open Pixel Control github example code of raver_plaid.py where it was used to make diagonal dark stripes in an LED grid.

User avatar
adafruit_support_mike
 
Posts: 67454
Joined: Thu Feb 11, 2010 2:51 pm

Re: Modulus programming uses

Post by adafruit_support_mike »

In math language, the modulus operator gives you a way to create periodic (repeating) patterns from a monotonically increasing sequence (1, 2, 3, ... 101, 102, 103, ...).

Periodic sequences have three defining parameters: period, amplitude, and phase. The modulus operator gives you the power to control period and phase in an easy-to-use way:

Code: Select all

    periodicValue = ( sequenceValue + phaseOffset ) % period;
You can use the periodic value as the argument to a function that gives you an amplitude:

Code: Select all

    output = sin(( sequenceValue + phaseOffset ) % 360 );
or you can use it as the index into an array of arbitrary amplitude values:

Code: Select all

    output = arbitrary[ ( sequenceValue + phaseOffset ) % ARRAY_SIZE ];
You could use that to control the activation patterns for the servos in a hexapod, for instance.

Fourier analysis says you can reproduce any pattern by adding together enough sine waves with different periods and phases, and using an array lets you generate arbitrary patterns in a single step. The modulus operator and simple value functions (treating arrays as a special form of simple value function) give you the power to handle any signal processing operation.

Locked
Please be positive and constructive with your questions and comments.

Return to “Test Message Forum (closed)”