How to Merge or Combine Code with an Arduino

hi guys!i am new in arduino project.i have to compine to two Arduino sketches in order to take a value from a hall effect sensor in my lcd and in the serial screen on arduino software…
the 1 sketch is:
#include “LiquidCrystal.h”

float refVoltage = 5.0 / 1024; // volts per step on ADC
float sensorVolts;
int val;

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup() {
// set up the LCD’s number of columns and rows:
lcd.begin(16, 2); // 16 char by 2 lines
lcd.noCursor(); // Hides the LCD cursor.
}

void loop() {

// read AD0
val = analogRead(0);
sensorVolts = refVoltage * val;

// set the cursor to column 0, line 1
// (note: line 1 is the second row, since counting begins with 0):
lcd.setCursor(0, 0);
lcd.print(“Hall Volts =”);

lcd.setCursor(0, 1);
lcd.print(sensorVolts);
delay(500); // 500 mSec

} // end loop

and the second is:
#define XRANGE 50
int x,gss;
void setup(){
Serial.begin(9600);
}
void loop(){
int aValue =analogRead(0);
x = map(aValue, 0, 1024, 0, XRANGE);
gss = map(aValue, 102, 922, -640, 640);
Serial.print("|");
for (int i=0;i<x;i++){
if(i==XRANGE/2-1)Serial.print("|");
else Serial.print("-");
}
Serial.print(“O”);
for (int i=x+1;i<XRANGE;i++){
if(i==XRANGE/2-1)Serial.print("|");
else Serial.print("-");
}
Serial.print("|");
Serial.print(gss);
Serial.println(“Gauss”);
delay(8000);
}

if i put the second in the first and i compile them i take this message
values_for_gauss_on_screen.ino: In function ‘void setup()’:
values_for_gauss_on_screen:35: error: redefinition of ‘void setup()’
values_for_gauss_on_screen:3: error: ‘void setup()’ previously defined here
values_for_gauss_on_screen.ino: In function ‘void loop()’:
values_for_gauss_on_screen:41: error: redefinition of ‘void loop()’
values_for_gauss_on_screen:6: error: ‘void loop()’ previously defined here

so i dont know what to do… please help me!

so i dont know what to do.

Read this

so i dont know what to do… please help me!

That’s hard to believe. The compiler is telling you that you have two setup() functions, and that that is not allowed. So, don’t.

The compiler is telling you that you have two loop() functions, and that that is not allowed. So, don’t.

The BEST way to combine two sketches is to understand EXACTLY what each is doing, and then throw them away, and write a new sketch that does what you want, borrowing pieces from the original sketches (before you throw them away).

You could declare/create a function in the 1st code to call upon the 2nd code:

#include "LiquidCrystal.h"

float refVoltage = 5.0 / 1024; // volts per step on ADC
float sensorVolts;
int val;
#define XRANGE  50 
int x,gss;
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup() {
  // set up the LCD's number of columns and rows: 
  lcd.begin(16, 2);  // 16 char by 2 lines
  lcd.noCursor(); // Hides the LCD cursor.
}

void loop() {
  // read AD0
  val = analogRead(0);
  sensorVolts = refVoltage * val;
  // set the cursor to column 0, line 1
  // (note: line 1 is the second row, since counting begins with 0):
  lcd.setCursor(0, 0);
  lcd.print("Hall Volts ="); 

  lcd.setCursor(0, 1);
  lcd.print(sensorVolts); 
  
  output(); //Calling/running the output function
  
  delay(500); // 500 mSec
}

void output(){ //Creating the output function
  int aValue =analogRead(0);
  x = map(aValue, 0, 1024, 0, XRANGE);
  gss = map(aValue, 102, 922, -640, 640);
  Serial.print("|"); 
  for (int i=0;i<x;i++){
    if(i==XRANGE/2-1)Serial.print("|");
    else Serial.print("-");
  }
  Serial.print("O");
  for (int i=x+1;i<XRANGE;i++){
    if(i==XRANGE/2-1)Serial.print("|");
    else Serial.print("-");
  }
  Serial.print("|");
  Serial.print(gss);
  Serial.println("Gauss");
  delay(8000); 
}

Compiles fine, test it and see what happens. I would also strongly suggest to avoid delay() and learn/understand the BlinkWithoutDelay code.

  • Luke

rep8:
You could declare/create a function in the 1st code to call upon the 2nd code:

#include "LiquidCrystal.h"

float refVoltage = 5.0 / 1024; // volts per step on ADC
float sensorVolts;
int val;
#define XRANGE  50
int x,gss;
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup() {
  // set up the LCD’s number of columns and rows:
  lcd.begin(16, 2);  // 16 char by 2 lines
  lcd.noCursor(); // Hides the LCD cursor.
}

void loop() {
  // read AD0
  val = analogRead(0);
  sensorVolts = refVoltage * val;
  // set the cursor to column 0, line 1
  // (note: line 1 is the second row, since counting begins with 0):
  lcd.setCursor(0, 0);
  lcd.print(“Hall Volts =”);

lcd.setCursor(0, 1);
  lcd.print(sensorVolts);
 
  output(); //Calling/running the output function
 
  delay(500); // 500 mSec
}

void output(){ //Creating the output function
  int aValue =analogRead(0);
  x = map(aValue, 0, 1024, 0, XRANGE);
  gss = map(aValue, 102, 922, -640, 640);
  Serial.print("|");
  for (int i=0;i<x;i++){
    if(i==XRANGE/2-1)Serial.print("|");
    else Serial.print("-");
  }
  Serial.print(“O”);
  for (int i=x+1;i<XRANGE;i++){
    if(i==XRANGE/2-1)Serial.print("|");
    else Serial.print("-");
  }
  Serial.print("|");
  Serial.print(gss);
  Serial.println(“Gauss”);
  delay(8000);
}




Compiles fine, test it and see what happens. I would also strongly suggest to avoid delay() and learn/understand the BlinkWithoutDelay code. 

- Luke

thanks you a lot!!you help me a lot!! i understand that i have to make new fuctions…guys thanks all for your help!

Read this http://www.thebox.myzen.co.uk/Tutorial/Merging_Code.html

eldimious: thanks you a lot!!you help me a lot!! i understand that i have to make new fuctions...guys thanks all for your help!

No worries buddy, you don't [u]need[/u] to create new functions. It is just a quick and easy way to achieve what you needed, it does have advantages/disadvantages at times. Glad it is working, good luck with what comes in the future.

  • Luke

It is just a quick and easy way to achieve what you needed, it does have advantages/disadvantages at times.

The main advantage is that the code to perform a specific function is isolated, and can be thoroughly tested. Then, when you see:

doSomethingSpecificThatHasBeenThoroughlyTested();

in your code, you don't have to worry about that function being a source of problems.

Assuming, of course, that doSomethingSpecificThatHasBeenThoroughlyTested() has been thoroughly tested.