To make it only run on odd (not even) variables, add in a new boolean and put the clock 'increment' section in an if statement
bool isRunning;
// ...
isRunning = switchPos % 2 == 1; // isRunning is true when switchPos is odd
// ...
if (isRunning) {
timeCounter++;
}
In this code snippet:
uint8_t switchPos; // switchpos will have a value between 0 and 15.
for (uint8_t i = 0; i < 4; i++) {
switchPos |= digitalRead(switch_pin[i]) << i;
}
I take advantage of the fact that digitalRead returns a boolean value, that is, a value that is either 0 or 1. It's actually a really condensed version, which is probably easier to read like this:
uint8_t switchPos; // switchpos will have a value between 0 and 15.
bool bit0, bit1, bit2, bit3;
bit0 = digitalRead(switch_pin[0]);
bit1 = digitalRead(switch_pin[1]);
bit2 = digitalRead(switch_pin[2]);
bit3 = digitalRead(switch_pin[3]);
switchPos = (bit3 << 3) | (bit2 << 2) | (bit1 << 1) | (bit0 << 0);
Breaking it down part by part:
bit0 = digitalRead(switch_pin[0]);
bit1 = digitalRead(switch_pin[1]);
bit2 = digitalRead(switch_pin[2]);
bit3 = digitalRead(switch_pin[3]);
This reads each switch pin (switch_pin[] is an array holding the four pins) and stores them as boolean values
switchPos = (bit3 << 3) | (bit2 << 2) | (bit1 << 1) | (bit0 << 0);
The << operator is a left-bit-shift, and works as such:
x << y = x * 2y
so if bit3 = 1, then bit3 << 3 is 23 = 8.
This is not very convenient in decimal, so lets look at it in binary.
bit3 is true, so we can see that in binary, you have something like 0b00000001. (the 0b denotes base 2)
If we multiply that by 8, or shift it three positions left, we get this: 0b00001000
Lets take the case where bit0 is 0, bit1 is 1, bit2 is 0, and bit3 is 1
In the code, we shift each one by a certain number of bits:
bit3 << 3 means it's shifted 3 bits, to 0b00001000
bit2 << 2 means it's shifted 2 bits, to 0b00000000 (since it's zero we don't see anything)
bit1 << 1 means it's shifted 1 bit, to 0b00000010
bit0 << 0 means it's not shifted, but it's zero, so it's 0b00000000
The other operator you probably don't know is the | operator, which is a bitwise or. It works such that if a given bit is true in EITHER operand, that bit will be true in the final operand, kind of like addition.
So you get something like this:
after shifting:
bit3 0b00001000
bit2 0b00000000
bit1 0b00000010
bit0 0b00000000
---------------------- After ORing them all together we get this:
0b00001010
If you convert that back in to base 10, you'll have a value of (0 * 1 + 1 * 2 + 0 * 4 + 1 * 8) = 10.
Notice that the maximum value you can get from this is if bit0, bit1, bit2, and bit3 are true, which would end up as 0b00001111
Converting that to decimal makes (1 * 1 + 1 * 2 + 1 * 4 + 1 * 8) = 15.
The minimum value is therefore if bit0, bit1, bit2, and bit3 are all false, which ends up as the expected 0b00000000
Converting that to decimal is just going to give us 0.
Thus, the code snippet takes in four binary values and makes it into a four-bit number, and when using decimal values, the limits of a four-bit number are 0 to 15. (--edit-- had 16 here originally, that was a fail --)
Hope that made sense - bitwise operations aren't the easiest to understand, but they make code a lot cleaner when you're dealing with boolean values =).