Pages: [1]   Go Down
Author Topic: How do i combine two Arduino sketches?  (Read 4265 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 58
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

 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? smiley
Thank you!! Appreciate much!! smiley



Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 604
Posts: 33439
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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);
« Last Edit: November 16, 2010, 12:05:38 pm by Grumpy_Mike » Logged

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 58
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks for the reply. smiley

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? smiley

Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 604
Posts: 33439
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Do u mean:.....
Yes  smiley-wink

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 ....
« Last Edit: November 16, 2010, 02:10:44 pm by Grumpy_Mike » Logged

Des Moines, WA - USA
Offline Offline
God Member
*****
Karma: 25
Posts: 779
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
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);
}
« Last Edit: November 17, 2010, 10:54:13 am by lloyddean » Logged

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 58
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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? smiley

Thank you guys smiley

Logged

Des Moines, WA - USA
Offline Offline
God Member
*****
Karma: 25
Posts: 779
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

It should I just rearranged what you had.  It was typed in while waiting someplace but wasn't compiled or tested!
« Last Edit: November 17, 2010, 03:15:33 am by lloyddean » Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 291
Posts: 25884
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
but wasn't compiled or tested!

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

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

Code:
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.
« Last Edit: November 17, 2010, 03:28:34 am by AWOL » Logged

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

Des Moines, WA - USA
Offline Offline
God Member
*****
Karma: 25
Posts: 779
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
« Last Edit: November 17, 2010, 02:10:28 pm by lloyddean » Logged

Pages: [1]   Go Up
Jump to: