Multiple Sketch Tab Issues.

One of the issues that I have is that my main tab Code works fine on my arduino it compiles well and downloads and runs just fine. I however cannot seem to get it to work with the second code. I know there are a lot of libraries there, but I am trying to create a default set of code for all of my projects here at school. So I can build and modify off them as I generally use a similar setup.

MAIN TAB CODE

#include <Wire.h>
#include <HID.h>
#include <ModbusMaster.h>
#include <Servo.h>
#include <SoftwareSerial.h>
#include <LiquidCrystal.h>
#include <SD.h>
#include <EEPROM.h>
#include <WiFi.h>
#include <WiFiClient.h>
#include <WiFiServer.h>
#include <WiFiUdp.h>
#include <Dhcp.h>
#include <Dns.h>
#include <Ethernet.h>
#include <EthernetClient.h>
#include <EthernetServer.h>
#include <EthernetUdp.h>
#include <Keyboard.h>
#include <DHT.h>

// --------------------------------------
// i2c_scanner
//
// Version 1
//    This program (or code that looks like it)
//    can be found in many places.
//    For example on the Arduino.cc forum.
//    The original author is not know.
// Version 2, Juni 2012, Using Arduino 1.0.1
//     Adapted to be as simple as possible by Arduino.cc user Krodal
// Version 3, Feb 26  2013
//    V3 by louarnold
// Version 4, March 3, 2013, Using Arduino 1.0.3
//    by Arduino.cc user Krodal.
//    Changes by louarnold removed.
//    Scanning addresses changed from 0...127 to 1...119,
//    according to the i2c scanner by Nick Gammon
//    http://www.gammon.com.au/forum/?id=10896
// Version 5, March 28, 2013
//    As version 4, but address scans now to 127.
//    A sensor seems to use address 120.
// Version 6, November 27, 2015.
//    Added waiting for the Leonardo serial communication.
// 
//
// This sketch tests the standard 7-bit addresses
// Devices with higher bit address might not be seen properly.
//

#include <Wire.h>


void setup()
{
  Wire.begin();

  Serial.begin(9600);
  while (!Serial);             // Leonardo: wait for serial monitor
  Serial.println("\nI2C Scanner");
}


void loop()
{
  byte error, address;
  int nDevices;

  Serial.println("Scanning...");

  nDevices = 0;
  for(address = 1; address < 127; address++ ) 
  {
    // The i2c_scanner uses the return value of
    // the Write.endTransmisstion to see if
    // a device did acknowledge to the address.
    Wire.beginTransmission(address);
    error = Wire.endTransmission();

    if (error == 0)
    {
      Serial.print("I2C device found at address 0x");
      if (address<16) 
        Serial.print("0");
      Serial.print(address,HEX);
      Serial.println("  !");

      nDevices++;
    }
    else if (error==4) 
    {
      Serial.print("Unknow error at address 0x");
      if (address<16) 
        Serial.print("0");
      Serial.println(address,HEX);
    }    
  }
  if (nDevices == 0)
    Serial.println("No I2C devices found\n");
  else
    Serial.println("done\n");

  delay(5000);           // wait 5 seconds for next scan
}

SECOND TAB CODE:

I am sure many of you recognize this code. What I would like to do is to have these two tabs running as default program.

// Simple I2C protocol v0.2 for Arduino
// Master side program 
// (c) 2014 Ignas Gramba
//
#include <Wire.h>

byte DataPacket[11];
byte DataPacket2[12];// ADDED BY JP
byte command_nr;
int a, b, c, d, e; // arguments
byte SlaveDeviceId = (1);// CHANGED FROM 1 to WHATS SHOWN JP
void setup()
{
  Wire.begin();        // join i2c bus (address optional for master)
  Serial.begin(9600);  // start serial for output
  
  command_nr = 2;      // initialise test values
  a = 105;
  b = 2350;
  c = 4587;
  d = 12587;
  e = 12;  
}

void loop()
{
  makeDataPacket(command_nr, a, b, c, d, e);
  sendDataPacket();
 
  int response = receiveResponse();
  Serial.print("Slave response: ");
  Serial.println(response);

  while(1); // Stop
  while(2); // Stop ADDED EXTRA JP
}

void makeDataPacket(byte command, int aa, int bb, int cc, int dd, int ee){
  DataPacket[0] = command;
  DataPacket[1] = aa >>  8;
  DataPacket[2] = aa & 255;
  DataPacket[3] = bb >>  8;
  DataPacket[4] = bb & 255;
  DataPacket[5] = cc >>  8;
  DataPacket[6] = cc & 255;
  DataPacket[7] = dd >>  8;
  DataPacket[8] = dd & 255;
  DataPacket[9] = ee >>  8;
  DataPacket[10]= ee & 255;
}

void sendDataPacket(){
  Wire.beginTransmission(SlaveDeviceId);
  Wire.write(DataPacket, 11);
    delay(10);
}// ADDED BY JP

int receiveResponse(){
 int receivedValue;
  int available = Wire.requestFrom(SlaveDeviceId, (byte)2);
  if(available == 2)
  {
    receivedValue = Wire.read() << 8 | Wire.read(); // combine two bytes into integer
  }
  else
  {
    Serial.print("ERROR: Unexpected number of bytes received - ");
    Serial.println(available);
  }
  Wire.endTransmission(true);
  return receivedValue;  
}

**I would like someone to verify where I am exactly going wrong in each of the tabs. I want to be able to add tabs as I go when I find a sketch that works well, I would like to be able to add it into a default sketch as a tab. **
But I am making what I think are rookie arduino errors.

ERROR MESSAGES

I2CMASTER:12: error: redefinition of 'void setup()'

 void setup()

      ^

I2CMASTER:60: error: 'void setup()' previously defined here

   int available = Wire.requestFrom(SlaveDeviceId, (byte)2);

      ^

C:\Users\jjones\Desktop\TEmplate\I2CMASTER.ino: In function 'void loop()':

I2CMASTER:25: error: redefinition of 'void loop()'

 void loop()

      ^

I2CMASTER:70: error: 'void loop()' previously defined here

   Wire.endTransmission(true);

      ^

Using library Wire at version 1.0 in folder: C:\Program Files (x86)\Arduino\hardware\arduino\avr\libraries\Wire 
Using library HID at version 1.0 in folder: C:\Program Files (x86)\Arduino\hardware\arduino\avr\libraries\HID 
Using library ModbusMaster at version 0.11.0 in folder: C:\Users\jjones\Documents\Arduino\libraries\ModbusMaster 
Using library Servo at version 1.1.1 in folder: C:\Program Files (x86)\Arduino\libraries\Servo 
Using library SoftwareSerial at version 1.0 in folder: C:\Program Files (x86)\Arduino\hardware\arduino\avr\libraries\SoftwareSerial 
Using library LiquidCrystal at version 1.0.4 in folder: C:\Program Files (x86)\Arduino\libraries\LiquidCrystal 
Using library SD at version 1.0.6 in folder: C:\Program Files (x86)\Arduino\libraries\SD 
Using library EEPROM at version 2.0 in folder: C:\Program Files (x86)\Arduino\hardware\arduino\avr\libraries\EEPROM 
Using library WiFi at version 1.2.6 in folder: C:\Program Files (x86)\Arduino\libraries\WiFi 
Using library Ethernet at version 1.1.1 in folder: C:\Program Files (x86)\Arduino\libraries\Ethernet 
Using library Keyboard at version 1.0.0 in folder: C:\Program Files (x86)\Arduino\libraries\Keyboard 
Using library DHT_sensor_library at version 1.2.3 in folder: C:\Users\jjones\Documents\Arduino\libraries\DHT_sensor_library 
Using library SPI at version 1.0 in folder: C:\Program Files (x86)\Arduino\hardware\arduino\avr\libraries\SPI 
exit status 1
redefinition of 'void setup()'

I would really like some help in determining exactly what I am doing wrong so I can fix it on these two tabs, and then make it work when adding more tabs to this program.

I have a lot of experience with Automation, Ladder Logic, of various brands, but this Arduino is not only a wonderful platform its a bit disconcerting, because I am sure these are pretty much rookie errors, but I would like to get his up and running ASAP.

I believe I have to fix areas of void setup() and void loop() and I am sure that I have the { } incorrectly positioned, but I am just not sure how to correct these problems.

Any help is appreciated.

The thing about multiple tabs is that the IDE will combine them all together when you hit compile or upload. So you should think of them as all being on one long program. It would appear as if you may have the setup and loop functions defined on both tabs and when it gets put together into one long program that means you have setup and loop defined twice.

If the tabs are part of the same program you're OK. But if they are part of two separate programs then they should be separate sketches not tabs on the same sketch.

Vaclav: Check your PM

This is a FORUM, Vaclav... not a secret society; you should POST your recommendations.

a place on the internet where people can leave messages or discuss particular subjects with other people at the same time

Your recommendations ought to survive scrutiny... lest folks fall off the edge of the world... just beyond the dragons.

Both tabs define a setup()? You want to have two different sketches just somehow jammed together and uploaded to the Arduino?

It doesn't really work that way. You can't get the load-hauling power of a truck with the fuel economy of a Prius by stuffing both sets of blueprints into a car-building robot.

HAving said that:

rename setup and loop in your first sketch to setup1() and loop1(). rename setup and loop in your second sketch to setup2() and loop2().

Add a new setup and loop like this:

void setup() {
  setup1();
  setup2();
}

void loop() {
  loop1();
  loop2();
}

It might compile. But there's an excellent chance it wont work.

Thanks Everyone,

I was just wondering if i could eliminate anything from the second tabs, to that it would actually run and compile correctly?

I am probably not explaining it correctly...

3ECO:
I was just wondering if i could eliminate anything from the second tabs, to that it would actually run and compile correctly?

Yes, remove setup and loop functions from one tab or the other.

Thanks DELTA_G I was reading further in the forum something about the IDE processes the tabs in Alphabetical Order? Is this correct? I know there are a lot of you on here much more experienced with the IDE than I am and I apologize if these seem like rudimentary questions in advance.

I am used to with PLC programming of designing a sub-routine, then calling for that sub-routine with an JSR or similar type command.

For those of you who do understand that, is there an equivalent function with the Arduino IDE that allows for that and if so would I have to use a different extension for the "sub-routine" sketch?

Thank you again in advance.