skaarhoj ATEM arduino Control Code

Hello,

I have only used arduino for a week now but would be very grateful if someone is able to provide any support on the following code. I am needing to control an ATEM television studio via an arduino one with the triggers of the arduino via HTTP commands. The HTTP commands are triggered via a AV control system.

The below code does work to a degree but sometimes it takes a few seconds to respond, sometime it is instant. When I use
AtemSwitcher.doAuto(); instead of AtemSwitcher.doCut(); the connection is disconnected after completing the command.

I hoping someone with more experience is able to suggest some changes.

Thanks
Daniel

#include <SPI.h>         // needed for Arduino versions later than 0018
#include <Ethernet.h>

boolean reading = false;

byte mac[] = { 
  0x90, 0xA2, 0xDA, 0x0F, 0x2A, 0xA2 };		// <= SETUP
IPAddress ip(192, 168, 1, 99);				// <= SETUP

EthernetServer server = EthernetServer(80);

#include <ATEM.h>

ATEM AtemSwitcher(IPAddress(192, 168, 1, 200), 56417);  // <= SETUP (the IP address of the ATEM switcher)

template<class T>
inline Print &operator <<(Print &obj, T arg)
{  
  obj.print(arg); 
  return obj; 
}

void setup() { 

  Ethernet.begin(mac,ip);
  Serial.begin(9600);  
  server.begin();
  Serial << F("\n- - - - - - - -\nSerial Started\n");    // Initialize a connection to the switcher:
  
  AtemSwitcher.serialOutput(true);
  AtemSwitcher.connect();
  

  
}
/*
void loop() {
 
 // Check for packets, respond to them etc. Keeping the connection alive!
 AtemSwitcher.runLoop();
 checkForClient();
 }
 */

void loop() {
  // Check for packets, respond to them etc. Keeping the connection alive!
  // VERY important that this function is called all the time - otherwise connection might be lost because packets from the switcher is
  // overlooked and not responded to.
  AtemSwitcher.runLoop();

  // If connection is gone anyway, try to reconnect:
  if (AtemSwitcher.isConnectionTimedOut())  {
    Serial << F("Connection to ATEM Switcher has timed out - reconnecting!\n");
    AtemSwitcher.connect();
  }  

  // If you fancy to make delays in your sketches, ALWAYS do it using the AtemSwitcher delay function - this will wait while calling ru
  // Loop() checking for packets and thus keeping the connection up.
  AtemSwitcher.delay(5000);


  // If you monitor the serial output, you should see a lot of "ACK, rpID: xxxx" and then every 5 seconds this message:
  Serial << F("End of normal loop() - still kicking?\n");

  // Now, try also to disconnect the network cable - and see if it reconnects properly.

  checkForClient();

}


void checkForClient(){

  EthernetClient client = server.available();

  if (client) {

    // an http request ends with a blank line
    boolean currentLineIsBlank = true;
    boolean sentHeader = false;

    while (client.connected()) {
      if (client.available()) {

        if(!sentHeader){
          // send a standard http response header
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println();
          sentHeader = true;
        }

        char c = client.read();

        if(reading && c == ' ') reading = false;
        if(c == '?') reading = true; //found the ?, begin reading the info

        if(reading){
          Serial.print(c);

          switch (c) {
          case '1':
            AtemSwitcher.changePreviewInput(1);
            break;
          case '2':
            AtemSwitcher.changePreviewInput(2);
            break;
          case '3':
            AtemSwitcher.changePreviewInput(3);
\            break;
          case '4':
            AtemSwitcher.doCut();
            break;

          }

        }

        if (c == '\n' && currentLineIsBlank)  break;

        if (c == '\n') {
          currentLineIsBlank = true;
        }
        else if (c != '\r') {
          currentLineIsBlank = false;
        }

      }
    }

    delay(1); // give the web browser time to receive the data
    client.stop(); // close the connection:

  } 

}

void triggerPin(int pin, EthernetClient client){
  //blink a pin - Client needed just for HTML output purposes.  
  client.print("Turning on pin ");
  client.println(pin);
  client.print("
");

  digitalWrite(pin, HIGH);
  delay(25);
  digitalWrite(pin, LOW);
  delay(25);
}

I know nothing about the library that you are using or how it works, but this

 AtemSwitcher.delay(5000);

looks very much like it waits for 5 seconds during which time it will not respond to any input. If that is the case then no wonder

sometimes it takes a few seconds to respond, sometime it is instant.

What is the delay there for ?