I am creating laser tag guns and am having trouble converting the values read from the sensors and converting them into hexadecimals. I have a 8 value array representing the binary values. I need help converting the array into a hex value.
I am rather new to C++ and have came up empty on google. Thanks for any help,
Kirwinh
My issue is turning the array into a binary number. An example, int array[] = {1, 0, 0, 0, 1, 0, 1, 0}; // I need this to be converted to 10001010 or 138
I hope this better shows my problem. Thanks
My issue is turning the array into a binary number. An example,
int array[] = {1, 0, 0, 0, 1, 0, 1, 0}; // I need this to be converted to 10001010 or 138
I hope this better shows my problem. Thanks
You have an array, that's a good place to start from. What you need to do is iterate ( for() loop ) through the array elements and test each one. If it's a 1 set the corresponding bit in the result per Delta_G's 2nd link. Maddeningly, the array element indices are numbered in reverse order from the bits in a byte. Another puzzle.
Yeah kirwinh, what's this all about? Seems to be a unique situation. Here, I'll just give away the brute force code, so you can see what you need to do to deal with loose bits like these:
kirwinh:
My issue is turning the array into a binary number. An example, int array[] = {1, 0, 0, 0, 1, 0, 1, 0}; // I need this to be converted to 10001010 or 138
I hope this better shows my problem. Thanks
Well, no.
why do you have this array defined like this in the first place?
1a) or do you get it in another way - which you didn't disclose - and which can very well mean there is a MUCH better way of dealing with the problem?
why is an array of 1s and 0s an array of int?
Note: you can use a byte (uint8_t) as array of 8 bits, an unsigned int (uint16_t) as array of 16 bits, etc (you have at least uint32_t and uint64_t available).
Thanks everyone, this really helps!
Here is my function that is meant to record the incoming ir signals.
void receiveIR() { // Void checks for an incoming signal and decodes it if it sees one.
int error = 0;
if(digitalRead(IRreceivePin) == LOW){ // If the receive pin is low a signal is being received.
digitalWrite(hitPin,HIGH);
if(digitalRead(IRreceive2Pin) == LOW){ // Is the incoming signal being received by the head sensors?
received[0] = 1;
}
else{
received[0] = 0;
}
while(digitalRead(IRreceivePin) == LOW){
}
for(int i = 1; i <= 17; i++) { // Repeats several times to make sure the whole signal has been received
received[i] = pulseIn(IRreceivePin, LOW); // pulseIn command waits for a pulse and then records its duration in microseconds.
}
Serial.print("sensor: "); // Prints if it was a head shot or not.
Serial.print(received[0]);
Serial.print("...");
for(int i = 1; i <= 17; i++) { // Looks at each one of the received pulses
int receivedTemp[18];
receivedTemp[i] = 2;
if(received[i] > (IRpulse - 200) && received[i] < (IRpulse + 200)) {receivedTemp[i] = 0;} // Works out from the pulse length if it was a data 1 or 0 that was received writes result to receivedTemp string
if(received[i] > (IRpulse + IRpulse - 200) && received[i] < (IRpulse + IRpulse + 200)) {receivedTemp[i] = 1;} // Works out from the pulse length if it was a data 1 or 0 that was received
received[i] = 3; // Wipes raw received data
received[i] = receivedTemp[i]; // Inputs interpreted data
Serial.print(" ");
Serial.print(received[i]); // Print interpreted data results
}
Serial.println(""); // New line to tidy up printed results
// Parity Check. Was the data received a valid signal?
check = 0;
for(int i = 1; i <= 16; i++) {
if(received[i] == 1){check = check + 1;}
if(received[i] == 2){error = 1;}
}
Serial.println(check);
check = check >> 0 & B1;
Serial.println(check);
if(check != received[17]){error = 1;}
if(error == 0){Serial.println("Valid Signal");}
else{Serial.println("ERROR");}
if(error == 0){interpritReceived();}
digitalWrite(hitPin,LOW);
}
}
This gives me one array with the information of two 8 value bytes. I some how need to make it so that it outputs two bytes instead of an array. ChrisTenone's brute force version does exactly what I need, so I may just end up using that even though it isn't the best way to do it. Putting it into a normal form allows me to more easily interpret the information given. Any further suggestions would always be appreciated. Thanks everyone for being patient with me. -Kirwinh
Simply shift the bit received into an int variable. When you have done this 16 times you will have your two bytes. If the least significant bit comes in first shift to the right and add the bit.
int output = 0;
for( byte i = 0; i < 16; i ++){
output = output >> 1; // move existing bytes to the right
output |= getBit() << 15 ; // add new byte to the top bit of the result
}
Where the function getBit returns the next received bit
I really don't understand what you're trying to do in that snippet.
You have this array received[].
In the first if statement of the loop you read a pin, and store the result (1 or 0) in received*.* Then a while loop, waiting for a pin to go high (risky). Followed by a pulseIn() call for a LOW pulse on that same pin (mmm.... pulseIn() waits for the pin to go low from high before counting, but as it is low already that'd be the next pulse). The receive value is overwritten here without ever having been used, and the first if statement is redundant. Next you print out the pulse length - this may take long enough to miss even more pulses, especially as you're bitbanging the receipt of data. A for loop again - now inside the for loop you declare an 18-element array, of which only ever one element gets used. OK that's that. Now for your array to bits conversion: in your loop where you receive bits, just use the counter to place the bits in the correct place. *_ <em>*uint16_t result; for (byte i = 0; i < 16; i++) { result |= receiveBit() << (16 - i); }*</em> _* With receiveBit the function that receives the next bit somehow. Do give a good look at the DHT22 library as that's also a bitbanging library receiving a stream of bits that needs to be timed to know whether it's a 1 or a 0. Sounds quite similar to what you're doing. Trust me, this is NOT easy, it's pretty advanced programming.