SENSORE
Esercizio 1
In questo esercizio il sensore rileva la temperatura. I diodi LED si accendono in base alla temperatura percepita dal sensore, piu la temperatura è alta più LED vengono accesi, partendo dal primo fino al decimo. Quando la temperatura si abbassa pure i LED si spengono proporzionalmente.
Sketch dell'esercizio
// Esercizio 1 - SENSORE #include <OneWire.h> #define temperatura 25 OneWire ds(16); // on pin 16 int i; void setup() { for(i=4;i<14;i++){ pinMode(i,OUTPUT); digitalWrite(i,HIGH); } Serial.begin(9600); } void loop() { byte i; byte present = 0; byte type_s; byte data[12]; byte addr[8]; float celsius, fahrenheit; if ( !ds.search(addr)) { ds.reset_search(); delay(250); return; } ds.reset(); ds.select(addr); ds.write(0x44,1); // start conversion, with parasite power on at the end delay(300); // maybe 750ms is enough present = ds.reset(); ds.select(addr); ds.write(0xBE); // Read Scratchpad for ( i = 0; i < 9; i++) { // we need 9 bytes data[i] = ds.read(); } // convert the data to actual temperature unsigned int raw = (data[1] << 8) | data[0]; if (type_s) { raw = raw << 3; // 9 bit resolution default if (data[7] == 0x10) { // count remain gives full 12 bit resolution raw = (raw & 0xFFF0) + 12 - data[6]; } } else { byte cfg = (data[4] & 0x60); if (cfg == 0x00) raw = raw << 3; // 9 bit resolution, 93.75 ms else if (cfg == 0x20) raw = raw << 2; // 10 bit res, 187.5 ms else if (cfg == 0x40) raw = raw << 1; // 11 bit res, 375 ms // default is 12 bit resolution, 750 ms conversion time } celsius = (float)raw / 16.0; //fahrenheit = celsius * 1.8 + 32.0; Serial.print(" Temperature = "); Serial.print(celsius); Serial.println(" Celsius, "); //Serial.print(fahrenheit); //Serial.println(" Fahrenheit"); //1 if (celsius < temperatura){ digitalWrite(4,HIGH); for(i=5;i<14;i++){ digitalWrite(i,HIGH); } } //2 if (celsius >= temperatura && celsius < (temperatura + 1)){ digitalWrite(4,LOW); digitalWrite(5,LOW); for(i=6;i<14;i++){ digitalWrite(i,HIGH); } } //3 if (celsius >= (temperatura + 1) && celsius < (temperatura + 2)){ for(i=4;i<7;i++){ digitalWrite(i,LOW); } for(i=7;i<14;i++){ digitalWrite(i,HIGH); } } if (celsius >=(temperatura + 2) && celsius < (temperatura + 3)){ for(i=4;i<8;i++){ digitalWrite(i,LOW); } for(i=8;i<14;i++){ digitalWrite(i,HIGH); } } if (celsius >= (temperatura + 3) && celsius < (temperatura + 4)){ for(i=4;i<9;i++){ digitalWrite(i,LOW); } for(i=9;i<14;i++){ digitalWrite(i,HIGH); } } if (celsius>= (temperatura + 5) && celsius < (temperatura + 6)){ for(i=4;i<10;i++){ digitalWrite(i,LOW); } for(i=10;i<14;i++){ digitalWrite(i,HIGH); } } if (celsius >= (temperatura + 6) && celsius < (temperatura + 7)){ for(i=4;i<11;i++){ digitalWrite(i,LOW); } for(i=11;i<14;i++){ digitalWrite(i,HIGH); } } if (celsius >= (temperatura + 7) && celsius < (temperatura + 8)){ for(i=4;i<12;i++){ digitalWrite(i,LOW); } for(i=12;i<14;i++){ digitalWrite(i,HIGH); } } if (celsius>=(temperatura + 8) && celsius < (temperatura + 9)){ for(i=4;i<13;i++){ digitalWrite(i,LOW); } digitalWrite(13,HIGH); } if (celsius >= (temperatura + 9)){ for(i=4;i<14;i++){ digitalWrite(i,LOW); } } }