/* Temperature controlled extension cord - By Bruce Fast. As provided, the cord turns on at 5 units (about 2 degrees celsius, 35F ) and turns off at 8 (about 3 degrees celsius, 37F) Diagnostics: The on-board led blinks out the "temperature" in sensor units (about 2.5 per degree celsius)) The on-board led's blink is mostly on if the cord is "on", and mostly off if the cord is "off" */ // Which Arduino pins are active const int analogInPin = A2; // Analog input pin that the potentiometer is attached to const int relay = 1; // Output port for relay -- don't use 0, its wierd. const int led = 13; // Output for feedback. Its nice to know what the machine is thinking in a flash. // Temperature range const int TooCold = 5; // Formula: divide by 2.5 to get celsius const int TooHot = 8; // For internal use int sensorValue = 0; // value read from the temperature sensor bool RelayOn; // True if relay is currently on void setup() { // initialize serial communications at 9600 bps: pinMode( relay, OUTPUT ); pinMode(led, OUTPUT ); // Assure we are off at the start RelayOn = false; digitalWrite( relay,LOW ); } void loop() { int a1,a2; int OnFor, OffFor; // read the temperature sensorValue = analogRead(analogInPin); // CONTROL THE HEATER // If currently heating if( RelayOn ) { if( sensorValue > TooHot ) { // Turn relay off RelayOn = false; digitalWrite( relay,LOW ); } } else { // If currently not heating if( sensorValue < TooCold ) { // Turn relay on RelayOn = true; digitalWrite( relay, HIGH ); } } /// REPORT STATUS OUT OF THE LED /// LED mostly on = on, LED mostly off = off if( RelayOn ) { OnFor = 500; // 1/2 second OffFor = 100; // 1/10 second } else { OnFor = 100; // 1/10 second OffFor = 500; // 1/2 second } /// Do the blinking if( sensorValue < 100 && sensorValue > 0 ) { for( a1 = 1;a1 < sensorValue; a1++ ) { digitalWrite( led,HIGH ); delay( OnFor ); digitalWrite( led, LOW ); delay( OffFor ); } } else { // Sensor not working for( a1 = 1; a1 < 10; a1++) { digitalWrite( led,HIGH); delay( 100 ); digitalWrite( led, LOW ); delay( 100 ); } } // 2 seconds of quiet delay( 2000 ); }