# Writing an equation based on sensor results?

I’m a student at Clemson University and I am currently working on my senior capstone project which is to put together and program four temperature and relative humidity sensors in order to calculate moisture content in grain. These sensors are going to be applied to a miniature grain dryer. Right now I’m running a Metro 328 with a TCA9548A multiplexer and two BME280 temperature & relative humidity sensors. At the moment I’ve got both sensors displaying temp & humidity, which is great but it’s only halfway complete. Now I need to figure out how to incorporate an equation to calculate the moisture contents based off of each individual sensor’s readings. This is my first experience with writing code/programming, and so far everything that I’ve tried has resulted in multiple errors. So I figured it’s time to seek help and go to the forums.

The equation that I need to include looks like this:

MCdb = [(ln(1-RH))/(-K(T+C))]^(1/N)

Where:
MCdb = Moisture Content, dry basis
K, N, and C = coefficients defined for various grain types,
RH = relative humidity (decimal), and
T = temperature (degrees C)

I know that each coefficient has to be defined, but after that I’m lost. Could anyone point me in the right direction? I will attach a picture of my application for the sensors and paste the program I’ve got so far.

#include <Wire.h>
extern “C” {
#include “utility/twi.h” // from Wire library, so we can do bus scanning
}
#include <SPI.h>

//MC Coefficients:
#define K (1)
#define C (1)
#define N (1)

void tcaselect(uint8_t i) {
if (i > 7) return;

Wire.write(1 << i);
Wire.endTransmission();
}

void setup(void)
{
while (!Serial);
delay(1000);

Wire.begin();

Serial.begin(9600);

for (uint8_t t=0; t<8; t++) {
tcaselect(t);
Serial.print(“TCA Port #”); Serial.println(t);

uint8_t data;
if (! twi_writeTo(addr, &data, 0, 1, 1)) {
}
}
}
Serial.println("\ndone");

Serial.println(“BME280 Test”); Serial.println("");

tcaselect(0);
if(!bme1.begin())
{

Serial.println(“Ooops, no BMP280 detected … Check your wiring!”);
while(1);
}

tcaselect(1);
if(!bme2.begin())
{

Serial.println(“Ooops, no BMP280 detected … Check your wiring!”);
while(1);
}

}

void loop(void)
{
sensors_event_t event;

tcaselect(0);

Serial.print("Sensor 1 Temperature = ");
Serial.println(“F”);

/*
Serial.print(“Sensor 1 Pressure = “);
Serial.println(” Pa”);
*/

Serial.print(“Sensor 1 Humidity = “);
Serial.println(”%”);

Serial.print("Sensor 1 MC = ");
Serial.print(
Serial.println(“Db”);

tcaselect(1);

Serial.print("Sensor 2 Temperature = ");
Serial.println(“F”);

/*
Serial.print(“Sensor 2 Pressure = “);
Serial.println(” Pa”);
*/

Serial.print(“Sensor 2 Humidity = “);
Serial.println(”%”);

delay(10000);
}

don't do such maths using integers - go with float (add a decimal point to the constants)

``````//MC Coefficients:
const float K = 1.0;
const float C = 1.0;
const float N = 1.0;

...

float tf = bme2.readTemperature()*9.0 / 5.0 + 32;
...
``````

you can use the pow() function for the exponent and have a look here for the common math functions

`[code]`` [color=blue]// your code is here[/color] ``[/code]`.
It should look like this:`// your code is here`