Brighton, UK
Offline
Newbie
Karma: 0
Posts: 8
|
 |
« on: March 11, 2012, 06:46:20 pm » |
hi guys i`m trying to control the fan speed with a temperature sensor, display the temperature and fan speed on dedicated LCD. i have wired up every thing correctly. managed to display temp on LCD as well as serial monitor. managed to switch the fan on and off. but when it comes to control speed bit in programming i get stuck and the fan doesnt move.... here is my 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 = 3; int fanspeed = 0;
LiquidCrystal lcd(7, 8, 9, 10, 11, 12); // 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 }
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.0; // 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 < 40 && tempc > 10 ) { fanspeed = tempc + 50; analogWrite(3,fanspeed); } else if ( tempc < 10 ) { fanspeed = 0; analogWrite(3,fanspeed); } else{ analogWrite(3,255); }
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 } please help this is really wasting my time......... 
|
|
|
|
|
Logged
|
|
|
|
|
Gosport, UK
Offline
Faraday Member
Karma: 19
Posts: 3118
|
 |
« Reply #1 on: March 12, 2012, 04:17:49 am » |
You are Serial.printing the value of tempc. Do the results look right?
Add some debugging prints to the fan speed if statements to see which parts are being executed.
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 311
Posts: 35470
Seattle, WA USA
|
 |
« Reply #2 on: March 12, 2012, 04:49:41 am » |
Why are you storing the temperatures in an array, during the averaging process? The array is completely unnecessary. if ( tempc < 40 && tempc > 10 ) { This is hard to read. if(tempc > 10 && tempc < 40) { is much closer to how people decide to do things. The compiler doesn't care, but it's easier to maintain understandable code. What, exactly, is connected to pin 3? What kind of fan are you using?
|
|
|
|
|
Logged
|
|
|
|
|
London
Offline
Full Member
Karma: 0
Posts: 146
Ugi was not declared in this scope
|
 |
« Reply #3 on: March 12, 2012, 06:50:09 am » |
tempc = tempc/8.0; // better precision
You are doing floating point maths (using 8.0) on an integer variable. I haven't checked but I reckon this may be your issue.
Try defining tempc as a float & see if it works any better. Better might be:
tempc=tempc>>3;
or for closer rounding:
tempc=((tempc+4)>>3)
Also, why not put a delay at then end of the setup function just to check that the fan runs OK at full speed before moving into the loop. If it doesn't then it sounds like a hardware issue.
|
|
|
|
« Last Edit: March 12, 2012, 06:52:50 am by Dr_Ugi »
|
Logged
|
|
|
|
|
Brighton, UK
Offline
Newbie
Karma: 0
Posts: 8
|
 |
« Reply #4 on: March 12, 2012, 07:54:18 am » |
You are Serial.printing the value of tempc. Do the results look right?
Add some debugging prints to the fan speed if statements to see which parts are being executed.
yes temperature measurements are right, thanks you
|
|
|
|
|
Logged
|
|
|
|
|
Brighton, UK
Offline
Newbie
Karma: 0
Posts: 8
|
 |
« Reply #5 on: March 12, 2012, 07:58:26 am » |
Why are you storing the temperatures in an array, during the averaging process? The array is completely unnecessary. if ( tempc < 40 && tempc > 10 ) { This is hard to read. if(tempc > 10 && tempc < 40) { is much closer to how people decide to do things. The compiler doesn't care, but it's easier to maintain understandable code. What, exactly, is connected to pin 3? What kind of fan are you using? thank you the base of the TIP-122 darlington transistor with 1k resistor driving 12v fan.
|
|
|
|
|
Logged
|
|
|
|
|
Gosport, UK
Offline
Faraday Member
Karma: 19
Posts: 3118
|
 |
« Reply #6 on: March 12, 2012, 08:06:38 am » |
You are Serial.printing the value of tempc. Do the results look right?
Add some debugging prints to the fan speed if statements to see which parts are being executed.
yes temperature measurements are right, thanks you What about debugging prints to trace the path through the code?
|
|
|
|
|
Logged
|
|
|
|
|
Brighton, UK
Offline
Newbie
Karma: 0
Posts: 8
|
 |
« Reply #7 on: March 12, 2012, 08:08:35 am » |
tempc = tempc/8.0; // better precision
You are doing floating point maths (using 8.0) on an integer variable. I haven't checked but I reckon this may be your issue.
Try defining tempc as a float & see if it works any better. Better might be:
tempc=tempc>>3;
or for closer rounding:
tempc=((tempc+4)>>3)
Also, why not put a delay at then end of the setup function just to check that the fan runs OK at full speed before moving into the loop. If it doesn't then it sounds like a hardware issue.
thanks for the hint but temperature statements works fine and i have changed the value 8.0 to 8 this is just dividing the 8 samples by 8 its just to get better measurements.
|
|
|
|
|
Logged
|
|
|
|
|
Brighton, UK
Offline
Newbie
Karma: 0
Posts: 8
|
 |
« Reply #8 on: March 12, 2012, 08:12:35 am » |
What about debugging prints to trace the path through the code?
im about to do it now and see where the problem is because the fan is not moving at all but when i set it high it switch on so its just fan statements and the serial.print is the best hint i didnt even think of it.
|
|
|
|
|
Logged
|
|
|
|
|
Brighton, UK
Offline
Newbie
Karma: 0
Posts: 8
|
 |
« Reply #9 on: March 12, 2012, 09:29:07 am » |
i still can not get it here is the updated 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 = 3; int fanspeed = 0; int fanStemp = 0;
LiquidCrystal lcd(7, 8, 9, 10, 11, 12); // 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 > 10 && tempc < 40 ) { fanStemp = tempc + 50; fanspeed = fanStemp + 100; analogWrite(3,fanspeed); Serial.print("Fan started at temp+50+100 speed\n"); Serial.print(fanspeed,DEC); Serial.print("\n"); delay(100); } else if ( tempc < 10 ) { fanspeed = 0; analogWrite(3,fanspeed); Serial.print("Fan off\n"); delay(100); } else{ analogWrite(3,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 } i got the fan working and running but on 5v but i can not see the speed change am i using the right function [ analogWrite(3, fanspeed) ] why not [ digitalWrite(3,fanspeed) ] what is the difference i know the voltage may be one of the problems but still at least i should see a change when i change the added value of 100 in the fanspeed to 0 !!! probable the connection ?? iam connecting Arduino Pin 3 (PWM) with TIP-122 Base (B) pin through 1k resistor.5V fan connect to the TIP-122 Collector (C). And the Emitter (E) pin connect to the Ground. i dont understand the statements are executed the way i want it on the serial print but ????
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 311
Posts: 35470
Seattle, WA USA
|
 |
« Reply #10 on: March 12, 2012, 09:45:18 am » |
digitalWrite() turns the pin HIGH or LOW, one time. analogWrite() starts the pin toggling on and off, so that it's on amount is the ratio specified.
Ditch the complicated code. Write a sketch that calls analogWrite() with various values, from 0 to 200, with a 2 second delay between changes. If the fan speed does not change, the fan is not meant to be variable speed.
|
|
|
|
|
Logged
|
|
|
|
|
Brighton, UK
Offline
Newbie
Karma: 0
Posts: 8
|
 |
« Reply #11 on: March 12, 2012, 09:57:48 am » |
int fanstart = 0; // start fan speed at 0 int fadeAmount = 20; // how many points to fade the LED by
void setup() { // declare pin 3 to be an output: pinMode(3, OUTPUT); }
void loop() { // set the speed of pin 3: analogWrite(3, fanstart);
// change the speed for next time through the loop: fanstart = fanstart + fadeAmount;
// reverse the direction of the fading at the ends of the fade: if (fanstart == 0 || fanstart == 255) { fadeAmount = -fadeAmount ; } // wait for 30 milliseconds to see the dimming effect delay(30); } nothing happens the fan runs on one speed ! Connections are the same the connection ?? iam connecting Arduino Pin 3 (PWM) with TIP-122 Base (B) pin through 1k resistor.5V fan connect to the TIP-122 Collector (C). And the Emitter (E) pin connect to the Ground
|
|
|
|
|
Logged
|
|
|
|
|
New Jersey
Offline
Edison Member
Karma: 24
Posts: 2345
|
 |
« Reply #12 on: March 12, 2012, 10:13:31 am » |
This will never be true: fanstart == 255 You're incrementing in steps of 20. Write something much simpler - just hard code a few values and put a decent delay between them.
|
|
|
|
|
Logged
|
|
|
|
|
Brighton, UK
Offline
Newbie
Karma: 0
Posts: 8
|
 |
« Reply #13 on: March 12, 2012, 11:25:57 am » |
damn  it wasn't working properly because of the connections the problem solved thanks all for suggestions.
|
|
|
|
|
Logged
|
|
|
|
|
|