/* littleBits Game Counter Gabriel Wilkes Nov 4, 2015 Based on code from littleBits Arduino Counter by Richard Born: http://littlebits.cc/projects/the-littlebits-count */ const int countButton2 = 0; // this button counts up and down presses of the 2nd count button const int resetButton = A0; // this button will reset counter to zero const int countButton = A1; // this button counts up and down presses of the count button const int unitsNumber2 = 5 // displays count unit 2 const int unitsNumber = 9; // displays count units // The following array converts PWM values to integer values from 0 to 99 // For example: numbers[0] = 0; numbers[1] = 3; numbers[2] = 6; numbers[3] = 8, etc. // This allows us to display a number on the number module based upon the corresponding PWM value int numbers [] = {0,3,6,8,10,13,15,18,20,23,26,28,30,33,35,38,40,43,45,48,50,53,55,58,61, 64,66,69,71,74,76,78,81,84,86,89,91,94,96,99,102,104,106,108,111,114,116, 119,121,124,127,129,132,134,136,139,141,143,146,149,151,154,156,159,160,163, 166,169,171,173,176,179,181,184,186,188,190,193,196,199,202,205,208,210,212, 214,217,218,221,224,227,230,232,235,237,239,242,245,247,252,255}; int resetButtonState = 0; // current state of the reset button int countButtonState = 0; // current state of the count button int countOldButtonState = 0; // previous state of the count button int countButtonState2 = 0; // current state of the 2nd count button int countOldButtonState2 = 0; // previous state of the 2nd count button int count = 0; // counts the number of presses of the count button int count2 = 0; // counts the number of presses of the 2nd count button int upsAndDowns = 0; // counts presses and releases of the count button int upsAndDowns2 = 0; // counts presses and releases of the 2nd count button void setup() { pinMode(countButton2, INPUT); // sets pin to INPUT mode pinMode(resetButton, INPUT); // sets pin to INPUT mode pinMode(countButton, INPUT); // sets pin to INPUT mode pinMode(unitsNumber2, OUTPUT); // sets pin to OUTPUT mode pinMode(unitsNumber, OUTPUT); // sets pin to OUTPUT mode } void loop() { resetButtonState = digitalRead(resetButton); // get the state of the reset button countButtonState = digitalRead(countButton); // get the state of the counter button countButtonState2 = digitalRead(countButton2); // get the state of the 2nd counter button if (resetButtonState == HIGH) { // reset both numbers and up and down counter to zero upsAndDowns = 0; upsAndDowns2 = 0; analogWrite(unitsNumber2, numbers[0]); analogWrite(unitsNumber, numbers[0]); } if ((countButtonState == HIGH && countOldButtonState == LOW) || (countButtonState == LOW && countOldButtonState == HIGH)) { // counter button has been pressed down or has been released upsAndDowns += 1; count = upsAndDowns/2; // actual count is half the ups and downs countOldButtonState = countButtonState; // saves the old state of the count button analogWrite(unitsNumber, numbers[count % 100]); // displays the units counts } if ((countButtonState2 == HIGH && countOldButtonState2 == LOW) || (countButtonState2 == LOW && countOldButtonState2 == HIGH)) { // counter button has been pressed down or has been released upsAndDowns2 += 1; count2 = upsAndDowns2/2; // actual count is half the ups and downs countOldButtonState2 = countButtonState2; // saves the old state of the count button analogWrite(unitsNumber2, numbers[count2 % 100]); // displays the units counts } delay(20); // debounce time for buttons }