# How do i combine two Arduino sketches?

First Arduino sketch:

void setup( ) { Serial.begin (9600); } void loop( ) { float voltage = 0; int sensorValue = analogRead (A0); 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() { val = analogRead(SENSOR); 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?

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);

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; int sensorValue = analogRead (A0); voltage = sensorValue * 0.0048; // code width(sensitivity) = 5 / 210 = 4.8mV Serial.print("The voltage is "); Serial.println (voltage, DEC); val = analogRead(SENSOR); 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.print("The temperature reading is "); Serial.println(celcius); }

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

Do u mean:.....

Yes ;)

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.

How will the serial monitor looked like

The voltage is 2.3 The temperature reading is 24 The voltage is 2.3 The temperature reading is 24 ....

Or can be re-factored into something more modular, maintainable and easier to read along the lines of -

``````enum {
sensorTHERMOMETER =  0    // LM335A
, sensorVOLTAGE     = A0
};

{
// 5 / 210 = 4.8mV
const float scaleVOLTAGE = 0.0048f;
}

{
const float scaleTHERMAL = 0.00489f;
}

void loop()
{
// --- voltage

Serial.print("The voltage is ");
Serial.println(voltage);

// --- temperature

float   kelvin  = readTemperature() * 100.0f;
float   celsius = kelvin - 273.15f;

Serial.println(celsius);
}

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

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 :)

It should I just rearranged what you had. It was typed in while waiting someplace but wasn't compiled or tested!

but wasn't compiled or tested!

No, it wasn't!

``````return analogRead(sensorTHERMOMETER * scaleTHERMAL[glow])[/glow];
``````

:o

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

`````` 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.

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.