-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathArduinoSensorFun
More file actions
141 lines (103 loc) · 3.82 KB
/
ArduinoSensorFun
File metadata and controls
141 lines (103 loc) · 3.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*
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 <math.h>
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