Wednesday, December 3, 2014

The Coding

This code was downloaded from the Sparkfun website on the buldr.com extension showing how to code for a one wire temperature sensor. The second part of the code was taken from an arduino forum that uses and input and output function to relate the temperature sensor to the LED. The group was stuck on combining these two codes for the longest time due to some small errors and bugs but we finally worked it out to give the final product. Here is a copy of the code below:

int tempIn = 3;       // temperature sensor on Analogue Pin 3
int ledOut = 7;        // LED on Digital Pin 7
int aiValue = 0;       // input value (0-1023 = 0 to 5v)
float setPoint = 71;    // Trigger value in 'C
float deadband = 0.5;   // Differential for reset value in 'C

/*
18.
NOTE: the deadband action can be disabled by setting the deadband to 0
19.
*/
float degC;            // The temperature in Degrees Centigrade
void setup()
{

pinMode(ledOut, OUTPUT);    // Configure the Digital Pin Direction for the LED 

Serial.begin(9600);
}
void loop()
{

degC = getTemperature(getVolts());      //Get the Volts and the Temperature using Helper functions.
if (degC > setPoint)
{
digitalWrite(ledOut, HIGH);      // Temperature Limit Reached, turn the LED on.
Serial.println(" | Output: ON.");

}
else
{

if (degC < (setPoint - deadband))
{
digitalWrite(ledOut, LOW);       // Temperature dropped below SP and deadband, turn the LED off.
Serial.println(" | Output: OFF.");
}
}   
delay(1000);

}

float getVolts()
{

int inputValue;

inputValue = analogRead(tempIn);

float volts;

volts = (((float)inputValue / 1024) * 5);

Serial.print("Input Value: ") ; Serial.print(inputValue);
Serial.print(" | Voltage: ") ; Serial.print(volts);

return volts;

}

float getTemperature(float volts)

{

float temp = (volts - 0.5) / 0.01 ;

Serial.print(" | Temperature: "); Serial.print(temp); Serial.print(" 'C");

return temp;
}

No comments:

Post a Comment