need help with mathematical code

Hi .
Anybody have idea how to translate this to arduino code . I´m trying but always errors.

is as follow

Td =(K1/100)(Ta -K2/10)+(K3/100)(Exp(K4/1000*Ta))^(K5/100)+T67
Exp(n)= e (the base of natural logarithms) raised to the power of n.

and this

If Abs((K2 / 10 - Ta)) < 1 Then T67 =Sgn(K6)Sgn(Ta -K2/10)Abs((K2/10-Ta))
Else T67 =K6/10
Sgn(Ta -K2/10)
(Log(Abs((K2/10-Ta)))/Log(10)+K7/100)
End If
where Sgn( x ) = function that returns the sign of x
Log( x ) = function that returns the natural logarithm of x
Abs( x ) = function that returns the absolute value of x

for this last one I make this but I lost with the Sgn that i don´t know what fuction use

K9=(K2/10-Tamb);
abs (K9);
if (K9<1)
{T67 =Sgn(K6)Sgn(Tamb -K2/10)abs(K9);} else {T67 =K6/10Sgn(Tamb -K2/10)(log (abs((K9)))/log(10)+K7/100);}

Also I don´t know in this last one if the log is really the fuction that give the logarithm of K9.
Also I try to use the fuction pow , but my arduine ide seens like not recognice pow , any idea.?

Thank in advance

Carlos

I think most of those function exist in the Arduino's C/C++ compiler but the names are all lower case. e.g. Exp should be exp, and Abs should be abs, etc.

Pete

Also I try to use the fuction pow , but my arduine ide seens like not recognice pow , any idea.?

What makes you think that?

http://arduino.cc/en/Reference/Pow#.UyXOkygz2kU

if i what to do this

(Exp(K4/1000*Ta))^(K5/100)+T67

I think will be
pow (exp(K4/1000*Ta), ((K5/100)+67))

but my ide not put in yellow the pow

also how i can do this
where Sgn( x ) = function that returns the sign of x
what code have arfduino for make this ?

without the sgn this compile fine
{T67 =(K6)(Tamb -K2/10)abs(K9);} else {T67 =K6/10(Tamb -K2/10)(log (abs((K9)))/log(10)+K7/100);}

Carlos

but my ide not put in yellow the pow

Color-coding is a crutch. It means exactly nothing.

(Exp(K4/1000*Ta))^(K5/100)+T67

I think will be
pow (exp(K4/1000*Ta), ((K5/100)+67))

No. It will be:

pow (exp(K4/1000*Ta), K5/100)+T67;

There is no sgn function in the math library. You will have to write your own.

Pete

first use spaces to make formulas more readable!
use better names than 2 letter acronyms.

// Td =(K1/100)(Ta -K2/10)+(K3/100)(Exp(K4/1000*Ta))^(K5/100)+T67

Td = (K1/100) * ( Ta -K2/10)  +  (K3/100) * pow( exp(K4/1000*Ta), (K5/100) )  +  T67;

// If Abs((K2 / 10 - Ta)) < 1 Then T67 =Sgn(K6)Sgn(Ta -K2/10)Abs((K2/10-Ta))
// Else T67 =K6/10
Sgn(Ta -K2/10)
(Log(Abs((K2/10-Ta)))/Log(10)+K7/100)
// End If

// extracted common sub expression
float temp1 = K2/10 -Ta;
float temp2 = abs(temp1);

if (temp2 < 1)
{
  T67 = sign(K6) * sign(-temp1) * temp2;
}
else
{
  T67 = K6/10 * sign(-temp1) *  (log( temp2 )/log(10) + K7/100 );
}
// not the ultimate implementation (IEEE754 also has 0.0 and  -0.0) , but it will probably be good enough
float sign(float n)
{
  if (n < 0) return -1;
  return 1;
}

K9=(K2/10-Tamb);
abs (K9);
if (K9<1)
{T67 =Sgn(K6)Sgn(Tamb -K2/10)abs(K9);} else {T67 =K6/10Sgn(Tamb -K2/10)(log (abs((K9)))/log(10)+K7/100);}

should be easy now.

oh woww , ¡¡¡

thank you very much , much clear for me , I try it to see .

Carlos

hi again

this code don´t work

// not the ultimate implementation (IEEE754 also has 0.0 and -0.0) , but it will probably be good enough
float sign(float n)
{
if (n < 0) return -1;
return 1;
}
I modify for somethink like this

// not the ultimate implementation (IEEE754 also has 0.0 and  -0.0) , but it will probably be good enough
  float sign(float temp2);

  if (temp2 < 0) {return -1;} else
  {return 1;}

but also some is wrong because the compiler say me this error
returning 'void'
CloudDetectorFinal:90: error: return-statement with a value, in function returning 'void'

any idea

I put the complete code to see what is going wrong

float Tsky;
  float Tir;
  float Td;
  float Tamb;
  int K1;
  int K2;
  int K3;
  int K4;
  int K5;
  int K6;
  int K7;
  float T67;
 

// extracted common sub expression
float temp1 = K2/10 -Tamb;
float temp2 = abs(temp1);
 // not the ultimate implementation (IEEE754 also has 0.0 and  -0.0) , but it will probably be good enough
  float sign(float temp2);

  if (temp2 < 0) return -1;
  return 1;

if (temp2 < 1)
{
  T67 = sign(K6) * sign(-temp1) * temp2;
}
else
{
  T67 = K6/10 * sign(-temp1) *  (log( temp2 )/log(10) + K7/100 );
}
Td = (K1/100) * ( Tamb -K2/10)  +  (K3/100) * pow( exp(K4/1000*Tamb), (K5/100) )  +  T67;

  Tsky=Tir-Td;

cvdarias:
hi again

this code don´t work

// not the ultimate implementation (IEEE754 also has 0.0 and -0.0) , but it will probably be good enough
float sign(float n)
{
if (n < 0) return -1;
return 1;
}

What does not work? can you give an example value for which the code fails?

I modify for somethink like this

// not the ultimate implementation (IEEE754 also has 0.0 and  -0.0) , but it will probably be good enough

float sign(float temp2);

if (temp2 < 0) {return -1;} else
  {return 1;}




but also some is wrong because the compiler say me this error
returning 'void'
CloudDetectorFinal:90: error: return-statement with a value, in function returning 'void'

any idea

The ; after the function declaration is incorrect.

cvdarias:
I put the complete code to see what is going wrong

float Tsky;

float Tir;
  float Td;
  float Tamb;
  int K1;
  int K2;
  int K3;
  int K4;
  int K5;
  int K6;
  int K7;
  float T67;

// extracted common sub expression
float temp1 = K2/10 -Tamb;
float temp2 = abs(temp1);
// not the ultimate implementation (IEEE754 also has 0.0 and  -0.0) , but it will probably be good enough
  float sign(float temp2);

if (temp2 < 0) return -1;
  return 1;

if (temp2 < 1)
{
  T67 = sign(K6) * sign(-temp1) * temp2;
}
else
{
  T67 = K6/10 * sign(-temp1) *  (log( temp2 )/log(10) + K7/100 );
}
Td = (K1/100) * ( Tamb -K2/10)  +  (K3/100) * pow( exp(K4/1000*Tamb), (K5/100) )  +  T67;

Tsky=Tir-Td;

This is not a program, you cannot even compile this. It is a collection code snippets.

You need a setup() and a loop() function.

Please create a proper sketch first including the code snippets

Hi , well the complete program is this , but have a lot of thing that in the final version not go because first I want to see what I se in the serial and after go to a little display, for this reason some of the code are in // and almost of the serial.print will be delated.

#include <math.h>
#include <dht.h>
#include <Wire.h>
#include <Adafruit_MLX90614.h>
#define DHT11_PIN 4
#define _Digole_Serial_UART_
#include <DigoleSerial.h>
#if defined(_Digole_Serial_UART_)
DigoleSerialDisp mydisp(&Serial, 38400); //UART:Arduino UNO: Pin 1(TX)on arduino to RX on module
#endif
#define SC_W 96  //screen width in pixels
#define SC_H 64  //screen Hight in pixels
#include "detectornubes.h"



dht DHT;
Adafruit_MLX90614 mlx = Adafruit_MLX90614();
int inputPin = 3;               // Connect push button to input pin 3



void setup(){
  pinMode(inputPin, INPUT);// declara el boton como una entrada
  pinMode(6, OUTPUT);//piezo buzzer
  mlx.begin();
  mydisp.begin();
  //mydisp.setColor(224);
  //mydisp.clearScreen(); //CLear screen
  //mydisp.setFont(fonts[2]);
  //mydisp.setMode('C'); //set graphic Drawing Mode to COPY
  //mydisp.displayStartScreen1
  Serial.begin(9600);
  Serial.println("DHT TEST PROGRAM ");
  Serial.print("LIBRARY VERSION: ");
  Serial.println(DHT_LIB_VERSION);
  Serial.println();
  Serial.println("Type,\tstatus,\tHumidity (%),\tTemperature (C)");
  Serial.println("Adafruit MLX90614 test");
  delay (3000);
  
}

void loop(){
  
   // READ DATA
   //mydisp.clearScreen();
  Serial.print("DHT11, \t");
  DHT.read11(DHT11_PIN);
  //mydisp.print("Temperatura = ")
  //mydisp.print(DHT.temperature,1)
  //mydisp.print("Humedad = "
  //mydisp.print(DHT.humidity,1)
  
 // DISPLAT DATA
  Serial.print(DHT.humidity,1);
  Serial.print(",\t");
  Serial.println(DHT.temperature,1);
  Serial.print("Ambient = ");
  Serial.print(mlx.readAmbientTempC()); 
  Serial.print("*C\tObject = ");
  Serial.print(mlx.readObjectTempC());
  Serial.println("*C");
  Serial.print("Ambient = "); 
  Serial.print(mlx.readAmbientTempF()); 
  Serial.print("*F\tObject = "); 
  Serial.print(mlx.readObjectTempF());
  Serial.println("*F");
  float Tsky;
  float Tir;
  float Td;
  float Tamb;
  int K1;
  int K2;
  int K3;
  int K4;
  int K5;
  int K6;
  int K7;
  float T67;
  float temp1;
  float temp2;
// extracted common sub expression
temp1 = K2/10 -Tamb;
temp2 = abs(temp1);

if (temp2 < 1)
{
  T67 = (K6) * (-temp1) * temp2;
}
else
{
  T67 = K6/10 * (-temp1) *  (log( temp2 )/log(10) + K7/100 );
}

Td = (K1/100) * ( Tamb -K2/10)  +  (K3/100) * pow( exp(K4/1000*Tamb), (K5/100) )  +  T67;
Tir = (mlx.readObjectTempC());
Tsky=Tir-Td;
  //if(Tsky>=0)
  //{  
  //mydisp.drawBitmap256(39,30,70,44,nubes);
  //}
  //if(Tsky < 0 && Tsky > -10)
  //{
  //mydisp.drawBitmap(39,30,70,40,nubesluna);
  //}
  //if(Tsky <= -10)
  //{
  //mydisp.drawBitmap(39,30,70,40,luna);
  //}
  int val = digitalRead(inputPin);  // read input value
  if (val == HIGH)  { mydisp.backLightOff(); } //turn display off
  else {mydisp.backLightOn();}              //turn display on
  //Hace sonar el buzzer cuando la humedad es igual o superior a 90
  int humedad = (DHT.humidity,1);
  if (humedad>=90) { digitalWrite(6, HIGH);}
  else { digitalWrite(6, LOW);}
  
  
  
  delay(1000); 
}

now look better , no ;-), well this version is compiled ok but without the " sight"

Carlos

now look better , no

I'd be happier to see some auto format

#include <math.h>
#include <dht.h>
#include <Wire.h>
#include <Adafruit_MLX90614.h>
#define DHT11_PIN 4
#define _Digole_Serial_UART_
#include <DigoleSerial.h>
#if defined(_Digole_Serial_UART_)
DigoleSerialDisp mydisp(&Serial, 38400); //UART:Arduino UNO: Pin 1(TX)on arduino to RX on module
#endif
#define SC_W 96  //screen width in pixels
#define SC_H 64  //screen Hight in pixels
#include "detectornubes.h"



dht DHT;
Adafruit_MLX90614 mlx = Adafruit_MLX90614();
int inputPin = 3;               // Connect push button to input pin 3



void setup(){
  pinMode(inputPin, INPUT);// declara el boton como una entrada
  pinMode(6, OUTPUT);//piezo buzzer
  mlx.begin();
  mydisp.begin();
  //mydisp.setColor(224);
  //mydisp.clearScreen(); //CLear screen
  //mydisp.setFont(fonts[2]);
  //mydisp.setMode('C'); //set graphic Drawing Mode to COPY
  //mydisp.displayStartScreen1
  Serial.begin(9600);
  Serial.println("DHT TEST PROGRAM ");
  Serial.print("LIBRARY VERSION: ");
  Serial.println(DHT_LIB_VERSION);
  Serial.println();
  Serial.println("Type,\tstatus,\tHumidity (%),\tTemperature (C)");
  Serial.println("Adafruit MLX90614 test");
  delay (3000);

}

void loop(){

  // READ DATA
  //mydisp.clearScreen();
  Serial.print("DHT11, \t");
  DHT.read11(DHT11_PIN);
  //mydisp.print("Temperatura = ")
  //mydisp.print(DHT.temperature,1)
  //mydisp.print("Humedad = "
  //mydisp.print(DHT.humidity,1)

  // DISPLAT DATA
  Serial.print(DHT.humidity,1);
  Serial.print(",\t");
  Serial.println(DHT.temperature,1);
  Serial.print("Ambient = ");
  Serial.print(mlx.readAmbientTempC()); 
  Serial.print("*C\tObject = ");
  Serial.print(mlx.readObjectTempC());
  Serial.println("*C");
  Serial.print("Ambient = "); 
  Serial.print(mlx.readAmbientTempF()); 
  Serial.print("*F\tObject = "); 
  Serial.print(mlx.readObjectTempF());
  Serial.println("*F");
  float Tsky;
  float Tir;
  float Td;
  float Tamb;
  int K1;
  int K2;
  int K3;
  int K4;
  int K5;
  int K6;
  int K7;
  float T67;
  float temp1;
  float temp2;
  // extracted common sub expression
  temp1 = K2/10 -Tamb;
  temp2 = abs(temp1);

  if (temp2 < 1)
  {
    T67 = (K6) * (-temp1) * temp2;
  }
  else
  {
    T67 = K6/10 * (-temp1) *  (log( temp2 )/log(10) + K7/100 );
  }

  Td = (K1/100) * ( Tamb -K2/10)  +  (K3/100) * pow( exp(K4/1000*Tamb), (K5/100) )  +  T67;
  Tir = (mlx.readObjectTempC());
  Tsky=Tir-Td;
  //if(Tsky>=0)
  //{  
  //mydisp.drawBitmap256(39,30,70,44,nubes);
  //}
  //if(Tsky < 0 && Tsky > -10)
  //{
  //mydisp.drawBitmap(39,30,70,40,nubesluna);
  //}
  //if(Tsky <= -10)
  //{
  //mydisp.drawBitmap(39,30,70,40,luna);
  //}
  int val = digitalRead(inputPin);  // read input value
  if (val == HIGH)  { 
    mydisp.backLightOff(); 
  } //turn display off
  else {
    mydisp.backLightOn();
  }              //turn display on
  //Hace sonar el buzzer cuando la humedad es igual o superior a 90
  int humedad = (DHT.humidity,1);
  if (humedad>=90) { 
    digitalWrite(6, HIGH);
  }
  else { 
    digitalWrite(6, LOW);
  }

  delay(1000); 
}

moderator update : replaced quote tags -> code tags

Are you deliberately being a troll? You've been asked to post code correctly. THAT IS NOT CORRECT!

sorry i don´t understand you

Hint: use code tags "#", not quotes, to post your code.

If you are translating a program from the BASIC language, then it may be a very bad idea to declare K1, K2, etc. as type int as you do below

  int K1;
  int K2;
  int K3;

because expressions like the one below in BASIC are often evaluated as floating point numbers.

Td = (K1/100) * ( Tamb -K2/10)  +  (K3/100) * pow( exp(K4/1000*Tamb), (K5/100) )  +  T67;

Without extensive tests, I'm never sure exactly what a C/C++ compiler does in cases like the above but it seems very likely that if K3 is of type int, then (K3/1000) will be evaluated as an int (truncating the remainder) and then converting to floating point for multiplication with pow(). In any case, you should carefully test the finished program to make sure that the results of the calculation are correct. If not, try declaring K1, K2, etc. to be of type float.

Thank¡¡¡

well , no problem I think ,m the K1K2K3K4K5 variables havesa fixes value that is not a float number , this is why I declare like int, I read in some place that use float can make errors in same case if the number is not a float number , them :wink:

Thank you very much¡¡¡¡¡

Carlos

Thank to all

The code work

thank ¡¡¡

Carlos