Arduino风扇控制器代码问题(LM35)

时间:2017-12-19 19:52:50

标签: arduino

风扇上的PWM信号出现了问题,当它达到21°C时它实际上会达到100%,而应该是10%。我不认为这是一个电路问题,所以对代码的任何建议?我很确定问题出在Map函数的某个地方,似乎无法弄明白。

 #include <LiquidCrystal.h>
LiquidCrystal lcd(12,11,5,4,3,2);
int tempPin = A1;   // the output pin of LM35
int fan = 11;       // the pin where fan is
int led = 8;        // led pin
int temp;
int tempMin = 20;   // the temperature to start the fan
int tempMax = 30;   // the maximum temperature when fan is at 100%
int fanSpeed;
int fanLCD;

void setup() {
  pinMode(fan, OUTPUT);
  pinMode(led, OUTPUT);
  pinMode(tempPin, INPUT);
  lcd.begin(16,2);  
  Serial.begin(9600);
}

void loop() {  
   temp = readTemp();     // get the temperature
   if(temp  < tempMin) { // if temp is lower than minimum temp 
      fanSpeed = 0; // fan is not spinning 
      digitalWrite(fan, LOW); 
   } 
   if((temp  >= tempMin) && (temp <= tempMax)) { // if temperature is higher than minimum temp 
      fanSpeed = map(temp, tempMin, tempMax, 115, 255); // the actual speed of fan 
      fanLCD = map(temp, tempMin, tempMax, 0, 100); // speed of fan to display on LCD 
      analogWrite(fan, fanSpeed); // spin the fan at the fanSpeed speed 
   } 
   if(temp  > tempMax) {        // if temp is higher than tempMax
     digitalWrite(led, HIGH);  // turn on led 
   } else {                    // else turn of led
     digitalWrite(led, LOW); 
   }

   lcd.print("TEMP: ");
   lcd.print(temp,1);      // display the temperature
   lcd.print("C ");
   lcd.setCursor(0,1);   // move cursor to next line
   lcd.print("FAN: ");
   lcd.print(fanLCD);    // display the fan speed
   lcd.print("%");
   delay(500);
   lcd.clear();   
}

int readTemp() {  // get the temperature and convert it to celsius
  temp = analogRead(tempPin);
  return (temp * 0.48828125)-48;
}

1 个答案:

答案 0 :(得分:0)

好吧,很明显,这个特定的引脚有问题,我尝试了另一个(PWM),它工作得很好!