본문 바로가기

개발이야기

자동 온도 변화에 따라 선풍기 속도 조절

자동 온도 변화에 따라 선풍기 속도 조절


#include<math.h>

#include <SoftwareSerial.h>


#define A 0.001129148

#define B 0.000234125

#define C 0.0000000876741

#define Vin 5.0

#define R2 10000.0


int mtSpeed[3] = {190, 220, 255};

int mtStep = 0;

int mtPin = 6;

int ledPin [3] = {10, 11, 12};

int btMtStep = 0;

int loopCnt = 0;


SoftwareSerial BTSerial(2,3);


void setup() {

pinMode(mtPin, OUTPUT);

for(int i = 0 ; i < 3 ; i++) {

pinMode(ledPin[i], OUTPUT);

}


Serial.begin(9600);

BTSerial.begin(9600);

}


void loop() {

int adcData = analogRead(A0);

double Vout = (adcData*Vin) / 1023;

double Rth = ((Vin*R2)/Vout) - R2;

double T = SteinharHart(Rth) - 273.15;


if(BTSerial.available()) {

char code = BTSerial.read();


Serial.print("Code: ");

Serial.print(code);

switch(code) {

case 'a':

btMtStep = 9;

break;

case '1':

btMtStep = 1;

break;

case '2':

btMtStep = 2;

break;

case '3':

btMtStep = 3;

break;

default :

btMtStep = 0;

break;

}

Serial.print("btMtStep: ");

Serial.print(btMtStep);

Serial.print("mtStep: ");

Serial.print(mtStep);

}


if(btMtStep == 9) {

tCheck(int(T));

} else if(btMtStep >= 1 && btMtStep <= 3) {

mtSetup(btMtStep);

} else mtSetup(0);


loopCnt++;

if(loopCnt > 600) {

BTSerial.print("C: ");

BTSerial.print(T);

BTSerial.print("mt: ");

BTSerial.println(mtStep);


Serial.print("C: ");

Serial.print(T);

Serial.print("mt: ");

Serial.println(mtStep);

loopCnt = 0;

}

}


void tCheck(int T) {

if(T > 22 && T <= 26) {

mtStep = 1;

} else if (T > 26 && T <= 30) {

mtStep = 2;

} else if (T > 30) {

mtStep = 3;

} else {

}

mtSetup(mtStep);

}


void mtSetup(int step) {


if(step >= 1 && step <=3)

analogWrite(mtPin, mtSpeed[step-1]);

else analogWrite(mtPin, 0);

mtStep = step;

ledOff();

switch(step) {

case 3:

digitalWrite(ledPin[2], HIGH);

case 2:

digitalWrite(ledPin[1], HIGH);

case 1:

digitalWrite(ledPin[0], HIGH);

break;

}

}


void ledOff() {

for(int i = 0 ; i < 3 ; i++) {

digitalWrite(ledPin[i], LOW);

}

}


double SteinharHart(double R) {

double logR = log(R);

double logR3 = logR * logR * logR;

return 1.0 / (A + B * logR + C * logR3);

}