0
\$\begingroup\$

I am reading data from an IR sensor array (8 sensors) and converting the ADC result to binary via a set threshold.

If n is the current result, for example: (N.B: n-1 is the previous result)

n-1 = 0b00111000
n = 0b01110011

I want to perform some logic operations in my Arduino code such that the result would yield 0b01110000 since the first group of 1's in 'n' 'anded' with 'n-1' yields a non-zero value, hence this is not discarded. Whereas the second gorup of 1's (consisting of two bits) 'anded' with 'n-1' yields a zero value, this is discarded.

Another example:

n-1 = 0b00111000
n = 0b10001110

Would result in 0b00001110 since the first group of 1's (consisting of 1 bit in this example) 'anded' with n-1 yields zero, hence this is discarded. Whereas the second group of 1's (consisting of 3 bits) 'anded' with n-1 yields a non-zero result. Hence, this group would not be discarded.

I can't figure out what bitwise operations I should use and how to separate the current result in separate groups of 1's to achieve this result.

asked Apr 17, 2021 at 0:39
\$\endgroup\$
6
  • 2
    \$\begingroup\$ Your description is hard to follow, but I don't think this can be done simply with bitwise operations. I think you are going to write a more rigorous definition of your algorithm and use some loops and conditional statements. \$\endgroup\$ Commented Apr 17, 2021 at 0:52
  • \$\begingroup\$ @ElliotAlderson I edited it for more clarity, the second example should explain it better. I was hoping i could achieve this in less computational power to avoid using a bunch of unnecessary loops and conditional statements \$\endgroup\$ Commented Apr 17, 2021 at 1:01
  • 1
    \$\begingroup\$ Bitwise operations don’t support the concept of a group of bits and so you can’t directly do this in the way that you want. Intuitively what you want is a function that can identify a group of bits and (presumably) check whether there’s a roughly-corresponding group in the previous byte. \$\endgroup\$ Commented Apr 17, 2021 at 1:36
  • \$\begingroup\$ Sounds like a crude way to quantize a low SNR ratio by clusters of bits. Why not improve the SNR in the 1st place since they don't appear to represent any IR current values and you are applying non-linear functions. Define the entire system otherwise , you may be chasing "skinny little rats" \$\endgroup\$ Commented Apr 17, 2021 at 2:48
  • \$\begingroup\$ "I am reading data from an IR sensor array (8 sensors) and converting the ADC result to binary" - isn't the ADC result already binary? \$\endgroup\$ Commented Apr 17, 2021 at 4:37

1 Answer 1

2
\$\begingroup\$

Something like this: (?)

uint8_t
Solve(uint8_t n, uint8_t prev_n)
{
 uint8_t result = 0;
 uint8_t group_bits = 0;
 uint8_t mask_bit = 0x80; 
 while (1)
 {
 if (n & mask_bit)
 {
 //If current mask bit is set in n, save it in the current group...
 group_bits |= mask_bit;
 }
 else
 {
 //If current mask bit is not set in n, then we found a group of 1 bits...
 if (prev_n & group_bits)
 {
 //Store to result if any of the current group of 1 bits is set in prev_n...
 result |= group_bits;
 }
 group_bits = 0;
 
 //Break loop if mask_bit shifted right 8 times...
 if (!mask_bit)
 {
 break;
 }
 }
 mask_bit >>= 1;
 }
 return result;
}
answered Apr 17, 2021 at 7:33
\$\endgroup\$
2
  • \$\begingroup\$ It wouldn't work if I had three groups of 1's. Or if the groups of 1's were in one nibble \$\endgroup\$ Commented Apr 19, 2021 at 14:44
  • \$\begingroup\$ @SimonSultana I guess I misunderstood the question. I modified the code, hopefully this is what you want. \$\endgroup\$ Commented Apr 19, 2021 at 22:02

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.