Hello all,
this is my first post here, but i have been working on Arduino for quite some time now.
For instance i got some cars to work with servo’s and ultrasonic sensors, calibrated the speed of the wheels using infrared sensors, eventually also controlled by means of a remote control.
This time I am trying to read a DHT22 sensor without the use of a library.
Yeah yeah i know… but i still want to be able to achieve this and understand the way these things work and be able to program the code for that.
I have succeeded in initializing the sensor and reading the 40 bits into 5 arrays, each holding 8 positions to represent the bits.
Now i am having trouble putting together the first two of the arrays and convert that to the humidity value.
From the datasheet i understand that the first sixteen bits sent by the sensor are the humidity value times 10.
So to get the principle working i created a small separate program to just get that to work. Right now i have an array containing 16 values (1 or 0). I want to convert this to an integer, by shifting the bits into the integer which should work because that would be a 16 bit value.
But i am getting the strange result of 16 one’s in front of my actual values when showing it on the serial monitor.
int TwoBytes=0x0000;
int Byte1[]={1,0,1,0,1,1,0,0,1,1,1,0,1,1,1,1};
void setup()
{
Serial.begin(9600);
/* add setup code here */
Serial.println(TwoBytes,BIN);
for (int i=0;i<16;i++)
{
Serial.print(Byte1[i],BIN);
}
Serial.println();
for (int i=0;i<16;i++)
{
TwoBytes += (Byte1[i]<<(15-i));
Serial.print("Step:");
Serial.print(i);
Serial.print(" ");
Serial.println(TwoBytes,BIN);
}
}
void loop()
{
/* add main program code here */
}
I know that the first loop works since it correctly shows the contents of the array, and this is what i would expect to show up in step 15 (see below).
The second loop gives the result described above and shown below:
Opening port
Port open
0
1010110011101111
Step:0 11111111111111111000000000000000
Step:1 11111111111111111000000000000000
Step:2 11111111111111111010000000000000
Step:3 11111111111111111010000000000000
Step:4 11111111111111111010100000000000
Step:5 11111111111111111010110000000000
Step:6 11111111111111111010110000000000
Step:7 11111111111111111010110000000000
Step:8 11111111111111111010110010000000
Step:9 11111111111111111010110011000000
Step:10 11111111111111111010110011100000
Step:11 11111111111111111010110011100000
Step:12 11111111111111111010110011101000
Step:13 11111111111111111010110011101100
Step:14 11111111111111111010110011101110
Step:15 11111111111111111010110011101111
After changing the count (…;i<14;… and …<<(14-i)…)in the second “for loop” the result is this:
Port open
0
1010110011101111
Step:0 100000000000000
Step:1 100000000000000
Step:2 101000000000000
Step:3 101000000000000
Step:4 101010000000000
Step:5 101011000000000
Step:6 101011000000000
Step:7 101011000000000
Step:8 101011001000000
Step:9 101011001100000
Step:10 101011001110000
Step:11 101011001110000
Step:12 101011001110100
Step:13 101011001110110
Step:14 101011001110111
But now i am missing the final bit.
Anyone know how to help me on this?
I have been at it for quite some time now and come to the conclusion that i am stuck.
Thanks in advance, any help is appreciated!