Guest User

Binary to BCD Converter

a guest
Apr 19th, 2012
2,548
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. int inputPins[8] = {A0,A1,A2,A3,A4,A5,2,3};
  2. int outputPins[10] = {4,5,6,7,8,9,10,11,12,13};
  3.  
  4. void setup(){
  5.   Serial.begin(9600);
  6.   for(int derp=0; derp<8; derp++){
  7.     pinMode(inputPins[derp], INPUT);
  8.   }
  9.   for(int durp=0; durp<11; durp++){
  10.     pinMode(outputPins[durp], OUTPUT);
  11.   }
  12. }
  13.  
  14. void loop(){
  15.   output(getbcd(getinput()));
  16. }
  17.  
  18. int getinput(){
  19.   int output = 0;
  20.   output += digitalRead(inputPins[0])*1;
  21.   output += digitalRead(inputPins[1])*2;
  22.   output += digitalRead(inputPins[2])*4;
  23.   output += digitalRead(inputPins[3])*8;
  24.   output += digitalRead(inputPins[4])*16;
  25.   output += digitalRead(inputPins[5])*32;
  26.   output += digitalRead(inputPins[6])*64;
  27.   output += digitalRead(inputPins[7])*128;
  28.   return output;
  29. }
  30.  
  31. int getbcd(int input){
  32.   int bcd = 0;
  33.   int n1 = floor(input/100);
  34.   int n2 = floor((input-n1*100)/10);
  35.   int n3 = input-n1*100-n2*10;
  36.   bcd = n3 + (n2 << 4) + (n1 << 8);
  37.   return bcd;
  38. }
  39.  
  40. void output(int bcdint){
  41.   for(int llama=0; llama<11; llama++){
  42.     digitalWrite(outputPins[llama], (bcdint&1<<llama?HIGH:LOW));
  43.   }
  44. }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×