mbed_binary_counter

Dependencies:   mbed

Revision:
1:4e311ab3af04
Parent:
0:858e3371c27e
--- a/main.cpp	Tue Oct 07 23:55:54 2014 +0000
+++ b/main.cpp	Thu Oct 09 20:30:07 2014 +0000
@@ -1,36 +1,54 @@
+// for LPC1768
+//binary counter with 4 onboard leds. counts +1 with every button press
+
 #include "mbed.h"
 #define PRESSED 0
 #define UNPRESSED 1
-
-int getNext();
+#define BUTTONPIN p8
 
-DigitalIn pin8(p8); //button on pin8
-BusOut counter(LED4, LED3, LED2, LED1);
+int getNext(); //get next num for counter. ( counts 0 to 15) after 15 there is a rollback. 
+void setLeds(int);  //sets the leds to show in binary the argument
+Serial pc(USBTX, USBRX);//initialize serial connection
+
+DigitalIn button(BUTTONPIN); //button on pin8
+DigitalOut leds[4]={ DigitalOut(LED1), DigitalOut(LED2), DigitalOut(LED3), DigitalOut(LED4) };
  
 //----------------------------------------------- 
 int main() {
     
-    pin8.mode(PullUp); //set internal pullup
+    button.mode(PullUp); //set button internal pullup
     
-    bool previousState=pin8;
+    bool previousState=button;
     bool currentState;
    
-    while(1) {
+    while(1) {//polling
         
-        currentState=pin8;
-        if(currentState==PRESSED && previousState==UNPRESSED){
-              counter=getNext();  
+        currentState=button;
+        if(currentState==PRESSED && previousState==UNPRESSED){ //on button push
+              setLeds(getNext());
+
             
         }
         previousState=currentState;
-        wait(0.1);
-        
-        
-        
+        wait(0.1); //small delay for debounce
+  
     }
 }
-
-
+//------------------------------------------
+void setLeds(int num){
+        
+              int eights    =num/8;     //how many of 8's the number has...
+              int fours     =(num - eights*8) /4 ;
+              int twos      =(num - eights*8 -fours*4)/2;
+              int ones      =num%2; 
+              
+              leds[0]=eights;
+              leds[1]=fours;
+              leds[2]=twos;
+              leds[3]=ones;
+              pc.printf("dec=%d bin=%d%d%d%d \n",num,eights,fours,twos,ones); 
+    
+}
 //-----------------------------------------------
 int getNext(){
     static int i=0;