You are on page 1of 2

Laboratory Activity 1: DEC TO BIN

Azarcon, John Benedict B.


Department of Electronics, Computer,and Communications Engineering
Ateneo de Manila University
Quezon City, Philippines
February 22, 2021
john.azarcon@obf.ateneo.edu

Abstract—An Arduino sketch was written that II. SCREENSHOT OF SIMULATIONS


has the ability to convert decimal numbers from 0
to 1023 to its binary equivalent. The said binary
numbers are represented through the LEDs
displayed in the Arduino.

I. COMPUTATIONS
The first algorithm determines the size of array that
is to be declared.
void loop() {
Numcpy=number;
expn=1;
expset=1;
while(number>expset*2-1) {
expset*=2;
expn++;
}
The second algorithm, is where each digit of the converted Figure 1. Arduino Output when DEC=1
binary value is stored as an individual array element. (Binary=0001)
int bindigit[10]; //10 bits

for(int digit=0; digit<expn; digit++) {


digitalWrite(digit+2, LOW);
bindigit[digit]=0;
if(Numcpy%2==1){
digitalWrite(digit+2, HIGH);
bindigit[digit]=1;
}
Numcpy/=2;
}

The code ends with an if statement which is


responsible in resetting the integer to 0 if the
number exceed 1023.

Figure 2. Arduino Output when DEC=3


(Binary=0011)

III. PROGRAM LISTING


void setup() {
for(int ledPin=2; ledPin<=13; ledPin++)
pinMode(ledPin, OUTPUT);

XXX-X-XXXX-XXXX-X/XX/$XX.00 ©20XX IEEE


Serial.begin(9600); for(int ledPin=2; ledPin<14; ledPin++)
} digitalWrite(ledPin, LOW);
}
int number=0, Numcpy, expn, expset;
}

void loop() {
Numcpy=number; REFERENCES
expn=1; [1] C.M . Oppus, J.Lim, Marinas J.A., Arduino
expset=1; Laboratory Manual, Quezon City: Ateneo de
Manila University, 2
while(number>expset*2-1) {
expset*=2;
expn++;
}

int bindigit[10]; //10 bits

for(int digit=0; digit<expn; digit++) {


digitalWrite(digit+2, LOW);
bindigit[digit]=0;
if(Numcpy%2==1){
digitalWrite(digit+2, HIGH);
bindigit[digit]=1;
}
Numcpy/=2;
}

Serial.print(number);
Serial.print(" = ");

for(int digit=expn-1; digit>=0; digit--)


Serial.print(bindigit[digit]);
Serial.print("\n");
delay(100);
number++;

if(number==1024) {
number=0;

You might also like