Sunday 24 March 2013

FYP 2 Week 9

Alhamdulillah, now I finally found the code that works for my circuit. It works beautifully as I have expected. Here is the code:

#include <LiquidCrystal.h>

int pin = 0;                // analog pin
int tempc = 0,tempf=0;      // temperature variables
int samples[8];             // variables to make a better precision
int maxi = -100,mini = 100; // to start max/min temperature
int i;
int TIP122pin = 9;
int fanspeed = 0;
int fanStemp = 0;

LiquidCrystal lcd(12, 11, 5, 4, 3, 2); // Customize for your connections.

void setup()
{
  Serial.begin(9600);       // start serial communication
  lcd.begin(16,2);          // set up the LCD's number of columns and rows
  pinMode(TIP122pin, OUTPUT); // Set pin for output to control TIP122 Base pin
  analogWrite(TIP122pin, 255); // control the fan speed by changing values from 0 to 255
  delay(200);
}

void loop()
{
  
tempc = 0;

for(i = 0;i<=7;i++){         // gets 8 samples of temperature
  samples[i] = ( 5.0 * analogRead(pin) * 100.0 ) / 1024.0;
  tempc = tempc + samples[i];
  delay(1000);
}


tempc = tempc/8;               // better precision
tempf = (tempc * 9)/ 5 + 32;     // converts to fahrenheit
if(tempc > maxi) {maxi = tempc;} // set max temperature
if(tempc < mini) {mini = tempc;} // set min temperature

 if ( tempc > 24 && tempc < 37 ) {
    fanStemp = tempc + 50;
    fanspeed = fanStemp + 100;
    analogWrite(9,fanspeed);
    Serial.print("Fan started at temp+50+100 speed\n");
    Serial.print(fanspeed,DEC);
    Serial.print("\n");
    delay(100);
  }
    else if ( tempc <= 24 ) {
    fanspeed = 0;
    analogWrite(9,fanspeed);
    Serial.print("Fan off\n");
    delay(100);
   }
   else{
     analogWrite(9,255);
     Serial.print("Fan started at full speed\n");
     delay(100);
   }
   
Serial.print(tempc,DEC); // serial print for debugging all serial.print commands
Serial.print(" Celsius, ");
Serial.print(tempf,DEC);
Serial.print(" fahrenheit -> ");
Serial.print(maxi,DEC);
Serial.print(" Max, ");
Serial.print(mini,DEC);
Serial.println(" Min");

lcd.setCursor(0, 0);  // set the cursor to (0,0)
lcd.print(tempc,DEC);  // print temp in C and F on the first row of the lcd
lcd.print(" C, ");
lcd.print(tempf,DEC);
lcd.print(" F");

lcd.setCursor(0, 1);  // set the cursor to (16,1)
lcd.print(maxi,DEC);   // Print max temp and min temp on the second row of lcd
lcd.print("  Max, ");
lcd.print(mini,DEC);
lcd.println(" Min.");

delay(1000);                   // delay before loop
}

These are the pictures of the constructed circuit on the breadboard:
The circuit before being plugged with power supply.

The circuit with the power supply, you can see the fan is moving and the LCD display is showing the temperature.









3 comments:

  1. Sir can i have the complete circuit diagram and codes.. I need it badly for my project?

    ReplyDelete
  2. hi... bro boleh kasi complete circuit x..?

    ReplyDelete
  3. any one have circuit diagram??

    ReplyDelete