[Solved]Help!! function wont return.

Help.. iam stumped.. The first function, tempCoolant returns the value just fine, but oilpressure function does not return the value!!! i can print out the value serially just before the return, but if i try to print from the main loop, it wont print like temp coolant does...

Thanks

Nobody likes going to the rubbish bin to get code. Post it here if you want help. Post all of it. And use code tags when you do.

#include <Wire.h>



int oilpres;
int temprF;
int temprFIAT;
const int fanOut = 2;

void setup() {
  // Start i2c master
	Wire.begin();
	// initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
 pinMode(fanOut, OUTPUT);
}




// the loop routine runs over and over again forever:
void loop() {

 tempCoolant(); // Calls on Coolant temp sensor that is in the Intake manifold. 
 Serial.println("CLT Temp:");
 Serial.println(temprF);
 
 //tempIAT();  // Calls on IAT sensor in Manifold
 
 oilpressure();  // calls for OilPressure calculation
 Serial.println("Oil Pressure:");
 Serial.println(oilpres);
 
 delay(1000);
 //fanControl(temprF); // controls fan
// batteryVoltage(); // 1m to 100k voltage divider 1k to ground, 1m to source, measure from middle to ADC.
// Wire.beginTransmission(9); // transmit to device #9
 //Wire.write(oilp);              // sends oil pressure
 //Wire.write(temprF);
 //Wire.endTransmission();    // stop transmitting
}


float tempCoolant()
{

  float array[44] = {
    // Voltage From Sensor on left // Corresponding Temprature on right  ,
    //You find this by using voltage divider forumla. Current set is for a 4.7k ohm R1 and R2 is your ACdelco Temp Sensor
	  2.429165 , 15 ,
	  2.137986 , 20 ,
	  1.864576 , 25 ,
	  1.613833 , 30 ,
	  1.38795  , 35 ,
	  1.188159 , 40,
	  1.013571 , 45,
	  0.8626761 ,50,
	  0.734253 , 55,
	  0.6246509 , 60 ,
	  0.5314699 ,65,
	  0.4536661 ,70,
	  0.3876349 ,75,
	  0.3317441 ,80,
	  0.286803  ,85 ,
	  0.2446477 ,90,
	  0.2110208 ,95,
	  0.1824518 ,100,
	  0.1582331 ,105,
	  0.1376963 ,110,
	  0.1201279 ,115,
	  0.1050845 ,120,
  };

  // read the input on analog pin 0:
  float sensorValue = analogRead(A0);

  // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
  float voltage = sensorValue * (5.00 / 1023.0);    // change for differant base voltage


  float tempr ;

  if ( voltage >= array[0] )
  {
    tempr = array[1] ;

  }
  else if ( voltage <= array[42] )
  {
    tempr = array[43] ;

  }
  else
  {
    for ( int i = 1 ; i < 44 ; i++ )
    {

      int ii = 2 * i ;

      if ( voltage <= array[ii] && voltage >= array[ii + 2] )
      {
        tempr = array[ii + 1] + ( (array[ii + 3] - array[ii + 1]) * ( voltage - array[ii] ) / ( array[ii + 2] - array[ii] )) ;
        break ;
      }
    }
  }



  temprF = tempr * 1.8 + 32;

  
  
 
  return temprF;

  
  
}

float oilpressure()
{

	float array[12] = {
		// Voltage From Sensor on left // Corresponding Temprature on right  ,
		//You find this by using voltage divider forumla. Current set is for a 330 ohm R1 and R2 is your VDO Oil Pressure sensor

		2.105263 , 0.1,
		1.944444 , 14.5,
		1.764706 , 29,
		1.5625 , 43.5,
		1.071429 , 72.5,
		0.4141189 , 100,

	};

	// read the input on analog pin 1:
	float sensorValue = analogRead(A6);

	// Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
	float voltage = sensorValue * (5.00 / 1023.0);    // change for differant base voltage
													  //Serial.println(sensorValue);

	float oilp;

	if (voltage >= array[0])
	{
		oilp = array[1];

	}
	else if (voltage <= array[10])
	{
		oilp = array[11];

	}
	else
	{
		for (int i = 1; i < 12; i++)
		{

			int ii = 2 * i;

			if (voltage <= array[ii] && voltage >= array[ii + 2])
			{
				oilp = array[ii + 1] + ((array[ii + 3] - array[ii + 1]) * (voltage - array[ii]) / (array[ii + 2] - array[ii]));
				break;
			}
		}
	}

	int oilpres = oilp;
	

	return oilpres;





}

Thats the code. SOrry, didnt know pastebin was hated here…

float oilpressure()
{

Your code promises to return a float.

int oilpres = oilp;
	

	return oilpres;

So why are you returning an int?

i tried both ways, but i had to end up changing it to a int , due to you cannot send floats over I2C communication.

what gets me, is that the coolant temp is the same formula, same return style, it works, this one doesnt...

MurdockGT:
i tried both ways, but i had to end up changing it to a int , due to you cannot send floats over I2C communication.

Baloney. I do it all the time. You can send anything over I2C if you break it up into bytes first.

MurdockGT:
what gets me, is that the coolant temp is the same formula, same return style, it works, this one doesnt...

When you say it doesn't return, what do you mean by that? What evidence do you have that it won't return? That would be a compile error. IF you're not getting a compile error then it's returning something it just may not be what you expect.

I simplified it to remove the calcuations.. So... why isnt the value being returned to the main loop to println there.

int oilpres;

void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);

}

void loop() {
  // put your main code here, to run repeatedly:
oilpressure();

  Serial.println("Oil Pressure:");
  Serial.println(oilpres);
  delay(5000);
}

int oilpressure()
{


  int oilpres = 10;
  return oilpres;

}

Delta_G:
When you say it doesn't return, what do you mean by that? What evidence do you have that it won't return? That would be a compile error. IF you're not getting a compile error then it's returning something it just may not be what you expect.

void loop() {
  // put your main code here, to run repeatedly:
oilpressure();

  Serial.println("Oil Pressure:");
  Serial.println(oilpres);
  delay(5000);
}

Oh simple. It is returning and you're throwing that value away.

Try this:

void loop() {
  // put your main code here, to run repeatedly:

  Serial.println("Oil Pressure:");
  Serial.println(oilpressure());
  delay(5000);
}

Or this:

void loop() {
  // put your main code here, to run repeatedly:
int op = oilpressure();

  Serial.println("Oil Pressure:");
  Serial.println(op);
  delay(5000);
}

What do you expect it to do, given that you throw away the value returned by oilpress()? You calculate a value, store it in a local variable named oilpres within oilpress(), return that value, which is then discarded, and finally print a different variable also named oilpres, whose value is never defined.

Regards,
Ray L.

THANK YOU SO MUCH... been pulling my hair out for hours... I used your second method.. Ill put this in my notes book to do it this way from now on..

I see tht now Ray, I now know better. Thanks guys.

Delta, do you have a link or document about changing the float to a bit before sending it? i wont need it in this case, but it would be usefull to know .

thanks

MurdockGT:
Delta, do you have a link or document about changing the float to a bit before sending it? i wont need it in this case, but it would be usefull to know .

thanks

Well, how are you sending an int?

Have you noticed that all Arduino functions have noun and verb in the name? analogRead() reads from an analog pin. digitalWrite() writes to a digital pin.

oilpressue() does what? Press on some oil? How can the Arduino press on some oil?

readOilPressureSensor() makes a lot more sense.

Good point paul, makes more sense that way.

aarg, this is how i currently send it.

Wire.beginTransmission(9); // transmit to device #9
Wire.write(oilpres); // sends five bytes
Wire.write(temprF); // sends one byte
Wire.endTransmission(); // stop transmitting

MurdockGT:
aarg, this is how i currently send it.

Wire.beginTransmission(9); // transmit to device #9

Wire.write(oilpres);        // sends five bytes
 Wire.write(temprF);              // sends one byte
 Wire.endTransmission();    // stop transmitting

but you have:

int oilpres;

Wire.write only accepts strings or bytes as a single parameter:reference. It's not doing what you think. I suspected as much.

Wire.write(oilpres); // sends five bytes

Five? There are no 5 byte data types on the Arduino.

As a side note, OP may want to consider putting the constant lookup tables into PROGMEM:

static const float array[12] PROGMEM = { ...

use 419 bytes of SRAM instead of 643....

PaulS:
Five? There are no 5 byte data types on the Arduino.

sorry, wasnt a direct copy from my code, i went from the arduino referance page and that was the comments there..