Pages: [1]   Go Down
Author Topic: Bit String to INT  (Read 494 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 22
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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;
}
« Last Edit: February 03, 2011, 03:35:43 pm by waterboy550 » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 617
Posts: 49463
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Pages: [1]   Go Up
Jump to: