Combining 2 programs into 1

Hey
I have a program which is TCP/IP client which can send and receive messages to the industrial smart camera. The program is going to work like that:
1.User turns on the device ( will add screen later, now working on serial)
2. User types in basic specs like IP adress and TCP PORT number
3. User chooses to just listen or send messages and listen for a respond from the server
a) just litening in a loop
b) sending message ( typed in by user) and getting respond from the server in a loop

Hardware:
Arduino DUE
ENC28J60
OMRON FQ2 Smart Camera
Router working as a network switch

For now I have 2 programs.
Main in which I choose 3a or 3b option and it works but the message is in a program (without typing it in )
Second one is a program in which I can type words by USB keyboard and i print these words (commands) in a serial.

Now i want to combine them so add the keyboard functionality into to the main program.
I tried a few options but it did not work ( just pasting the lines in a main program).
I want to type in from keyboard:
Ip and TCP port number
Message (command)

Here is the 1st program:

#include <EthernetENC.h>
#include <SPI.h>
char command = 0;
char incomingByte = 0;
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress server(10, 5, 5, 10); // Server ip
IPAddress ip(10, 5, 5, 109); // Setting arduino uip
EthernetClient client;

void setup() {
  Ethernet.init(10);
  // Open serial communications and wait for port to open:
  Serial.begin(115200);
  // start the Ethernet connection:
  Ethernet.begin(mac, ip);
  // give the Ethernet shield a second to initialize:
  delay(1000);
  Serial.println("connecting...");
  // if you get a connection, report back via serial:
  if (client.connect(server, 9876)) {
    Serial.println("connected");
  }
  else {
    // if you didn't get a connection to the server:
    Serial.println("connection failed");
  }
  Serial.println("Press a to listen ");
  Serial.println("Press b to send commands");
}

void loop() {
  Ethernet.maintain();
  // Read serial input:
  if (Serial.available() )
  {
    incomingByte = Serial.read();
  }
  else
  {
    incomingByte = 0;
  }

  switch (incomingByte) {
    //CASE a
    case 'a':
      if (client.available())   {
        char c = client.read();
        Serial.print(c);
      }

      break;

    //CASE b
    case 'b':
      delay(100);
      //
      while (Serial.available()) {
        command = Serial.read();
        delay(500);
        client.write(command);
      }
      Serial.print("SENT");
      delay(500);

      int camretLen = client.available();
      if (camretLen > 0) {
        Serial.println("RESULT");
        for (int i = 0 ; i < camretLen; i++) {
          char c = client.read();
          Serial.print(c);
        }

        client.flush();

        break;
      }
      if (!client.connected()) {
        client.connect(server, 9876);
      }
  }
}

Here is the 2nd program:

#include <KeyboardController.h>

String inputString = "";         // a string to hold incoming data
String finalString = "";
boolean stringComplete = false;  // whether the string is complete

USBHost usb;

// Attach Keyboard to native USB socket
KeyboardController keyboard(usb);

void setup() {
  Serial.begin(115200);
  Serial.println(" Enter command ");

}

void keyPressed() {
  char inChar = keyboard.getOemKey();
  char inKey = keyboard.getKey();
  Serial.print("OEM key: ");
  Serial.println(keyboard.getOemKey());
  Serial.write(keyboard.getKey());
  Serial.println();



  if (inKey == '\') {  // If carriage return

    stringComplete = true;
  }
  else
  {
    inputString += inKey;
  }
}
void loop() {
  usb.Task();

  if (stringComplete) {
    Serial.print(" You entered this command: ");
    Serial.println(inputString);

    delay(1000);
    inputString = "";

    stringComplete = false;
    Serial.println(" Enter command  ");
  }

}

It is a common goal.

While you wait here, try googling

combine two sketches arduino

and see what kind of company your misery has!

At a glance your situation doesn’t seem all too hard.

a7

I comined them together but it does not work as it should be. That’s why I ask for help.

Does not work is kinda vague. Does it even compile?

Show us what you have, say what it does that it shouldn’t or doesn’t do what it should and

did you follow any guidance from amongst the literally hundreds of people who have tried this path and talked about it on the internets?

a7

Okay it works in a way that:
I press b and I cann start typing BUT when i press ENTER nothing happens…

//ENC28J60
int help=0;
#include <EthernetENC.h>
#include <SPI.h>
char command = 0;
char incomingByte = 0;
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress server(10, 5, 5, 10);
IPAddress ip(10, 5, 5, 109);
EthernetClient client;
//Klawiatura
#include <KeyboardController.h>

String inputString = "";         // a string to hold incoming data
boolean stringComplete = false;  // whether the string is complete

USBHost usb;

// Attach Keyboard to native USB socket
KeyboardController keyboard(usb);
void setup() {
  Ethernet.init(10);
  // Open serial communications and wait for port to open:
  Serial.begin(115200);
  // start the Ethernet connection:
  Ethernet.begin(mac, ip);
  // give the Ethernet shield a second to initialize:
  delay(1000);
  Serial.println("connecting...");
  // if you get a connection, report back via serial:
  if (client.connect(server, 9876)) {
    Serial.println("connected");
  }
  else {
    // if you didn't get a connection to the server:
    Serial.println("connection failed");
  }
  Serial.println("Wcisnij a aby odczytywac pakiety");
  Serial.println("Wcisnij b aby wysylac pakiety");
}
void keyPressed(){
    if (help==1){
  char inChar = keyboard.getOemKey();
  char inKey = keyboard.getKey();
  Serial.print("OEM key: ");
  Serial.println(keyboard.getOemKey());
  Serial.write(keyboard.getKey());
  Serial.println();
   


  if (inChar == 40) {  // If carriage return

    stringComplete = true;
  }
  else
  {
    inputString += inKey;
  }
    }
  }
void loop() {
  usb.Task();  
  Ethernet.maintain();
    usb.Task();
  // Read serial input:
  if (Serial.available() )
  {
    incomingByte = Serial.read();
  }
  else
  {
    incomingByte = 0;
  }

  switch (incomingByte) {
//CASE a
    case 'a':
      if (client.available())   {
        char c = client.read();
        Serial.print(c);
      }

      break;
      
//CASE b
    case 'b':
     usb.Task();
     keyPressed();
    help=1;
      delay(100);
      //
     


 if (Serial.available()){


if (stringComplete) {
    Serial.print(" You entered this command: ");
    Serial.println(inputString);

    delay(1000);
    inputString = "";

    stringComplete = false;
    Serial.println(" Enter command  ");
  }

  
        command = Serial.read();
        delay(500);
        client.write(command);
        }
        Serial.print("SENT");
        delay(500);

      int camretLen = client.available();
      if (camretLen > 0) {
        Serial.println("RESULT");
        for (int i = 0 ; i < camretLen; i++) {
          char c = client.read();
          Serial.print(c);
        }

        client.flush();

        break;
      }
      if (!client.connected()) {
        client.connect(server, 9876);
      }
  }
}
    

I can’t tell for sure, but it looks like you’ve changed the structure inadvertently.

Use auto-format in the IDE. This can help you sort what’s going with what.

And.

If you click just to the right of an opening brace ‘{’, the editor will help you find the matching closing brace ‘}’.

Then.

Note in the margin the little boxes with the minus signs - these will let you collapse the various statements so that you can see what I think, that is your case ‘b’ runs all the way to the end of the loop() function, this doesn’t look right.

One of your two original programs has a while statement, your combined code does not. This suggests that you have failed to understand thoroughly how each of the two works standing on its own.

If you are combining sketches, it is crucial that you do so from a point where you understand thoroughly how each works.

BTW, does each work as you found it? Where did they come from original?

a7