Hey folks. My task is to create a decimal (0-15) to binary converter using some loops. I did manage to print double result, but I can not light the LEDs. Basically the idea is like that:
I set the pins as output in Setup.
In Void I do a loop that print the result.
I also plan to light the LEDs here, so I store the binary result in array (BinaryList[3][14)
However I can not do that. My plan is to check in double for loop, every element of BinaryList, and if it is ==1 than I light a pin, else i keep it LOW. After delay 1000, turn all off. Could you please help me on that? I did that in several places, but it seems to do nothing. Also any recommendations on the code will be very helpful! Thank you!
Finally I do a conversion in my function called decToBinary();
int BinaryList[3][14]; //List with all of the binary numbers
int decNumber;
void setup() {
Serial.begin(9600);
for (int x=2;x<6;x++) //Set output pins;
{
pinMode (x, OUTPUT);
}
}
void loop() {
for (int decCount=0;decCount<16;decCount++) {
decNumber = decCount; //set the dec number
decToBinary();
Serial.println ("");
Serial.print ("Decimal: ");
Serial.println (decCount);
Serial.print ("Double: ");
for(int y=0;y<4;y++) { //adding arry to array list, and printing all of the values
BinaryList[y][decCount]=Binary[y];
Serial.print (BinaryList[y][decCount]);
}
Serial.println ("");
Serial.print ("------------");
}
}
void decToBinary () {
int remainder;
for (int i=3;i>=0 ;i--) {
remainder=decNumber%2; //determination of remainder
decNumber=decNumber/2; //division by 2
Binary[i]=remainder; //an array is getting the value of the decimal
}
delay(1000);
}```
To turn LEDs on and off, put their pin numbers in an array, use the value of the bit variable as the index to that array and bitValue to control the state of the LED for that bit
Thank you UKHeli! This is exactly what I am trying, and miserably failing to do. I made it more complicated intentionally, just to challenge my self if I will be able to think out how to do the conversion with my function, but I got stuck on the LEDs solution.
I understand, and thanks again. But the logic here is a bit different than what I got. For example my function do not return a value, that I can assign to bitValue instead it change existing variable. Maybe that is a mistake, but I messed the things with "return". Also my variable is an array of 4 that holds the binary number, but it is actually an int. Maybe it is not the right way to do things. But still is it not possible to take the values from list of array (BinaryList[3][14]), compare them, and set lights on when value is 1, and off when value is 0?
int BinaryList[3][14]; //List with all of the binary numbers
This array has 3 rows, each of 14 values but I thought that you wanted to count up to 15
Personally I would turn the array the other way round and have 15 rows, each of 3 values and declare it as a byte array so as not to waste even more memory that you already are. Ai would also populate the array in setup(), or a function called from setup because it only needs to be done once
To show the bit values on the LEDs, use a for loop (or the loop() function) to count from 0 to 15, use the current count as the index to the first level of the array and a for loop to get the value of each bit in that number and use that value to control the LED by using the inner for loop variable as the index to the associated LED pin
Having said all that, there is a simpler way as you have seen
I think that array count from 0, so if one needs array of 15 elements should actually declare array[14]. Also I do try with loop function, yes, but something I do wrong. I will post example, if I get to something useful.