Bit manipulation of sensor data


hello
i new programming new arduino.  have set of 8 sensors

the 8 sensor gives bit stream.  bit stream corresponds relative position.  sensors 3 samples collected .

for example , structure looks follows

   sens1   sens2   sens3   sens4   sens5   sens6   sens7      sens8   output
samp1   1   1   0   1   1   1   1   0   2  ,4
samp2   0   0   0   1   1   1   0   0   3
samp3    0   1   0   1   1   0   1   1   2 ,2


so samp1 should return values of  2 & 4
samp 2 should return values of 3
samp3 should return value of 2  & 2

basically should count number of consecutive ones matrix of sensor , samples.  example codes (bit manipulation, storing of bit count in array) helpful.

have @ following code

code: [select]
byte sample1 = b11011110;
byte sample2 = b00011100;
byte sample3 = b01011011;
byte sample4 = b00000000;
byte sample5 = b11111111;


void scanbyte(byte sample)
{
  // looking 2 or more consecutive bits set 1
  bool patternstarted = false;
  byte patternlength = 0;
  byte nbpatterns = 0;

  // let's print bits. not using binary print version because not print starting 0. want see 8 bits
  serial.print ("b");
  (int currentbit = 7; currentbit >= 0; --currentbit) serial.print(bitread(sample, currentbit));
  serial.print ("--> ");

  // parse each bit , pattern of 2 or more consecutive 1
  (int currentbit = 7; currentbit >= 0; --currentbit) {

    // if have started pattern, check if can confirm , continue pattern
    if (patternstarted) {
      if (bitread(sample, currentbit) == 1) {
        // found 1, pattern confirmed, keep counting
        patternlength++;
        if (currentbit == 0) {
        // need deal case @ end of byte
          if (nbpatterns) serial.print(","); //we printed something, print comma
          nbpatterns++;
          serial.print(patternlength);
          patternlength = 0;
          patternstarted = false;
        }
      } else {
        // found zero, if patternlength more 1 had pattern
        if (patternlength > 1) {
          if (nbpatterns) serial.print(","); //we printed something, print comma
          nbpatterns++;
          serial.print(patternlength);
        }
        patternlength = 0;
        patternstarted = false;
      }
    }

    // have not yet identified possible pattern, check if start of one
    if ((!patternstarted) &&  (bitread(sample, currentbit) == 1)) {
      // found 1, that's possibly start of pattern
      patternstarted = true;
      patternlength = 1;
    }

  } // end each bit in sample

  serial.print(" matching = ");
  serial.println(nbpatterns);


}

void setup() {
  serial.begin(115200);
  serial.println("-------------");
  scanbyte(sample1);
  scanbyte(sample2);
  scanbyte(sample3);
  scanbyte(sample4);
  scanbyte(sample5);
}

void loop() {}

the output in console (set @ 115200 bauds) should be

-------------
b11011110--> 2,4 matching = 2
b00011100--> 3 matching = 1
b01011011--> 2,2 matching = 2
b00000000-->  matching = 0
b11111111--> 8 matching = 1



of course instead of printing store count , hits array.


Arduino Forum > Using Arduino > Programming Questions > Bit manipulation of sensor data


arduino

Comments

Popular posts from this blog

Error: ‘for’ loop initial declarations are only allowed in C99 or C11 mode - Raspberry Pi Forums

class MPU6050 has no member named begin

missing filename after '-o'