Hi, I am going to apologise because im sure this has probably been asked a dozen times but Google is not being my friend but i am after a way to convert a binary signal from infrared to a integer.
I am currently sending a value of 010101 which is 21 and i receive the binary and store it in a array now my issue is converting it back to a integer for processing however all the codes on Google seem to either not work, be buggy or return different values other than 21.
if anybody can supply me with some example code or some links i would much appreciate it.
P.S im using Binary to Decimal Converter to convert it from a binary to a decimal so if anybody does post code if you could check that my sample works.
PaulS i hate to disagree with you if you type the binary into the website linked it gives i just checked...
and as for the code that is where i need the help i have a array name Binary[6] and it contains 6 values being 010101 that are stored i need this as an integer.
The thing you are doing now is possible of course...I have done some quick code.
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
}
void loop() {
// put your main code here, to run repeatedly:
//010101
byte array_containing_bit_values[6]={0,1,0,1,0,1};
int j=5;
byte total_value=0;
for (byte i=1; i<64;i*=2)
{
total_value+=array_containing_bit_values[j]*i;
Serial.print("i is equal to:");
Serial.print(i);
Serial.print(" j is equal to:");
Serial.println(j);
delay(500);
j--;
}
Serial.println(total_value);
delay(500);
}
PaulS:
Which means right. Or pedantic, whichever you prefer.
No, because it is not in a code context. It was just a naked 010101, unless you are actually a compiler - which would explain a lot - there is no reason to make it octal, especially when told it was binary.
I interpreted it as a thinly veiled complaint at the vagueness of the post. I also felt like my gray matter hurt from trying to grasp exactly what the OP was trying to do. It was stated to be binary, but with no explanation of the storage or transmission format. Indeed, we didn't get the explanation until reply #5.
Delta_G:
When writing code it really helps to be able to think like a compiler.
Right, though it is probably better to think like an interpreter, not a compiler. Which has nothing to do with this, since we were talking about a naked binary constant, no code involved.
KeithRB:
Right, though it is probably better to think like an interpreter, not a compiler. Which has nothing to do with this, since we were talking about a naked binary constant, no code involved.
The language of technical discourse is context dependent. Of course we do not use C++ to describe everything. I think it should be, "think like a programmer". Write in clear, explanatory, commonly accepted technical language. Read as if the writer is similarly versed.
A quick, naive read of "010101" does provoke the question of why it has a leading zero. We didn't get an answer until post #5. If the OP had been forthright in describing the whole scheme, it would have been very obvious.
There are so many mistakes, omissions and canards in problems that are posed here that we are inured to taking everything "with a huge grain of salt". Or poking fun. The most social way to overcome that is to simply request more details, which I try mightily to do. Even when I believe that a poster should be able to read their own post and recognize that it doesn't communicate clearly and sufficiently.
I am reminded of the (legendary, possibly stereotyped) Zen master technique of beating the student with a stick. I believe that although seeming cruel, the concept is to force the student to assume responsibility for their own learning. In many situations, it's more effective than guidance.
Although mostly healed, I have had the bruises to show for my learning...
Okay firstly I would like to apologise for not being clear my data is taken from a infrared sensor which receives a signal from another arduino board that encodes the data to be sent.
The transmitter at the moment only accepts 0's and 1's I have yet because I have yet to give it a method to encode ints into decimal bits to send so I just plugged in 010101 into the online converter and it gave me 25 so I went from there now I need to convert it back to a int on the receive side.
The receive side stores each bit in an array and that was my choice if there is a better way of dealing with raw bits advice is appreciated.
I will be sending two sets of data in one transmission one value from 0-999 and another from 0-9999 separated by a delimiter so I need the final results in an int preferably so that I can process the data easier.
A breakdown will be the transmit program has to dynamic values sent...
Value A from 0-999
Value b from 0-9999
And it will convert this to binary and transmit the 1's and 0's with pulse length modulation.
(I have not made the part of the program on the transmit side to turn the values into binary, so I just used a random binary value of 010101 I realise this is my error)
Hopefully I clarified afew things and I hope someone can provide me with some reliable code for converting binary to ints
The IR libraries I have seen use an unsigned long to record IR codes. Instead of labouring over serializing it as a string and decoding the string at the other end, you should just send the unsigned long as four bytes.