# Arduino Forum

## Forum 2005-2010 (read only) => Software => Syntax & Programs => Topic started by: guang on Nov 16, 2010, 05:48 pm

Title: How do i combine two Arduino sketches?
Post by: guang on Nov 16, 2010, 05:48 pm
First Arduino sketch:

void setup( )
{
Serial.begin (9600);
}
void loop( )
{
float voltage = 0;
voltage = sensorValue * 0.0048; // code width(sensitivity) = 5 / 210  = 4.8mV
Serial.println (voltage, DEC);
delay (1000);
}

This program will print the voltage(in decimal) on the serial monitor after i have connected some circuitry on a breadboard.

Second Arduino sketch:

#define SENSOR 0 // select the input pin for
// the LM335A temperature sensor

float val = 0;
float val2 = 0;
float deg = 0;
float celcius = 0;

void setup()
{
Serial.begin(9600);
}

void loop()
{
val2 = val * 0.00489; // take SENSOR value and multiply it by 4.89mV
// and store value to val2
deg = val2 * 100; // multiply by 100 to get degrees in K
celcius = deg - 273.15; // subtract absolute zero to get degrees celcius

Serial.println(celcius);

delay(1000);
}

This program will print the temperature reading in degree celcius on the serial monitor.

I have tested both programs individually and it worked.
I was wondering if if i can combine both programs together.

However, there is only one serial monitor. So how do i do it?
How should i write my program then?

Thank you!! Appreciate much!! :)

Title: Re: How do i combine two Arduino sketches?
Post by: Grumpy_Mike on Nov 16, 2010, 06:02 pm
Just put them together.
The setup is the same so just have one.

Put the code in one loop to directly follow the code in the other, there are no variable names in common so you should have no problems, you might want to remove a delay however.
Just put some print statements in front of the print values to identify what the number refers to.
So change:-
voltage = sensorValue * 0.0048; // code width(sensitivity) = 5 / 210  = 4.8mV
Serial.println (voltage, DEC);
to:-
voltage = sensorValue * 0.0048; // code width(sensitivity) = 5 / 210  = 4.8mV
Serial.print("The voltage is ");
Serial.println (voltage, DEC);
Title: Re: How do i combine two Arduino sketches?
Post by: guang on Nov 16, 2010, 08:00 pm

Do u mean:

#define SENSOR 0 // select the input pin for
// the LM335A temperature sensor

float val = 0;
float val2 = 0;
float deg = 0;
float celcius = 0;

void setup( )
{
Serial.begin (9600);
}
void loop( )
{
float voltage = 0;
voltage = sensorValue * 0.0048; // code width(sensitivity) = 5 / 210  = 4.8mV
Serial.print("The voltage is ");
Serial.println (voltage, DEC);
val2 = val * 0.00489; // take SENSOR value and multiply it by 4.89mV
// and store value to val2
deg = val2 * 100; // multiply by 100 to get degrees in K
celcius = deg - 273.15; // subtract absolute zero to get degrees celcius
Serial.println(celcius);
}

Why do u not need the delay anymore?
How will the serial monitor looked like? :)

Title: Re: How do i combine two Arduino sketches?
Post by: Grumpy_Mike on Nov 16, 2010, 08:10 pm
Quote
Do u mean:.....

Yes  ;)

Quote
Why do u not need the delay anymore

You never did but if you want slow it down put one delay in not two, that's what I meant.

Quote
How will the serial monitor looked like

The voltage is 2.3
The voltage is 2.3
The temperature reading is 24 ....
Title: Re: How do i combine two Arduino sketches?
Post by: lloyddean on Nov 16, 2010, 09:59 pm
Or can be re-factored  into something more modular, maintainable and easier to read along the lines of -

Code: [Select]
`enum {     sensorTHERMOMETER =  0    // LM335A   , sensorVOLTAGE     = A0};float readVoltage(){    // 5 / 210 = 4.8mV    const float scaleVOLTAGE = 0.0048f;    return analogRead(sensorVOLTAGE) * scaleVOLTAGE;}float readTemperature(){    const float scaleTHERMAL = 0.00489f;    return analogRead(sensorTHERMOMETER) * scaleTHERMAL;}void loop(){    // --- voltage    float   voltage = readVoltage();    Serial.print("The voltage is ");    Serial.println(voltage);    // --- temperature    float   kelvin  = readTemperature() * 100.0f;    float   celsius = kelvin - 273.15f;    Serial.print("The temperature reading is ");    Serial.println(celsius); }void setup() {    Serial.begin(9600);}`
Title: Re: How do i combine two Arduino sketches?
Post by: guang on Nov 17, 2010, 08:47 am
Thank you Grumpy_Mike. I appreciate your prompt replies to my posts.
Thank you lloyddean for making the effort to alter my disorganised program.

Anyway, if i put a delay into my program, do i put it right at the end?

Also, the altered program that lloyddean posted....does it function the same way as my old program? :)

Thank you guys :)

Title: Re: How do i combine two Arduino sketches?
Post by: lloyddean on Nov 17, 2010, 09:11 am
It should I just rearranged what you had.  It was typed in while waiting someplace but wasn't compiled or tested!
Title: Re: How do i combine two Arduino sketches?
Post by: AWOL on Nov 17, 2010, 09:26 am
Quote
but wasn't compiled or tested!

No, it wasn't!
Code: [Select]
`return analogRead(sensorTHERMOMETER * scaleTHERMAL[glow])[/glow];`  :o

Don't you need to move one of the sensor pins?

Code: [Select]
` float   voltage = readVoltage();    Serial.print("The voltage is ");    Serial.println(voltage, DEC);`

Trying to print a float to ten decimal places is a waste of time.
Title: Re: How do i combine two Arduino sketches?
Post by: lloyddean on Nov 17, 2010, 05:01 pm
Code corrected above!

That was done at someone else computer which seems to have some cut/copy/paste buffer bug, or key failure, as the e-mail to myself has the pin assignment correct.

As to the DEC in the print statement, as I said the code is a rearrangement of that in the first posting above in which you'll find the same problem.

EDIT:
Oh, and AWOL thanks for noticing that and bringing it to my attention.