Components and supplies
Standard LCD - 16x2 White on Blue
Arduino Nano R3
Rotary Encoder with Push-Button
Apps and platforms
Arduino IDE
Project description
Code
PC_Chasis_Fan.ino
arduino
PC Chassis Fans
1#include <Adafruit_PWMServoDriver.h> 2#include <Wire.h> 3#include <LiquidCrystal_I2C.h> 4#include <EEPROM.h> 5//#include "writeAnything.h" 6 7/* 8-VCC-5V 9 | 10 NTC - Vout 11 | 12 R(fixed) 13 | 14 GND 15 ----Scaling Formula---- 16Reference Point ADC Reading 17 =========== ========== 18 x1(min) a1(min) 19 x2(max) a2(max) 20 21 X - x1 A - a1 22--------- = ----------- 23x2 - x1 a2 - a1 24 25==> X = (x2 - x1)*(A - a1)/(a2 - a1) + x1 26 27temp = ((110 - 0)*(average_sensor - 498))/191; 28*/ 29Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver(); 30#define ledPin 13 31LiquidCrystal_I2C lcd(0x3C,16,2); //sometimes the adress is not 0x3f. Change to 0x27 if it dosn't work.//0x3C is that A0 and A1 are shorted 32 33short ThermistorPin = 0;// Analog input pin for thermistor voltage 34short Temp_Pot = 1; //Temperature Potentiometer PIN 35 36boolean f_encoder_a; 37boolean f_encoder_b; 38boolean r_encoder_a; 39boolean r_encoder_b; 40boolean f_encoder_sw; 41boolean r_encoder_sw; 42uint8_t f_pwmnum= 0; 43uint8_t r_pwmnum= 1; 44 45boolean switch_temp; 46int Vo;// Integer value of voltage reading 47float R = 9870.0;// Fixed resistance in the voltage divider 48float logRt,Rt,T; 49float c1 = 2.108508173e-03, c2 = 0.7979204727e-04, c3 = 6.535076315e-07;//Water Sersor R=10kOhm-25 degree Celcius. B=3435 50int pot_raw; 51float pot_map; 52 53boolean f_encoder_a_prev; 54boolean r_encoder_a_prev; 55boolean f_encoder_sw_prev; 56boolean r_encoder_sw_prev; 57boolean f_fan_raw; 58boolean r_fan_raw; 59boolean trig_a; 60boolean trig_b; 61 62unsigned int f_counter; 63unsigned int r_counter; 64unsigned int f_counter_temp; 65unsigned int r_counter_temp; 66unsigned int amount[3]; 67unsigned short f_rpm_amount; 68unsigned short r_rpm_amount; 69unsigned short f_encoder_sw_counter; 70unsigned short r_encoder_sw_counter; 71unsigned short f_counter_th; 72unsigned short f_counter_hu; 73unsigned short f_counter_te; 74unsigned short f_counter_de; 75unsigned short f_counter_12; 76unsigned short f_counter_34; 77int f_c; 78int r_c; 79//RPMreading// 80int f_frequency; 81int r_frequency; 82int f_freq_counter; 83int r_freq_counter; 84 85unsigned long currentMicros; 86unsigned long previousMicros = 0; 87const long interval = 1000000; 88void setup() { 89 pwm.begin(); 90 pwm.setPWMFreq(1600); // This is the maximum PWM frequency 91 Wire.setClock(300000); //I2C clock frequency 300kHz 92 Serial.begin(9600); 93 f_counter = EEPROM_int_read(0); 94 r_counter = EEPROM_int_read2(10); 95 lcd.init(); // Initializing LCD 96 lcd.begin(16, 2); 97 lcd.backlight(); //lcd backlight o 98 99 100 pinMode(2, INPUT); 101 pinMode(3, INPUT); 102 pinMode(4, INPUT); 103 pinMode(5, INPUT); 104 pinMode(6, INPUT); 105 pinMode(7, INPUT); //Front Fan RPM input 106 pinMode(8, INPUT); //Rear Fan RPM input 107 pinMode(11, INPUT); //Front Encoder Switch 108 pinMode(12, INPUT); //Rear Encoder Switch 109 amount[0] = 1; 110 amount[1] = 10; 111 amount[2] = 100; 112 113} 114 115void loop() { 116 temp_reading(); 117 rpm_reading(); 118 switch_temp = digitalRead(2); 119 f_encoder_a = digitalRead(3); 120 f_encoder_b = digitalRead(4); 121 r_encoder_a = digitalRead(5); 122 r_encoder_b = digitalRead(6); 123 f_encoder_sw = digitalRead(11); 124 r_encoder_sw = digitalRead(12); 125 encoder_switch(); 126 encoder_reading(); 127 pwm_out(); 128 /* 129 Serial.print(f_counter); 130 Serial.print(" "); 131 Serial.print(f_frequency_out); 132 Serial.println(); 133 */ 134} 135 136void temp_reading() 137{ 138 Vo = analogRead(ThermistorPin); 139 Rt = R*( (1023.0 / (float)Vo) - 1.0 ); 140 logRt = log(Rt); 141 T = ( 1.0 / (c1 + c2*logRt + c3*logRt*logRt*logRt ) ) - 273.15; 142 143 pot_raw = analogRead(Temp_Pot); 144 pot_map = ((40.0 - 20.0)*(float(pot_raw) - 0.0))/(1023.0 - 0.0)+20.0; 145 146 if(f_counter_temp<4096) 147 { 148 f_counter_temp = sqrt((T-pot_map))*550+500; 149 r_counter_temp = sqrt((T-pot_map))*550+250;//Rear RPM less than Front 150 } 151} //end temp_reading 152/***************************************************************************************/ 153void rpm_reading() 154{ 155 //FRONT FAN RPM// 156 f_fan_raw = digitalRead(7); 157 if(f_fan_raw==false&&trig_a==false) 158 { 159 f_freq_counter++; 160 trig_a = true; 161 } 162 if(f_fan_raw==true&&trig_a==true) 163 { 164 trig_a = false; 165 } 166 //REAR FAN RPM// 167 r_fan_raw = digitalRead(8); 168 if(r_fan_raw==false&&trig_b==false) 169 { 170 r_freq_counter++; 171 trig_b = true; 172 } 173 if(r_fan_raw==true&&trig_b==true) 174 { 175 trig_b = false; 176 } 177 currentMicros = micros(); 178 if(currentMicros<previousMicros){previousMicros = currentMicros;} 179 if(currentMicros-previousMicros>=interval) 180{ 181 f_frequency = f_freq_counter * 60; 182 f_frequency=f_frequency/2; 183 r_frequency = r_freq_counter * 60; 184 r_frequency=r_frequency/2; 185 186 Serial.print(f_counter); 187 Serial.print(" "); 188 Serial.print(f_frequency); 189 Serial.println(); 190 lcd.clear(); 191 lcd.setCursor(0, 0); 192 lcd.print(f_counter); 193 lcd.setCursor(0, 1); 194 lcd.print(f_frequency); 195 196 lcd.setCursor(5, 0); 197 lcd.print(r_counter); 198 lcd.setCursor(5, 1); 199 lcd.print(r_frequency); 200 lcd.setCursor(10, 0); 201 lcd.print(T); 202 lcd.setCursor(15, 0); 203 lcd.print("C"); 204 lcd.setCursor(10, 1); 205 lcd.print("S:"); 206 lcd.setCursor(12, 1); 207 lcd.print(pot_map); 208 f_freq_counter = 0; 209 r_freq_counter = 0; 210 previousMicros = currentMicros; 211} 212}//end rpm_reading 213void encoder_switch() 214{ 215 //FRONT// 216 if(f_encoder_sw==false){f_encoder_sw_prev = f_encoder_sw;} 217 if(f_encoder_sw==true&&f_encoder_sw_prev==false) 218 { 219 f_encoder_sw_counter++; 220 if(f_encoder_sw_counter>2){f_encoder_sw_counter = 0;} 221 f_encoder_sw_prev = f_encoder_sw; 222 } 223 switch (f_encoder_sw_counter) 224 { 225 case 0: 226 f_rpm_amount = amount[0]; 227 break; 228 case 1: 229 f_rpm_amount = amount[1]; 230 break; 231 case 2: 232 f_rpm_amount = amount[2]; 233 break; 234 } 235 //REAR// 236 if(r_encoder_sw==false){r_encoder_sw_prev = r_encoder_sw;} 237 if(r_encoder_sw==true&&r_encoder_sw_prev==false) 238 { 239 r_encoder_sw_counter++; 240 if(r_encoder_sw_counter>2){r_encoder_sw_counter = 0;} 241 r_encoder_sw_prev = r_encoder_sw; 242 } 243 switch (r_encoder_sw_counter) 244 { 245 case 0: 246 r_rpm_amount = amount[0]; 247 break; 248 case 1: 249 r_rpm_amount = amount[1]; 250 break; 251 case 2: 252 r_rpm_amount = amount[2]; 253 break; 254 } 255}//end encoder_switch 256/***************************************************************************/ 257 258// reading FRONT Fan conrolling value to EEPROM using 2bytes 259int EEPROM_int_read(int addr) { 260 byte raw[2]; 261 for(byte i = 0; i < 2; i++) raw[i] = EEPROM.read(addr+i); 262 int &f_c = (int&)raw; 263 return f_c; 264} 265 266// writing FRONT Fan conrolling value to EEPROM using 2bytes 267void EEPROM_int_write(int addr, int f_c) { 268 if (EEPROM_int_read(addr)!= f_c){// 269 byte raw[2]; 270 (int&)raw = f_c; 271 for(byte i = 0; i < 2; i++) EEPROM.write(addr+i, raw[i]); 272 } 273} 274// reading REAR Fan conrolling value to EEPROM using 2bytes 275int EEPROM_int_read2(int addr) { 276 byte raw[2]; 277 for(byte i = 0; i < 2; i++) raw[i] = EEPROM.read(addr+i); 278 int &num = (int&)raw; 279 return num; 280} 281 282// writing REAR Fan conrolling value to EEPROM using 2bytes 283void EEPROM_int_write2(int addr, int num) { 284 if (EEPROM_int_read2(addr)!= num){// 285 byte raw[2]; 286 (int&)raw = num; 287 for(byte i = 0; i < 2; i++) EEPROM.write(addr+i, raw[i]); 288 } 289} 290 291void encoder_reading() 292{ 293 //FRONT// 294 if(f_encoder_a != f_encoder_a_prev) //Increasing 295 { 296 if(f_encoder_b != f_encoder_a) 297 { 298 if(f_encoder_a_prev==true) 299 { 300 if((f_counter+f_rpm_amount)<4096) 301 { 302 f_counter = f_counter + f_rpm_amount; 303 EEPROM_int_write(0, f_counter); 304 } 305 } 306 f_encoder_a_prev = f_encoder_a; 307 } 308 else //Decreasing 309 { 310 if(f_encoder_a_prev==true) 311 { 312 if(f_counter>200) 313 { 314 f_counter = f_counter - f_rpm_amount; 315 EEPROM_int_write(0, f_counter); 316 } 317 } 318 f_encoder_a_prev = f_encoder_a; 319 } 320 321 } 322 //REAR// 323 if(r_encoder_a != r_encoder_a_prev) //Increasing 324 { 325 if(r_encoder_b != r_encoder_a) 326 { 327 if(r_encoder_a_prev==true) 328 { 329 if((r_counter+r_rpm_amount)<4096) 330 { 331 r_counter = r_counter + r_rpm_amount; 332 EEPROM_int_write2(10, r_counter); 333 } 334 } 335 r_encoder_a_prev = r_encoder_a; 336 } 337 else //Decreasing 338 { 339 if(r_encoder_a_prev==true) 340 { 341 if(r_counter>200) 342 { 343 r_counter = r_counter - r_rpm_amount; 344 EEPROM_int_write2(10, r_counter); 345 } 346 } 347 r_encoder_a_prev = r_encoder_a; 348 } 349 350 } 351 352}//end encoder_reading 353 354void pwm_out() 355{ 356 if(switch_temp==true) 357 { 358 pwm.setPWM(f_pwmnum, 0, f_counter % 4096 ); 359 pwm.setPWM(r_pwmnum, 0, r_counter % 4096 ); 360 }else 361 { 362 pwm.setPWM(f_pwmnum, 0, f_counter_temp % 4096 ); 363 pwm.setPWM(r_pwmnum, 0, r_counter_temp % 4096 ); 364 } 365} 366 367
Downloadable files
Schematic
Schematic connection
Schematic
Schematic
Schematic connection
Schematic
Comments
Only logged in users can leave comments