简体   繁体   English

Arduino Uno代码上的多个MLX90614(温度传感器)

[英]Multiple MLX90614 (temperature sensor) on Arduino Uno code

I have been assigned with a task to create a temperature based fan controlling over the PWM output. 我被分配了一个任务,以创建一个基于温度的风扇来控制PWM输出。 I am using Arduino Uno with AVRISPmkII programmer and ATmega328 controller. 我正在将Arduino Uno与AVRISPmkII编程器和ATmega328控制器配合使用。 Now the task is to connect 4 MLX90614 sensors on a I²C wire (I have assigned the different slave addresses to each sensor as I cannot connect them on I²C, each having the same address). 现在的任务是在I²C线上连接4个MLX90614传感器(我为每个传感器分配了不同的从机地址,因为我无法在I²C上连接它们,每个地址都具有相同的地址)。

I am able to read the temperature of each sensor and get the maximum temperature as well. 我能够读取每个传感器的温度并获得最高温度。 But the problem comes when controlling the four fans over PWM. 但是问题出在通过PWM控制四个风扇时。 I have to get the temperature reading from each sensor, get the maximum temperature, and then control the fans' speed according to the set temperature range over PWM output. 我必须从每个传感器获取温度读数,获取最高温度,然后根据PWM输出上设置的温度范围控制风扇的速度。 What is the matter with the code? 代码有什么问题?

#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;
}

I did not read all your code, but it looks like you're not commanding your fans at all. 我没有阅读所有代码,但看起来您根本没有命令粉丝。

/* 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);
}

Will make your 'setfanmode' function do something. 将使您的“ setfanmode”功能起作用。 I did not check for errors in the rest of your code, though. 不过,我没有检查其余代码中的错误。 If you have got several fan to control, you may want to change your function to the following: 如果要控制多个风扇,则可能需要将功能更改为以下内容:

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);
}

and I'm letting you imagine how you can transform your code for controlling several fans. 我让你想象一下如何改变代码以控制多个风扇。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM