// Week 12 - Input Devices // Author: Kati Pitkänen, April 30, 2018 // Temperature calculation made by Ari Vuokila #include int LED=PB2 // Define the threshold value int TEMPERATURE=PB3; // Define LED in the pin PB2 as an OUTPUT int threshold=22; // Define the threshold value void setup() { pinMode(LED, OUTPUT); pinMode(TEMPERATURE, INPUT); digitalWrite(LED,HIGH); } void loop() { // Main code to run repeatedly: float reading; // Define the variable to store the sensor value data reading = analogRead(TEMPERATURE); // Read and store the analog input value coming from the temp sensor pin double Temp, T; // Define the variables to show the temperature data double R = 10000 / (1023.0 / readng - 1); // Steinhart - Hart /// Resistance = 10 k ohm double loka = 10000 / R; // loka = R0/R // 10000/R = ReferenceResistance/ NominalResistance = ResistanceInNominalTemperature25oC) T = (0.0033540164 + 0.00026666666 * log(L)); // Steinhart-Hart 1/T=1/T0 + 1/B*ln(R0/R) T = 1/T; Temp = T - 273.15; // To get degrees Celcius Temperature = T - 273.15 (degrees Kelvin) if(Temp>threshold){ // If the current temperature is higher than the threshold value digitalWrite(LED,LOW); // Turn ON the LED (because of pull-up resistor the value is LOW=0) } else{ digitalWrite(LED,HIGH); // Else, turn OFF the LED (because of pull-up resistor the value is HIGH=1) } delay(1000); // Delay time before starting the loop again }