Hello,
I am trying to create a simple counter that will count down from 15 to 1 in binary with my arduino uno. As of right now I am making it print to the serial monitor but once I get the code worked out I wanted to make it light up LEDs to represent the bits.
Here is my code:
int new_Dop; //this is the difference between the number and the power two
int old_Dop; //this is to store the difference above
int prev_Dop; //this is to store the old difference
int num; //this is the decimal number that will be converted
int pwr; //this is the number that 2 will be raised to the power of
void setup(){
Serial.begin (9600);
Serial.println ("Binary converter 4.0"); //this is my fourth time with this program every time slimming it down more and more
}
void loop (){
for (num = 15; num >= 0; num--) {
old_Dop=num; //this is so the first time through the wow function the number in decimal will be in place
Serial.println ("--");
Serial.print ("the number is "); Serial.println (num);
wow();
Serial.println (" ");
}
}
void wow(){
for (pwr = 3; pwr >= 0; pwr--){ //this allows the loop to be run 4 times and so there will be 4 bits ex: 1011 for 11
new_Dop = old_Dop - pow(2,pwr); //this is the part that converts the decimal into binary one place at a time starting in the 8's
prev_Dop = old_Dop; //this stores the old difference to be used later if the new_Dop goes through the else statement
if (new_Dop >= 0){
Serial.print ("1");
old_Dop = new_Dop; //this is here to run the next place in binary
}
else{
Serial.print ("0");
old_Dop = prev_Dop; //this is here to set it back so if it can run the next place in binary
}
}
}
Essentially the program is taking a number in base ten and converting it to base 2 using the "descending powers of 2 subtraction" Here is a link that describes that in detai:
The function "wow()" subtracts the Decimal number from 2^3 then checks if it is greater than or equal to 0 and prints a 1 or 0 accordingly then it does the process over again subtracting the remainder (or if it was less than zero the previous number) by 2^2 and so on.
However... when it comes to the numbers 11, 7, and 3 (1011, 0111, 0011 in binary respectively) it decides it wants to give me the binary numbers for 12, 8, and 4 (1100, 1000, 0100) respectively. I made another copy of this code that prints out basically every change in the variables so I could figure out why it was doing this, and the thing that I saw where there is something wrong was at 11 for the 4's place that 3-4=0... Then at 7 for the 8's place it is saying 7-8=0. Finally for 3 in the 4's place again it is saying 3-4=0.
I am not receiving errors when I run the sketch and I have looked at this too the point where I can't see what's wrong and I have a feeling a fresh pair of eyes will be able to find the problem in a matter of mere seconds!
Thank you for the help and sorry for the long windedness on something so (seemingly) simple, I'm just starting to learn Arduino and programing in general so its all a little tricky.
-pacodataco