/* This sketch reads three sensors from the Grove Collection: A0: Temp and Humidity, SEN51035P A1: Light Sensor, SEN11302P A2: Rotary Sensor, COM22735P */ /* Base code for RHT03 stolen from https://codebender.cc/sketch:70199 */ #include double temp; double humi; unsigned int loopCnt; int chr[40] = {0}; // an array is created to store 40 elements unsigned long xtime; void setup() { Serial.begin(9600); // Serial port not used } //end setup void loop() { // ----------------- temp and humidity sensor------------------ // Give the "START SIGNAL" to the sensor: LOW + HIGH pulse of specific duration. delay(200);// Not less than 200 ms delay for 1 sensor! It can't read faster. (For 2 sensors, :2 !) pinMode(A0,OUTPUT); digitalWrite(A0,LOW);// send LOW pulse of 20 millis! delay(20); digitalWrite(A0,HIGH);// send HIGH pulse of 40 micros! delayMicroseconds(40); digitalWrite(A0,LOW);// Go back to LOW and wait for the sensor. pinMode(A0,INPUT);// Make the signal pin an input and listen to the sensor: loopCnt=10000; while(digitalRead(A0) != HIGH)// NOT HIGH: While waiting for the sensor to answer... { if(loopCnt-- == 0) { Serial.println("NO Signal!"); }// end if }// end while loopCnt=30000; while(digitalRead(A0) != LOW) { if(loopCnt-- == 0) { //Serial.println("OK sensor sends data!"); //Serial.println(" "); }// end if } // Now receiving data from sensor: for(int i=0;i<40;i++)// Go through 40 steps: Increment "i" with 1 each time and store the databits in the array "chr". { while(digitalRead(A0) == LOW) // LOW pulses are not interesting... { // do nothing! }// end while xtime = micros(); // start the timer! while(digitalRead(A0) == HIGH) // Now look at the HIGH pulses: LONG = 1, SHORT = 0! { }// end while if (micros() - xtime >50) // If a long HIGH pulse (>50 microseconds) is seen... { chr[i]=1; // store a "1" in the array "chr" }// end if else // If a short HIGH pulse (<50 microseconds) is seen... { chr[i]=0; // store a "0" in the array "chr" }// end else }// end for // Now convert 3 bytes (The 1st, 3rd and 5th) in the array "chr" into (decimal) integers: The sensor output values... humi=chr[6]*512+chr[7]*256+chr[8]*128+chr[9]*64+chr[10]*32+chr[11]*16+chr[12]*8+chr[13]*4+chr[14]*2+chr[15]; // Second byte received: This is the humidity (%) *10 (You can leave the first 5 bits out: The number will never become as big...) temp=chr[22]*512+chr[23]*256+chr[24]*128+chr[25]*64+chr[26]*32+chr[27]*16+chr[28]*8+chr[29]*4+chr[30]*2+chr[31]; // Second byte received: This is the temperature (°C) *10 (You can leave the first 5 bits out: The number will never become as big...The highest bit is used as + or - sign) humi=humi/10; // Divide by 10 to obtain the humidity (%) if (chr[16]==1) // If highest temp bit = 1 => minus temperature! { temp=temp/-10; // Divide by -10 to change to minus }// end if else // If highest temp bit = 0 => plus temperature... { temp=temp/10; // Divide by 10 to obtain the temperature (°C) }// end else // convert to F temp = (temp * 1.8) + 32.0; int itemp = (int) (temp + 0.5); int ihumi = (int) (humi + 0.5); // ----------------- light sensor int lightRaw = analogRead(1); float light=(float)lightRaw/1023.0; // ----------------- rotary sensor int rotVal = analogRead(2); // map it to the range of the analog out: int rot = map(rotVal, 0, 1023, 0, 255); rot = rot * 39; if(rot<1000) { rot = 1000; } Serial.print(itemp); Serial.print("F, "); Serial.print(ihumi); Serial.print("%, "); Serial.print(light); Serial.print("L, "); Serial.print(rot); Serial.println("R"); delay(rot); }// end loop