Need some help creating a function

My project is a controller for a camper. I am sensing the following:

sensorPin 0 is a Temperature sensor LM34 (10mv/degF)
sensorPin 1 is 9-15 Volts from the house battery through a 1/3 divider

Eventially these inputs will be applied through a relay that will also load the batteries during the voltage measurement and change between water, refridge, inside and outside temperatures and house and vehicle batteries and a water level sensor.

Hence the reason for Relay 1 & 2 currently commented out.

Here is what I am trying to do.

My sketch works and displays the temp and voltage while averaging the inputs, without the testRMtemp_and_BatVolts() function stuff.

I would like to assign all of this, including the global define's, as one Function called testRMtemp_and_BatVolts(). If that's possible.

Maybe later breaking it into two Functions one for the battery and one for temperature.

I am working on calling these Functions from a couple of momentary switch inputs and that's the need to assign this as a Function so I can test and call the desired Function.

Any help/improvements in my coding would be appreciated.

Thanx a bunch! :-/

  • Phil

// include the library code:
#include <LiquidCrystal.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(2,3,4,9,10,12,13);

// testRMtemp_and_BatVolts(){ **** is not working ****
int testRMtemp_and_BatVolts()
{
// int Relay1Pin = 5; // Relay1 connected to digital pin 5
// int Relay2Pin = 6; // Relay2 connected to digital pin 6

int sensorPin = 0; // select the input for the Thermometer
int sensorValue = 0; // store the sensor value
int sensorPin1 = 1; // select the input for the Battery Voltage
int sensorValue1 = 0; // variable to store the Battery sensor value
int i = 0; // initialized variable

void setup() {

// pinMode(Relay1Pin, OUTPUT); // Set Relay1 output
// pinMode(Relay2Pin, OUTPUT); // Set Relay2 output

// set up the LCD's number of rows and columns:
lcd.begin(16, 2); // Print a message to the LCD.
lcd.print("Room Temp"); // print Room Temp:
lcd.setCursor(15, 0); // position cursor for print:
lcd.print("F"); // display F for Farenhiet:
lcd.setCursor(0, 1); // position cursor for print:
lcd.print("Battery"); //// print Battery:
lcd.setCursor(15, 1); // position cursor for print
lcd.print("V"); // display V for Voltage

}

void loop()
{
// this section Averages readings for stability:
for (i=0; i<7; i++){
// read the value from the Temperature sensor:
sensorValue = sensorValue + analogRead(sensorPin);
// read the Battery Level:
sensorValue1 = sensorValue1 + analogRead(sensorPin1);
delay(400);}

sensorValue = sensorValue / 8; // Avg the Sensor Reading:
sensorValue1 = sensorValue1 / 8; // Avg Sensor1 Reading:

// set the cursor to column 11, line 0:
lcd.setCursor(11, 0); // print the sensor value:
lcd.print(sensorValue0.0048100,1); // calculate Temperature

// set the cursor to column 11, second row:
lcd.setCursor(11, 1); // print the sensor value:
lcd.print(((sensorValue1*0.004810)-0.1867)*3,2); // actual input voltage is / by 3
// senserValue1 includes a offset for calibration
}
}
// My Attempt to use the Function **********:
int sens;
sens = testRMtemp_and_BatVolts()

// digitalWrite(Relay1Pin, HIGH); // set the Relay1 on
// digitalWrite(Relay2Pin, HIGH); // set the Relay2 on

// set delay
// delay(2000);

The structure of your program needs to be something like this:

// Global variables

void setup()
{
   // Init stuff
}

void loop()
{
   // Loopy stuff
}

RetType MyFunctionName(ArgType1 myArg1, ArgType2 myArg2,...)
{
   // My function's code
}

If your function is to return an int, RetType is replaced by int. If it's name is to be testRMtemp_and_BatVolts, fine. If it takes no arguments, the function declaration would be:

int testRMtemp_and_BatVolts()
{
   // My function's code
}

Move whatever is in loop that belongs in the function into the function, and call the function, instead:

void loop()
{
   int sens = testRMtemp_and_BatVolts();
}

Thank you Paul,

However, I am now confused. I don't understand what RETYPE is or does. A very quick look at the reference and I didn't see anything about it.

Can you point me somewhere that I can study the command?

And Wow, that was a quick response. Thanx again! :wink:

RetType is a placeholder for the type of data that will be returned by your function when it finishes execution (or the 'void' type if noting is to be returned).

See: http://www.arduino.cc/en/Reference/FunctionDeclaration