fmod()
[Math]
double fmod(double x, double y);
Description
Returns the remainder of the division of $\frac{x}{y}$.
Parameters
x
: The dividend value. y
: The divisor
value.
Returns
The remainder of the division of $\frac{x}{y}$.
Example Code
The code reads the value of two potentiometers connected to pins
INX
and INY
and place the remainder of the
division of $\frac{x}{y}$ on pin
OUT
.
void setup() {
(0, "INY"); // set the label of pin 0 to "INY"
pinLabel(1, "INX"); // set the label of pin 1 to "INX"
pinLabel(2, "OUT"); // set the label of pin 2 to "OUT"
pinLabel(2, OUTPUT); // set pin 2 as output
pinMode(3, "VCC"); // set the label of pin 3 to "VCC"
pinLabel(3); // set pin 3 as power pin
powerPin(4, "GND"); // set the label of pin 4 to "GND"
pinLabel(4); // set pin 4 as ground pin
groundPin}
void loop() {
int val_x = analogRead(0); // read the value of the potentiometers
int val_y = analogRead(1);
// map the values of the potentiometers from 0 to 1023
// to the range of 0 to 10
double x = map(val_x, 0, 1023, 0, 10);
double y = map(val_y, 0, 1023, 0, 10);
double z = fmod(x, y); // calculate the remainder of the division
int pwm = (int) map(z, 0, 10, 0, 1023); // map the value
(2, pwm); // write the value of pwm to pin 2
analogWrite}