foivos Hristou / Mbed 2 deprecated mbed_binary_counter

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 // for LPC1768
00002 //binary counter with 4 onboard leds. counts +1 with every button press
00003 
00004 #include "mbed.h"
00005 #define PRESSED 0
00006 #define UNPRESSED 1
00007 #define BUTTONPIN p8
00008 
00009 int getNext(); //get next num for counter. ( counts 0 to 15) after 15 there is a rollback. 
00010 void setLeds(int);  //sets the leds to show in binary the argument
00011 Serial pc(USBTX, USBRX);//initialize serial connection
00012 
00013 DigitalIn button(BUTTONPIN); //button on pin8
00014 DigitalOut leds[4]={ DigitalOut(LED1), DigitalOut(LED2), DigitalOut(LED3), DigitalOut(LED4) };
00015  
00016 //----------------------------------------------- 
00017 int main() {
00018     
00019     button.mode(PullUp); //set button internal pullup
00020     
00021     bool previousState=button;
00022     bool currentState;
00023    
00024     while(1) {//polling
00025         
00026         currentState=button;
00027         if(currentState==PRESSED && previousState==UNPRESSED){ //on button push
00028               setLeds(getNext());
00029 
00030             
00031         }
00032         previousState=currentState;
00033         wait(0.1); //small delay for debounce
00034   
00035     }
00036 }
00037 //------------------------------------------
00038 void setLeds(int num){
00039         
00040               int eights    =num/8;     //how many of 8's the number has...
00041               int fours     =(num - eights*8) /4 ;
00042               int twos      =(num - eights*8 -fours*4)/2;
00043               int ones      =num%2; 
00044               
00045               leds[0]=eights;
00046               leds[1]=fours;
00047               leds[2]=twos;
00048               leds[3]=ones;
00049               pc.printf("dec=%d bin=%d%d%d%d \n",num,eights,fours,twos,ones); 
00050     
00051 }
00052 //-----------------------------------------------
00053 int getNext(){
00054     static int i=0;
00055     if(i==15)i=0;
00056     else i++;
00057     
00058     return i;
00059     
00060     }