|
| 1 | +/* |
| 2 | +This sketch reads three sensors from the Grove Collection: |
| 3 | + |
| 4 | +A0: Temp and Humidity, SEN51035P |
| 5 | +A1: Light Sensor, SEN11302P |
| 6 | +A2: Rotary Sensor, COM22735P |
| 7 | + |
| 8 | +*/ |
| 9 | + |
| 10 | +/* |
| 11 | +Base code for RHT03 stolen from https://codebender.cc/sketch:70199 |
| 12 | +*/ |
| 13 | +#include <math.h> |
| 14 | + |
| 15 | +double temp; |
| 16 | +double humi; |
| 17 | + |
| 18 | +unsigned int loopCnt; |
| 19 | + |
| 20 | +int chr[40] = {0}; // an array is created to store 40 elements |
| 21 | +unsigned long xtime; |
| 22 | + |
| 23 | +void setup() |
| 24 | +{ |
| 25 | + Serial.begin(9600); // Serial port not used |
| 26 | +} //end setup |
| 27 | + |
| 28 | +void loop() |
| 29 | +{ |
| 30 | + // ----------------- temp and humidity sensor------------------ |
| 31 | + // Give the "START SIGNAL" to the sensor: LOW + HIGH pulse of specific duration. |
| 32 | + delay(200);// Not less than 200 ms delay for 1 sensor! It can't read faster. (For 2 sensors, :2 !) |
| 33 | + pinMode(A0,OUTPUT); |
| 34 | + digitalWrite(A0,LOW);// send LOW pulse of 20 millis! |
| 35 | + delay(20); |
| 36 | + |
| 37 | + digitalWrite(A0,HIGH);// send HIGH pulse of 40 micros! |
| 38 | + delayMicroseconds(40); |
| 39 | + digitalWrite(A0,LOW);// Go back to LOW and wait for the sensor. |
| 40 | + |
| 41 | + pinMode(A0,INPUT);// Make the signal pin an input and listen to the sensor: |
| 42 | + |
| 43 | + loopCnt=10000; |
| 44 | + |
| 45 | + while(digitalRead(A0) != HIGH)// NOT HIGH: While waiting for the sensor to answer... |
| 46 | + { |
| 47 | + |
| 48 | + if(loopCnt-- == 0) |
| 49 | + { |
| 50 | + Serial.println("NO Signal!"); |
| 51 | + }// end if |
| 52 | + |
| 53 | + }// end while |
| 54 | + |
| 55 | + loopCnt=30000; |
| 56 | + |
| 57 | + while(digitalRead(A0) != LOW) |
| 58 | + { |
| 59 | + |
| 60 | + if(loopCnt-- == 0) |
| 61 | + { |
| 62 | + //Serial.println("OK sensor sends data!"); |
| 63 | + //Serial.println(" "); |
| 64 | + }// end if |
| 65 | + |
| 66 | + } |
| 67 | + |
| 68 | + // Now receiving data from sensor: |
| 69 | + |
| 70 | + 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". |
| 71 | + { |
| 72 | + |
| 73 | + while(digitalRead(A0) == LOW) // LOW pulses are not interesting... |
| 74 | + { |
| 75 | + // do nothing! |
| 76 | + }// end while |
| 77 | + |
| 78 | + xtime = micros(); // start the timer! |
| 79 | + |
| 80 | + while(digitalRead(A0) == HIGH) // Now look at the HIGH pulses: LONG = 1, SHORT = 0! |
| 81 | + { |
| 82 | + |
| 83 | + }// end while |
| 84 | + |
| 85 | + if (micros() - xtime >50) // If a long HIGH pulse (>50 microseconds) is seen... |
| 86 | + { |
| 87 | + chr[i]=1; // store a "1" in the array "chr" |
| 88 | + }// end if |
| 89 | + |
| 90 | + else // If a short HIGH pulse (<50 microseconds) is seen... |
| 91 | + { |
| 92 | + chr[i]=0; // store a "0" in the array "chr" |
| 93 | + }// end else |
| 94 | + |
| 95 | + }// end for |
| 96 | + |
| 97 | +// Now convert 3 bytes (The 1st, 3rd and 5th) in the array "chr" into (decimal) integers: The sensor output values... |
| 98 | + |
| 99 | + 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...) |
| 100 | + 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) |
| 101 | + |
| 102 | + humi=humi/10; // Divide by 10 to obtain the humidity (%) |
| 103 | + |
| 104 | + if (chr[16]==1) // If highest temp bit = 1 => minus temperature! |
| 105 | + { |
| 106 | + temp=temp/-10; // Divide by -10 to change to minus |
| 107 | + }// end if |
| 108 | + |
| 109 | + else // If highest temp bit = 0 => plus temperature... |
| 110 | + { |
| 111 | + temp=temp/10; // Divide by 10 to obtain the temperature (°C) |
| 112 | + }// end else |
| 113 | + |
| 114 | + // convert to F |
| 115 | + temp = (temp * 1.8) + 32.0; |
| 116 | + |
| 117 | + int itemp = (int) (temp + 0.5); |
| 118 | + int ihumi = (int) (humi + 0.5); |
| 119 | + |
| 120 | + // ----------------- light sensor |
| 121 | + int lightRaw = analogRead(1); |
| 122 | + float light=(float)lightRaw/1023.0; |
| 123 | + |
| 124 | + // ----------------- rotary sensor |
| 125 | + int rotVal = analogRead(2); |
| 126 | + // map it to the range of the analog out: |
| 127 | + int rot = map(rotVal, 0, 1023, 0, 255); |
| 128 | + rot = rot * 39; |
| 129 | + if(rot<1000) { rot = 1000; } |
| 130 | + |
| 131 | + Serial.print(itemp); |
| 132 | + Serial.print("F, "); |
| 133 | + Serial.print(ihumi); |
| 134 | + Serial.print("%, "); |
| 135 | + Serial.print(light); |
| 136 | + Serial.print("L, "); |
| 137 | + Serial.print(rot); |
| 138 | + Serial.println("R"); |
| 139 | + |
| 140 | + delay(rot); |
| 141 | +}// end loop |
0 commit comments