Hello all, I'm having an issue programming the Arduito to convert and print a decimal number to binary. I got the code from a previously asked question in the old forum:
I modified the code since I needed 32 bits instead of 6. The issue happens in 8 numbers:
Number 13 prints number 25 to the LEDs in binary AND the opposite way around (number 25 prints number 13 in binary).
The same issue happens with numbers 13 and 25, 14 and 26, and finally 15 and 27.
Heres the code:
void setup() {
// initialize the digital pin as an output.
// Pin 13 has an LED connected on most Arduino boards:
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
pinMode(8, OUTPUT);
}
void loop() {
{
int var = 13;
digitalWrite(4, (HIGH && (var & B00010000)));
digitalWrite(5, (HIGH && (var & B00001000)));
digitalWrite(6, (HIGH && (var & B00000100)));
digitalWrite(7, (HIGH && (var & B00000010)));
digitalWrite(8, (HIGH && (var & B00000001)));
}
}
var = 13 (decimal implied) = B00001101
so 00001101
& 00010000
= 00010000 = 16
00001101
& 00001000
= 00001000 = 8
00001101
& 00000100
= 00000100 = 4
00001101
& 00000010
= 00000000 = 0
00001101
& 00000001
= 00000001 = 1
You have
HIGH && (logical AND) the number (var & Bxxxxxxx). If var & Bxxxxxxx >0 , then you have High && 1 == 1.
Seems like you would get a digitalWrite High for 4 of the 5 pins.
Am I missing something?
Yes, but instead of getting 01101 for 13, Im getting 11001 which is 25. It happens with the other numbers I specified in the first post. (You made a small mistake in the first operation, the result is 0 and not 16 =x ).
The function digitalWrite has parameters pin and value.
When value is set to the constant HIGH the output pin goes to 5V which lights your LED.
However you don't know what the constant HIGH is it may not be a 1.
Try printing out what the value of the constants HIGH and LOW are then check that you are calling digitalWrite with those values.
You are making an assumption that the constant HIGH has a value of 1, it might, but it might not.
It could be,for example, that HIGH has a value of 5 since the output has to go to 5V.
So I am suggesting you confirm what value HIGH is actually defined to have, then confirm that your logic correctly sends that value to the function.
I had a very quick look at the documentation to find the value of HIGH but could not find the actual value.
The boolean 'false' is 0. The boolean 'true' is any non-zero value.
I think you should be able to print values to the serial monitor using
Serial.print(val)
Serial.print(val, format)
but I have not tried these as my Arduino has not arrived yet
And i type:
int var = 10;
digitalWrite(5, (bitread(var,7)); will return 0
digitalWrite(6, (bitread(var,2)); will return 0
digitalWrite(7, (bitread(var,1)); will return 1
digitalWrite(8, (bitread(var,0)); will return 0
I would say check your wiring. All those number combinations flip bits 2 & 4.
Check the wires going to arduino pins 4 and 6.
My guess is that they are reversed.
While HIGH and LOW are currently 1 and 0, for portability
in situations like this it would be wiser to use a ternary operator to make sure you actually
use HIGH and LOW for the argument to the digitalWrite() function.
Example:
void loop()
{
int var = 13;
digitalWrite(4, ((var & B00010000) ? HIGH : LOW));
digitalWrite(5, ((var & B00001000) ? HIGH : LOW));
digitalWrite(6, ((var & B00000100) ? HIGH : LOW));
digitalWrite(7, ((var & B00000010) ? HIGH : LOW));
digitalWrite(8, ((var & B00000001) ? HIGH : LOW));
}
Now if it were me, I'd also use a bit mask generator macro that uses actual bit numbers to avoid accidental typos
vs using the Arduino bitmask Bxxx defines and for my old eyes it is easier for me to read than the Bxxxx defines.
The _BV() bit mask macro comes with the AVR C compiler.
i.e. _BV(2) is the same as arduino define B00000100