[英]Multiple MLX90614 (temperature sensor) on Arduino Uno code
我被分配了一個任務,以創建一個基於溫度的風扇來控制PWM輸出。 我正在將Arduino Uno與AVRISPmkII編程器和ATmega328控制器配合使用。 現在的任務是在I²C線上連接4個MLX90614傳感器(我為每個傳感器分配了不同的從機地址,因為我無法在I²C上連接它們,每個地址都具有相同的地址)。
我能夠讀取每個傳感器的溫度並獲得最高溫度。 但是問題出在通過PWM控制四個風扇時。 我必須從每個傳感器獲取溫度讀數,獲取最高溫度,然后根據PWM輸出上設置的溫度范圍控制風扇的速度。 代碼有什么問題?
#include <i2cmaster.h>
int PWMoutput=0;
int Temp[4];
int sensor[4]={0xAC<<1, 0xCC<<1, 0xC0<<1, 0xB0<<1}; // Array with address of four mlx90614 sensors
int Maxtemp;
int fan = 9; // FAN connected to digital pin 9
//------------------------------------------------//
//------------------SETUP-------------------------//
//------------------------------------------------//
void setup()
{
Serial.begin(9600); // Start serial communication at 9600 bit/s.
i2c_init(); // Initialise the I²C bus.
PORTC = (1 << PORTC4) | (1 << PORTC5); // Enable pullups.
// Initialize the digital pin as an output.
pinMode(fan, OUTPUT);
}
//------------------------------------------------//
//-------------------MAIN-------------------------//
//------------------------------------------------//
void loop()
{
Maxtemp=readtemp(0);
setfanmode(Maxtemp);
// Prints all readings in the serial port
i=0;
/*while(i<1)
{
Serial.print("Sensor");
Serial.println(i,DEC);
Serial.print("Celcius: ");
Serial.println(Temp[i],DEC);
i++;
}*/
Serial.print("Maximum: Celcius: ");
Serial.println(Maxtemp);
}
void setfanmode(int degree) // Setting the fan speed modes according to
// the temperature in Celcius
// with PWM Duty cycle to 0%, 50% and 100%.
{
if (degree<=30)
PWMoutput=0;
else if(degree<=60)
PWMoutput=127;
else if(degree<=80)
PWMoutput=255;
}
int maxtemp() // Reading max temperature
{
int i=1;
int temperature;
while (i<4)
{
if (Temp[i-1]<Temp[i])
temperature=Temp[i];
i++;
}
return(temperature);
}
float readtemp(int sensornumb)
{
int data_low = 0;
int data_high = 0;
int pec = 0;
// Write
i2c_start_wait(sensor[sensornumb]+I2C_WRITE);
i2c_write(0x07);
// Read
i2c_rep_start(sensor[sensornumb]+I2C_READ);
data_low = i2c_readAck(); // Read 1 byte and then send ack.
data_high = i2c_readAck(); // Read 1 byte and then send ack.
pec = i2c_readNak();
i2c_stop();
// This converts high and low bytes together and processes temperature,
// MSB is a error bit and is ignored for temperatures.
double tempFactor = 0.02; // 0.02 degrees per LSB (measurement
// resolution of the MLX90614).
double tempData = 0x0000; // Zero out the data.
int frac; // Data past the decimal point.
// This masks off the error bit of the high byte, then moves it left
// 8 bits and adds the low byte.
tempData = (double)(((data_high & 0x007F) << 8) + data_low);
tempData = (tempData * tempFactor)-0.01;
float celcius = tempData - 273.15;
// Returns temperature in Celcius.
return celcius;
}
我沒有閱讀所有代碼,但看起來您根本沒有命令粉絲。
/* setting the fan speed modes according to
* the temperature in celcius
* with PWM Duty cycle to 0%, 50% & 100%
*/
void setfanmode(int degree)
{
int PWMoutput;
if (degree<=30)
PWMoutput=0;
else if(degree<=60)
PWMoutput=127;
else if(degree<=80)
PWMoutput=255;
analogWrite(fan, PWMoutput);
}
將使您的“ setfanmode”功能起作用。 不過,我沒有檢查其余代碼中的錯誤。 如果要控制多個風扇,則可能需要將功能更改為以下內容:
void setfanmode(int degree, int fanpin)
{
int PWMoutput;
if (degree<=30)
PWMoutput=0;
else if(degree<=60)
PWMoutput=127;
else if(degree<=80)
PWMoutput=255;
analogWrite(fanpin, PWMoutput);
}
我讓你想象一下如何改變代碼以控制多個風扇。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.