-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsensori.txt
202 lines (157 loc) · 3.81 KB
/
sensori.txt
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
SENSORE OSTACOLO
uscita dal pin 2 (quello centrale)
quando passi la mano vicino, il gpio vale 0
SENSORE VIBRAZIONE
uscita dal pin S (quello in cui c'è scritto S)
quando si scuote (MOLTO), il gpio vale 0
SENSORE DI DISTANZA
uscita dal pin out (EN non serve!)
quando si avvicina la mano, il gpio vale 0. Le resistenze variabili cambiano la sensibilità
SENSORE DI LUMINOSITÀ (quello con led nero)
uscita dal pin DO
quando si tocca il led, il gpio vale 0. La resistenza cambia sensibilità (senso orario aumenta)
SENSORE TEMPERATURA
uscita dal pin S
serve questo programma:
void loop(void) {
byte i;
byte present = 0;
byte data[12];
byte addr[8];
int Temp;
if ( !ds.search(addr)) {
//Serial.print("No more addresses.n");
ds.reset_search();
return;
}
Serial.print("R="); //R=28 Not sure what this is
for( i = 0; i < 8; i++) {
Serial.print(addr[i], HEX);
Serial.print(" ");
}
if ( OneWire::crc8( addr, 7) != addr[7]) {
Serial.print("CRC is not valid!n");
return;
}
if ( addr[0] != 0x28) {
Serial.print("Device is not a DS18S20 family device.n");
return;
}
ds.reset();
ds.select(addr);
ds.write(0x44,1);
delay(1000);
// start conversion, with parasite power on at the end
// maybe 750ms is enough, maybe not
// we might do a ds.depower() here, but the reset will take care of it.
present = ds.reset();
ds.select(addr);
ds.write(0xBE);
// Read Scratchpad
Serial.print("P=");
Serial.print(present,HEX);
Serial.print(" ");
for ( i = 0; i < 9; i++) {
// we need 9 bytes
data[i] = ds.read();
Serial.print(data[i], HEX);
Serial.print(" ");
}
Temp=(data[1]<<8)+data[0];//take the two bytes from the response relating to temperature
Temp=Temp>>4;//divide by 16 to get pure celcius readout
//next line is Fahrenheit conversion
//Temp=Temp*1.8+32; // comment this line out to get celcius
Serial.print("T=");//output the temperature to serial port
Serial.print(Temp);
Serial.print(" ");
Serial.print(" CRC=");
Serial.print( OneWire::crc8( data, 8), HEX);
Serial.println();
}
SENSORE TEMPERATURA & UMIDITÀ
byte bGlobalErr;
byte dht_dat[5];
void setup(){
InitDHT();
Serial.begin(9600);
delay(300);
Serial.println("Humidity and temperaturenn");
delay(700);
}
void loop(){
ReadDHT();
switch (bGlobalErr){
case 0:
Serial.print("Current humdity = ");
Serial.print(dht_dat[0], DEC);
Serial.print(".");
Serial.print(dht_dat[1], DEC);
Serial.print("% ");
Serial.print("temperature = ");
Serial.print(dht_dat[2], DEC);
Serial.print(".");
Serial.print(dht_dat[3], DEC);
Serial.println("C ");
break;
case 1:
Serial.println("Error 1: DHT start condition 1 not met.");
break;
case 2:
Serial.println("Error 2: DHT start condition 2 not met.");
break;
case 3:
Serial.println("Error 3: DHT checksum error.");
break;
default:
Serial.println("Error: Unrecognized code encountered.");
break;
}
delay(800);
}
void InitDHT(){
pinMode(dht_dpin,OUTPUT);
digitalWrite(dht_dpin,HIGH);
}
void ReadDHT(){
bGlobalErr=0;
byte dht_in;
byte i;
digitalWrite(dht_dpin,LOW);
delay(20);
digitalWrite(dht_dpin,HIGH);
delayMicroseconds(40);
pinMode(dht_dpin,INPUT);
//delayMicroseconds(40);
dht_in=digitalRead(dht_dpin);
if(dht_in){
bGlobalErr=1;
return;
}
delayMicroseconds(80);
dht_in=digitalRead(dht_dpin);
if(!dht_in){
bGlobalErr=2;
return;
}
delayMicroseconds(80);
for (i=0; i<5; i++)
dht_dat[i] = read_dht_dat();
pinMode(dht_dpin,OUTPUT);
digitalWrite(dht_dpin,HIGH);
byte dht_check_sum =
dht_dat[0]+dht_dat[1]+dht_dat[2]+dht_dat[3];
if(dht_dat[4]!= dht_check_sum)
{bGlobalErr=3;}
};
byte read_dht_dat(){
byte i = 0;
byte result=0;
for(i=0; i< 8; i++){
while(digitalRead(dht_dpin)==LOW);
delayMicroseconds(30);
if (digitalRead(dht_dpin)==HIGH)
result |=(1<<(7-i));
while (digitalRead(dht_dpin)==HIGH);
}
return result;
}