merging two program in arduino

i have merged two program both of them are working good in individual

void setup(){    //100hz clock signal
pinMode (2, OUTPUT);
}
void loop(){
digitalWrite (2,  HIGH);
delay(1);
digitalWrite (2, LOW);
delay(9);
}



float val1 = 0;// variable to store the value coming from the sensor
float Bit1=1023; //set value for amount of bits
float volt1=0; //set value to store calculated voltage
void main() 
{

const int Sensor1Pin = A0;
int Sensor1Data;
pinMode(Sensor1Pin,INPUT); // Sensor(s)
  Serial.begin(9600);  // for debugging
 
}

void loop() 
{
  
 // Read and store Sensor 1 data
  val1 = analogRead(Sensor1Pin);
  volt1= val1*(5.0/Bit1); //equation to convert incoming value to voltage
  Sensor1Data=volt1;
  
  
  Serial.print("Sensor 1:");
  Serial.println(Sensor1Data);
  delay(500);
  
  
 
}

errors which i faced

test_6_100hz:2: error: ‘::main’ must return ‘int’
test_6_100hz:1: error: ‘::main’ must return ‘int’
test_6_100hz.ino: In function ‘int main()’:
test_6_100hz:3: error: a function-definition is not allowed here before ‘{’ token
test_6_100hz:6: error: a function-definition is not allowed here before ‘{’ token
test_6_100hz.ino: In function ‘void loop()’:
test_6_100hz:36: error: ‘val1’ was not declared in this scope
test_6_100hz:36: error: ‘Sensor1Pin’ was not declared in this scope
test_6_100hz:37: error: ‘volt1’ was not declared in this scope
test_6_100hz:37: error: ‘Bit1’ was not declared in this scope
test_6_100hz:38: error: ‘Sensor1Data’ was not declared in this scope
‘::main’ must return ‘int’

sorry i am new to programming i dont know what maybe the problem

test_6_100hz.ino (736 Bytes)

I suspect the function you have named "main" should actually be named "setup".

You should not have a function called "main" in an Arduino program because it will duplicate the main() function that the Arduino IDE adds in the background.

...R Simple Merge Demo

void setup(){    //100hz clock signal
pinMode (2, OUTPUT);
}
void loop(){
digitalWrite (2,  HIGH);
delay(1);
digitalWrite (2, LOW);
delay(9);
float val1 = 0;// variable to store the value coming from the sensor
float Bit1=1023; //set value for amount of bits
float volt1=0; //set value to store calculated voltage

const int Sensor1Pin = A0;
int Sensor1Data;
pinMode(Sensor1Pin,INPUT); // Sensor(s)
  Serial.begin(9600);  // for debugging

void loop() 


  val1 = analogRead(Sensor1Pin); // Read and store Sensor 1 data
  volt1= val1*(5.0/Bit1); //equation to convert incoming value to voltage
  Sensor1Data=volt1;
  
  Serial.print("Sensor 1:");
  Serial.println(Sensor1Data);
  delay(500);
 }

changed but now i got another problem

test_6_100hz.ino: In function 'void loop()':
test_6_100hz:21: error: expected initializer before 'val1'
expected initializer before 'val1'

Just like you can't have two "main"s, you can't have two "loop"s. you also can't have executable code outside a function, which is what happens after the second "void loop"

AWOL: Just like you can't have two "main"s, you can't have two "loop"s. you also can't have executable code outside a function, which is what happens after the second "void loop"

sorry sir i am new to programming could you please correct my mistakes and resend the code

Merging codes,

What is in setup() for each must be in setup() of the merged code.
What is in loop() for each must be in loop() of merged code.
Global variables for both should be declared above setup().

The hard parts come when the code from one sketch conflicts with the other.

Do yourself a big favor and go to this link

Read carefully to fill in the knowledge gaps you have in Basics and Programming. Going through the Microcontrollers section would also be good and save the rest for later.

akmsr: sorry sir i am new to programming could you please correct my mistakes and resend the code

Did you read the link I gave you in Reply #1 ?

...R

Try this.

Take your two other sketches. Rename setup() and loop() to setupA() and loopA() in one, and setupB() and loopB() in the other. If any of the other variables or functions in the two different sketches use the same name, then rename thins so they don't. If either of the two sketches use the same pins, well - you'll have to work out what to do and that depends on what the sketches actually are.

Then make a sketch with all the #includes from both the other two, all the code, and this:

void setup() {
  setupA();
  setupB();
}

void loop() {
  loopA();
  loopB();
}

It probably won't work. If either of the two other sketches use a delay(), or if either of the other two sketches have a for() or while() loop in their loop() functions, it almost definitely won't work.

But it might.