Go Down

Topic: How do i combine two Arduino sketches? (Read 5103 times) previous topic - next topic

Poosnboos

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?

Can someone please help? :)
Thank you!! Appreciate much!! :)




Grumpy_Mike

#1
Nov 16, 2010, 06:02 pm Last Edit: Nov 16, 2010, 06:05 pm by Grumpy_Mike Reason: 1
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);

Poosnboos

Thanks for the reply. :)

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


Grumpy_Mike

#3
Nov 16, 2010, 08:10 pm Last Edit: Nov 16, 2010, 08:10 pm by Grumpy_Mike Reason: 1
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 temperature reading is 24
The voltage is 2.3
The temperature reading is 24 ....

lloyddean

#4
Nov 16, 2010, 09:59 pm Last Edit: Nov 17, 2010, 04:54 pm by lloyddean Reason: 1
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);
}

Poosnboos

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


lloyddean

#6
Nov 17, 2010, 09:11 am Last Edit: Nov 17, 2010, 09:15 am by lloyddean Reason: 1
It should I just rearranged what you had.  It was typed in while waiting someplace but wasn't compiled or tested!

AWOL

#7
Nov 17, 2010, 09:26 am Last Edit: Nov 17, 2010, 09:28 am by AWOL Reason: 1
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.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

lloyddean

#8
Nov 17, 2010, 05:01 pm Last Edit: Nov 17, 2010, 08:10 pm by lloyddean Reason: 1
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.

Go Up