Need some help creating a function
my project controller camper. sensing following:
sensorpin 0 is temperature sensor lm34 (10mv/degf)
sensorpin 1 is 9-15 volts house battery through 1/3 divider
eventially these inputs applied through relay load batteries during voltage measurement , change between water, refridge, inside , outside temperatures , house , vehicle batteries , water level sensor.
hence reason relay 1 & 2 commented out.
here trying do.
my sketch works , displays temp , voltage while averaging inputs, without testrmtemp_and_batvolts() function stuff.
i assign of this, including global define's, 1 function called testrmtemp_and_batvolts(). if that's possible.
maybe later breaking 2 functions 1 battery , 1 temperature.
i working on calling these functions couple of momentary switch inputs , that's need assign function can test , call desired function.
any help/improvements in coding appreciated.
thanx bunch! :-/
- phil
// include library code:
#include <liquidcrystal.h>
// initialize library numbers of interface pins
liquidcrystal lcd(2,3,4,9,10,12,13);
// testrmtemp_and_batvolts(){ **** not working ****
int testrmtemp_and_batvolts()
{
// int relay1pin = 5; // relay1 connected digital pin 5
// int relay2pin = 6; // relay2 connected digital pin 6
int sensorpin = 0; // select input thermometer
int sensorvalue = 0; // store sensor value
int sensorpin1 = 1; // select input battery voltage
int sensorvalue1 = 0; // variable store battery sensor value
int = 0; // initialized variable
void setup() {
// pinmode(relay1pin, output); // set relay1 output
// pinmode(relay2pin, output); // set relay2 output
// set lcd's number of rows , columns:
lcd.begin(16, 2); // print message lcd.
lcd.print("room temp"); // print room temp:
lcd.setcursor(15, 0); // position cursor print:
lcd.print("f"); // display f farenhiet:
lcd.setcursor(0, 1); // position cursor print:
lcd.print("battery"); //// print battery:
lcd.setcursor(15, 1); // position cursor print
lcd.print("v"); // display v voltage
}
void loop()
{
// section averages readings stability:
for (i=0; i<7; i++){
// read value temperature sensor:
sensorvalue = sensorvalue + analogread(sensorpin);
// read battery level:
sensorvalue1 = sensorvalue1 + analogread(sensorpin1);
delay(400);}
sensorvalue = sensorvalue / 8; // avg sensor reading:
sensorvalue1 = sensorvalue1 / 8; // avg sensor1 reading:
// set cursor column 11, line 0:
lcd.setcursor(11, 0); // print sensor value:
lcd.print(sensorvalue*0.0048*100,1); // calculate temperature
// set cursor column 11, second row:
lcd.setcursor(11, 1); // print sensor value:
lcd.print(((sensorvalue1*0.004810)-0.1867)*3,2); // actual input voltage / 3
// senservalue1 includes offset calibration
}
}
// my attempt use function **********:
int sens;
sens = testrmtemp_and_batvolts()
// digitalwrite(relay1pin, high); // set relay1 on
// digitalwrite(relay2pin, high); // set relay2 on
// set delay
// delay(2000);
sensorpin 0 is temperature sensor lm34 (10mv/degf)
sensorpin 1 is 9-15 volts house battery through 1/3 divider
eventially these inputs applied through relay load batteries during voltage measurement , change between water, refridge, inside , outside temperatures , house , vehicle batteries , water level sensor.
hence reason relay 1 & 2 commented out.
here trying do.
my sketch works , displays temp , voltage while averaging inputs, without testrmtemp_and_batvolts() function stuff.
i assign of this, including global define's, 1 function called testrmtemp_and_batvolts(). if that's possible.
maybe later breaking 2 functions 1 battery , 1 temperature.
i working on calling these functions couple of momentary switch inputs , that's need assign function can test , call desired function.
any help/improvements in coding appreciated.
thanx bunch! :-/
- phil
// include library code:
#include <liquidcrystal.h>
// initialize library numbers of interface pins
liquidcrystal lcd(2,3,4,9,10,12,13);
// testrmtemp_and_batvolts(){ **** not working ****
int testrmtemp_and_batvolts()
{
// int relay1pin = 5; // relay1 connected digital pin 5
// int relay2pin = 6; // relay2 connected digital pin 6
int sensorpin = 0; // select input thermometer
int sensorvalue = 0; // store sensor value
int sensorpin1 = 1; // select input battery voltage
int sensorvalue1 = 0; // variable store battery sensor value
int = 0; // initialized variable
void setup() {
// pinmode(relay1pin, output); // set relay1 output
// pinmode(relay2pin, output); // set relay2 output
// set lcd's number of rows , columns:
lcd.begin(16, 2); // print message lcd.
lcd.print("room temp"); // print room temp:
lcd.setcursor(15, 0); // position cursor print:
lcd.print("f"); // display f farenhiet:
lcd.setcursor(0, 1); // position cursor print:
lcd.print("battery"); //// print battery:
lcd.setcursor(15, 1); // position cursor print
lcd.print("v"); // display v voltage
}
void loop()
{
// section averages readings stability:
for (i=0; i<7; i++){
// read value temperature sensor:
sensorvalue = sensorvalue + analogread(sensorpin);
// read battery level:
sensorvalue1 = sensorvalue1 + analogread(sensorpin1);
delay(400);}
sensorvalue = sensorvalue / 8; // avg sensor reading:
sensorvalue1 = sensorvalue1 / 8; // avg sensor1 reading:
// set cursor column 11, line 0:
lcd.setcursor(11, 0); // print sensor value:
lcd.print(sensorvalue*0.0048*100,1); // calculate temperature
// set cursor column 11, second row:
lcd.setcursor(11, 1); // print sensor value:
lcd.print(((sensorvalue1*0.004810)-0.1867)*3,2); // actual input voltage / 3
// senservalue1 includes offset calibration
}
}
// my attempt use function **********:
int sens;
sens = testrmtemp_and_batvolts()
// digitalwrite(relay1pin, high); // set relay1 on
// digitalwrite(relay2pin, high); // set relay2 on
// set delay
// delay(2000);
the structure of program needs this:
if function return int, rettype replaced int. if it's name testrmtemp_and_batvolts, fine. if takes no arguments, function declaration be:
move whatever in loop belongs in function function, , call function, instead:
code: [select]
// global variables
void setup()
{
// init stuff
}
void loop()
{
// loopy stuff
}
rettype myfunctionname(argtype1 myarg1, argtype2 myarg2,...)
{
// function's code
}if function return int, rettype replaced int. if it's name testrmtemp_and_batvolts, fine. if takes no arguments, function declaration be:
code: [select]
int testrmtemp_and_batvolts()
{
// function's code
}move whatever in loop belongs in function function, , call function, instead:
code: [select]
void loop()
{
int sens = testrmtemp_and_batvolts();
}
Arduino Forum > Forum 2005-2010 (read only) > Software > Syntax & Programs > Need some help creating a function
arduino
Comments
Post a Comment