Hi everyone,
I took the code maxbot & Baboomba originally posted on this forum and uncommented v_ir_comp[64], so that when I am calculating T_o, emissivity would always be accounted for. However, the code seems to be repeating in void setup().
I have attached the parts of the code that I modified but the rest of it is identical to the original post. If you run it as is, you well see that "initialization" will be repeated printed in the serial port.
Here is the link to the i2cmaster library that worked for me on the original code. II know a lot of ppl have been looking for it. http://bildr.org/2011/02/mlx90614-arduino/
Here I uncomment v_ir_comp. Note that I also am calculating each alpha_ij value as well. I have plenty of room, accuracy is my first priority.
/*
* Attention! I commented out the alpha_ij array, so if you're going to compile the sketch you'll get for sure an error.
* You should replace all 64 values with the alpha_ij calculated using the values stored in your MLX90620's EEPROM.
* I suggest you to make an EEPROM dump, print it on the Serial port and store it in a file. From there, with the help of a spreadsheet (Libreoffice, Google Docs, Excel...) calculate your own alpha_ij values.
* Please also pay attention to your emissivity value: since in my case it was equal to 1, to save SRAM i cut out that piece of calculation. You need to restore those lines if your emissivity value is not equal to 1.
* Page and section numbers refer to the MLX90630 Datasheet
*/
#include <i2cmaster.h>
int freq = 16; //Set this value to your desired refresh frequency
int IRDATA[64];
byte CFG_LSB, CFG_MSB, PTAT_LSB, PTAT_MSB, CPIX_LSB, CPIX_MSB, PIX_LSB, PIX_MSB;
int PIX, v_th, CPIX;
float ta, to, emissivity, k_t1, k_t2;
float temperatures[64];
int count=0;
unsigned int PTAT;
int a_cp, b_cp, tgc, b_i_scale;
int a_ij[64];
int b_ij[64];
int delta_alpha_ij[64];
float alpha_ij[64]; //I recalulcate alpha_ij in varInitialization function (nested inside read_EEPROM_MLX90620 function) during setup loop for automatic calibration
//float v_ir_off_comp[64]; //I'm going to merge v_ir_off_comp calculation into v_ir_tgc_comp equation. It's not required anywhere else, so I'll save 256 bytes of SRAM doing this.
float v_ir_tgc_comp[64];
float v_ir_comp[64]; //removed to save SRAM, in my case v_ir_comp == v_ir_tgc_comp because emmissivity == 1
Then I callculate v_ir_comp using emissivity
void calculate_TO(){ //Calculates the temperature seen from sensor in C (pg 16-19)
float v_cp_off_comp = (float) CPIX - (a_cp + (b_cp/pow(2, b_i_scale)) * (ta - 25)); //this is needed only during the to calculation, so I declare it here.
for (int i=0; i<64; i++){
v_ir_tgc_comp[i] = IRDATA[i] - (a_ij[i] + (float)(b_ij[i]/pow(2, b_i_scale)) * (ta - 25)) - (((float)tgc/32)*v_cp_off_comp);
v_ir_comp[i]= v_ir_tgc_comp[i] / emissivity; //removed to save SRAM, since emissivity in my case is equal to 1.
temperatures[i] = sqrt(sqrt((v_ir_comp[i]/alpha_ij[i]) + pow((ta + 273.15),4))) - 273.15; //if emissivity is not equal to one, this line should be used instead on the preceding line
//temperatures[i] = sqrt(sqrt((v_ir_tgc_comp[i]/alpha_ij[i]) + pow((ta + 273.15),4))) - 273.15; //edited to work with v_ir_tgc_comp instead of v_ir_comp
}
}
Finally, here is were the code repeats. I do not understand how void setup is able to loop.
void setup(){
pinMode(13, OUTPUT);
Serial.begin(115200);
i2c_init();
PORTC = (1 << PORTC4) | (1 << PORTC5);
delay(5);
Serial.println("initialization");
read_EEPROM_MLX90620();
config_MLX90620_Hz(freq);
Serial.println("configuration done");
}
Please let me know what could be causing this. I am trying to multiplex four mlx90620's together to read tire temp on my university FSAE car, and if I can get over this hurdle, I should be able to finish my code.
Thanks everyone!