...example of how of use the modulo operator...
Suppose you want to find out if an integer is odd or even. The modulo operator (%) gives you the remainder after division. Then:
int wholeNumber;
int i;
int remainder;
for (i = 0; i < 30; i++) {
wholeNumber = i / 2;
remainder = i % 2;
Serial.print("wholeNumber = "); // integer division
Serial.print(wholeNumber ); // modulo
Serial.print(" remainder = ");
Serial.println(remainder);
if (remainder == 1) {
Serial.println("Number is odd");
} else {
Serial.println("Number is even");
}
}
If you put this in the setup() function and run it, you will see how modulo is different than division. Because the modulo gives the remainder after division, remainder will alternate between 0 and 1, while wholeNumber increases as the loop progresses. The increase will not be "smooth", however, since integer division truncates the result.