// After running the simulator, click on the DS18B20 chip to change the temperature
// Chip by bonnyr, source code: https://github.com/bonnyr/wokwi-ds1820-custom-chip/
#include <OneWire.h>
#include <DallasTemperature.h>
#define speed1 12
#define speed2 11
#define speed3 10
#define speed4 9
OneWire oneWire(A0);
DallasTemperature sensor(&oneWire);
void setup(void) {
pinMode(speed1,OUTPUT);
pinMode(speed2,OUTPUT);
pinMode(speed3,OUTPUT);
pinMode(speed4,OUTPUT);
Serial.begin(115200);
delay(2);
sensor.begin();
delay(20);
}
void loop(void) {
sensor.requestTemperatures();
Serial.print("Temperature is: ");
delay(10);
float temp = sensor.getTempCByIndex(0);
Serial.println(temp);
if(temp<30)fanOFF();
else if(temp< 35)fanSpeed1();
else if(temp<40)fanSpeed2();
else if(temp<45)fanSpeed3();
else fanSpeed4();
delay(1000);
}
void fanOFF(){
digitalWrite(speed1,LOW);
digitalWrite(speed2,LOW);
digitalWrite(speed3,LOW);
digitalWrite(speed4,LOW);
}
void fanSpeed1(){
fanOFF();
digitalWrite(speed1,HIGH);
}
void fanSpeed2(){
fanOFF();
digitalWrite(speed2,HIGH);
}
void fanSpeed3(){
fanOFF();
digitalWrite(speed3,HIGH);
}
void fanSpeed4(){
fanOFF();
digitalWrite(speed4,HIGH);
}