Go Down

Topic: Bit String to INT (Read 538 times) previous topic - next topic

waterboy550

Feb 03, 2011, 09:08 pm Last Edit: Feb 03, 2011, 09:35 pm by waterboy550 Reason: 1
I have the following code.

Serial.print(dht_dat[0], DEC);

This works get when printing dht_dat out via serial. The result is that the serial port prints out an interger. dht_dat is an array that stores 8bits in each value. I want to store this value as a int to use later in the program. I'm not having any success with this however. Any help would be great!

Here is the code to generate the dht_dat array. Sorry this isn't my code and I just started using it so I don't know that much about it.

//This is the call to make the array
for (i=0; i<5; i++)
   dht_dat = read_dht_dat();

//This is the read_dht_dat function that places the values into the array
byte read_dht_dat() {
   //Collect 8 bits from datastream, return them interpreted
   //as a byte. I.e. if 0000.0101 is sent, return decimal 5.
   //Code expects the system to have recently entered the
   //dataline low condition at the start of every data bit's
   //transmission BEFORE this function is called.
   byte i = 0;
   byte result=0;
   for (i=0; i< 8; i++) {
        //We enter this during the first start bit (low for 50uS) of the byte
        //Next: wait until pin goes high
        while(digitalRead(TEMP_RH_PIN)==LOW);
        delayMicroseconds(30);
        if (digitalRead(TEMP_RH_PIN)==HIGH)
        result |=(1<<(7-i));
        while (digitalRead(TEMP_RH_PIN)==HIGH);
   }
   return result;
}

PaulS

Quote
I want to store this value as a int to use later in the program. I'm not having any success with this however. Any help would be great!

Have you looked at the bitSet() and bitWrite() functions? Or the bitRead() counterpart?

Go Up