Sorting input by odd/even?

I have a sketch in development which takes input from push button switches, read using 'byte' which returns a decimal value for the button pressed. The action that follows is dependent upon whether the buttonNo variable is either odd or even.

In my original project I only had four options so using an 'if' statement with 'or' was manageable. However, my new project has 11 options so using 'or' will be clunky to say the least. Is there a way to simply determine if a number in a variable is odd or even?

Here is the loop code up to the point of the 'if' statement - what happens afterwards isn't really relevant!

void loop() {
  byte buttonNo = PINB & 0b00001111; //Read pins 8-11 as input
  while (buttonNo != 15) {
  sigNo = (butSig[buttonNo]); // Fetch relevant Signal/Servo number for the button pressed
  offAng = (sigOffAry[sigNo]); // Fetch the 'Off' angle for that signal
  bdel = (bdelAry[sigNo]); // Fetch the starting speed delay for that signal
  Serial.print("Button pressed is:");
  Serial.println(buttonNo);

  Serial.print("Start Signal Status variable is:");
  Serial.println(sigStat[(sigNo)]);

    if ((buttonNo == 1) || (buttonNo == 3) || (buttonNo == 5) || (buttonNo == 7)) {
    // Signal Action is to pull Off

A number is even when the remainder after division by two is zero. You can use the modulo operator to calculate this remainder.

if (buttonNo % 2) {
  Serial.println("odd");
}
else {
  Serial.println("even");
}

Divide it by 2. If there is no remainder it is even

if (number % 2)
{
  //number is odd
}
else
{
  //number is even
}

Thanks both - That makes obvious sense!!

you read all pins and mask the result.
Why not just mask every second bit? --> do one action
Read and Mask the other bits --> do the other action

If pin8 (bit 0 of PORTB) is 1, the number entered is odd.

if (PINB&1) Serial.println("odd number");

Another very simple & elegant solution - thanks very much!

I would simply test the LSB (Least significant bit) If 1 odd if 0 even, size of number does not matter and it should be faster than the modulus divide.

Hmm - thinking about this - surely this would only work while the button is pressed? I have a momentary action button and there's no guarantee that when the code got to that point, I'd still have my finger on the button - in which case it might not return the correct result (if I release the button, bit 0 will be a 0 even if I had originally pressed an odd-numbered button?).
I know the code executes very quickly so it's possible it would work. I'll have to test it out I think.
The divide by 2 using remainder (%) would work on the captured result of the button press so would work regardless of whether I had released the button.

The only time it matters whether you are pressing the button or not is the instant that you digitalRead() the pin it's on.

Many Arduino loop() functions execute at a relatively high rate. A typical pattern is to read all the buttons and switches once, as the loop begins anew, and use those values through out the rest of the function.

Microprocessor fast, human slow. There is no reason to worry about button skew, a term I couldn't possibly have just made up.

HTH

a7

Ah - OK! I see that. Thanks very much. I'll certainly give it a try although speed of execution is not really an issue given the purpose of the project!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.