#include "WProgram.h" //function prototypes void binary_counter(); void reset_counter(); void print_digit(int theState); void print_counter(int val); // declare variables: int switchPin = 2; // digital input pin for a switch int switchPin2 = 5; int switchPin3 = 12; //my LEDs ;) int pinArray[] = {4, 3, 11, 10, 9}; // the state of the switches int switch_1_state = 0; int switch_2_state = 0; int switch_3_state = 0; // the state of the counter int state1 = 0; int state2 = 0; int state3 = 0; int state4 = 0; int state5 = 0; int decimalCounter = 0; void setup() { beginSerial(9600); pinMode(switchPin, INPUT); // set the switch pin to be an input pinMode(switchPin2, INPUT); // set the switch pin to be an input pinMode(switchPin3, INPUT); // set the switch pin to be an input for (int count=0;count<5;count++) { pinMode(pinArray[count], OUTPUT); } } void binary_counter() { //2**0 position if(state1 == 1){ digitalWrite(pinArray[0], HIGH); state1 = 0; }else { digitalWrite(pinArray[0], LOW); state1++; } //2**1 position switch(state2) { case 2: digitalWrite(pinArray[1], HIGH); state2++; break; case 3: digitalWrite(pinArray[1], HIGH); state2 = 0; break; default: digitalWrite(pinArray[1], LOW); state2++; } //2**2 position if((state3 >= 4) and (state3 <7)){ state3++; digitalWrite(pinArray[2], HIGH); } else if(state3 == 7){ digitalWrite(pinArray[2], HIGH); state3 = 0; } else { digitalWrite(pinArray[2], LOW); state3++;} //2**3 position if((state4 >= 8) and (state4 <15)){ state4++; digitalWrite(pinArray[3], HIGH); } else if(state4 == 15){ digitalWrite(pinArray[3], HIGH); state4 = 0; } else { digitalWrite(pinArray[3], LOW); state4++;} //2**4 position if((state5 >= 16) and (state5 < 31)){ state5++; digitalWrite(pinArray[4], HIGH); } else if(state5 == 31){ state5 = 0; digitalWrite(pinArray[4], HIGH); } else { digitalWrite(pinArray[4], LOW); state5++;} } void reset_counter() { state1 = 0; state2 = 0; state3 = 0; state4 = 0; state5 = 0; for (int count=0;count<5;count++) { digitalWrite(pinArray[count], LOW); } } void print_counter(int val) { printBinary(val); Serial.println(); } void loop() { // read the switch input: delay(1000); switch_1_state = digitalRead(switchPin); switch_2_state = digitalRead(switchPin2); switch_3_state = digitalRead(switchPin3); //switch 1 is a binary counter 2**4 //switch 2 controls the binary printer //switch 3 is a reset if(switch_3_state == 1){ reset_counter(); decimalCounter = 0; } else { if(switch_1_state == 1){ binary_counter(); if(decimalCounter == 32) { decimalCounter = 0; } else decimalCounter++; } } if(switch_2_state == 1){ print_counter(decimalCounter); } }