Convert binary from device into straight contact closure for sketch

Hello,
I have a working sketch c/ Henny from the Vmix forums. It allows some video production software to be controlled via it’s API over ethernet. The sketch is below.

#include <SPI.h>
#include <Ethernet.h>

// Enter a MAC address for your controller below.
byte mac[] = { 
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress server(192,168,1,85);  // numeric IP for Google (no DNS)


// Set the static IP address to use if the DHCP fails to assign
IPAddress ip(192,168,1,202);

// pin button:
const int button2Pin = A1;     // the number of the pushbutton pin
const int button3Pin = A2;
const int button4Pin = A3;
const int button5Pin = A4;

// variables will change:
int button2State = 0;     
int button2StateOld = 0; // variable for reading the pushbutton status
int button3State = 0;     
int button3StateOld = 0; // variable for reading the pushbutton status
int button4State = 0;     
int button4StateOld = 0; // variable for reading the pushbutton status
int button5State = 0;     
int button5StateOld = 0; // variable for reading the pushbutton status
int led = 9;

// Initialize the Ethernet client library
// with the IP address and port of the server 
// that you want to connect to (port 80 is default for HTTP):
EthernetClient client;

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }

  // start the Ethernet connection:

  // initialize the pushbutton pin as an input:
  pinMode(button2Pin, INPUT); 
  pinMode(button3Pin, INPUT);  
  pinMode(button4Pin, INPUT); 
  pinMode(button5Pin, INPUT); 
   pinMode(led, OUTPUT);

  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    
    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, 80)) {
    Serial.println("connected");
    digitalWrite(led, HIGH);

  } 
  else {
    // kf you didn't get a connection to the server:
    Serial.println("connection failed");
  }
}

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


void loop1 ()
{
  button2StateOld=button2State;
  button2State = digitalRead(button2Pin);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (button2State != button2StateOld)  {
    if (button2State == HIGH){

      Serial.println("Button1");
      client.connect(server, 8088);

      client.println("POST /API/?Function=PreviewInput&Input=1");
      client.println();
      client.stop();
    } 
  } 
}

void loop2 ()
{  
  button3StateOld=button3State;
  button3State = digitalRead(button3Pin);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (button3State != button3StateOld)  {
    if (button3State == HIGH){
      Serial.println("Button2");
      client.connect(server, 8088)
        ; 
      client.println("POST /API/?Function=PreviewInput&Input=2");
      client.println();
      client.stop();
    } 
  } 
}
void loop3 ()
{
  button4StateOld=button4State;
  button4State = digitalRead(button4Pin);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (button4State != button4StateOld)  {
    if (button4State == HIGH){

      Serial.println("Button3");
      client.connect(server, 8088);

      client.println("POST /API/?Function=PreviewInput&Input=3");
      client.println();
      client.stop();
    } 
  } 
}

void loop4 ()
{  
  button5StateOld=button5State;
  button5State = digitalRead(button5Pin);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (button5State != button5StateOld)  {
    if (button5State == HIGH){
      Serial.println("Button4");
      client.connect(server, 8088)
        ; 
      client.println("POST /API/?Function=cut");
      client.println();
      client.stop();
    } 
  } 
}

I want to connect a device to the arduino which provides open collector outputs ,but it does so in binary format.

I tried to combine two pins together like the snippet below and had no luck. Do I want to do something with an AND function, or is there another way to convert and store binary to (decimal?)

Thank you!
David

void loop1 ()
{
  button2StateOld=button2State;
  button2State = digitalRead(button2Pin);
 button3StateOld=button3State;
 button3State = digitalRead(button3Pin);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (button2State != button2StateOld)  {
    if (button2State == HIGH){

  if (button3State != button3StateOld)  {
    if (button3State == HIGH){

      Serial.println("Button1and2");
      client.connect(server, 8088);

      client.println("POST /API/?Function=PreviewInput&Input=1");
      client.println();
      client.stop();
    } 
  } 
    }
  }
}

David, the "snippet below" is not self-explanatory, so you need to explain its purpose and how it is supposed to function.

So...
You have 2 inputs for a possible 4 states; is that correct? If not then please explain what you do have.

In order to read an open collector output with an Arduino digital input, one approach is to connect the output to Arduino Vcc with a pullup resistor, typically 10K. Don't forget to connect the grounds.

Another approach is to use an optocoupler, which will need a current limiting resistor for the LED and a pullup on the output.

aarg:
David, the "snippet below" is not self-explanatory, so you need to explain its purpose and how it is supposed to function.

Hello,
Thanks for your reply.
The "snippet" is a part of the main code that I attempted to modify, I was trying to combine the reading of two buttons in an "and" type configuration. If you look further down at the main code, you will see there are 4 loops which contain code which reads the 4 buttons.
Hope that clarifies.

PerryBebbington:
So...
You have 2 inputs for a possible 4 states; is that correct? If not then please explain what you do have.

Hello,
The code as it stands has 4 inputs, to send 4 seperate HTTP commands.
(it's easy enough for me to follow, because it's basically the same code 4 times over)
What I would like to do, is read across 3 inputs for a total of 5 states, as per my IMGUR picture, but have each state trigger it's own seperate HTTP command.
Hope that is clear- thank you

jremington:
In order to read an open collector output with an Arduino digital input, one approach is to connect the output to Arduino Vcc with a pullup resistor, typically 10K. Don't forget to connect the grounds.

Another approach is to use an optocoupler, which will need a current limiting resistor for the LED and a pullup on the output.

Thank you - I have the physical interfacing sorted out, but I need to work out how to convert from the binary output and make it trigger the correct HTTP command strings.

I need to work out how to convert from the binary output

Could you explain the problem? The output is either 1 or 0.

You want to "and" buttons? Do they have to be down simultaneously or just sequentially?

Thanks all for the replies so far.
Please let me try and be clear on the requirement, that I am unsure how to approach in my code.

Currently, the code does this:

(button) input A1 > Send HTTP message 1
(button) input A2 > Send HTTP message 2
(button) input A3 > Send HTTP message 3
(button) input A4 > Send HTTP message 4

Really basic.

What I need is:

(output from device) HIGH on A1 > Send HTTP message 1
(output from device) HIGH on A2 > Send HTTP message 2
(output from device) HIGH on A1 AND A2 > Send HTTP message 3
(output from device) HIGH on A3 > Send HTTP message 4
(output from device) HIGH on A1 AND A3 > Send HTTP message 5

The actual "selection" is likely to change greater than once every few seconds - it's an output that indicates that a particular camera is selected on the controller.

I am clear on the single pin ones. It's the ones where I need to have two pins read simultaneously.

Is that a better explanation? :slight_smile:
Thank you,
David

I doubt that is what you need. This sends two messages, both 1 and 3.

(output from device) HIGH on A1 > Send HTTP message 1
(output from device) HIGH on A1 AND A2 > Send HTTP message 3

To do four different actions based on two inputs this is one approach

if (A1==1 && A2==0) send_msg_1();
if (A1==0 && A2==1) send_msg_2();
if (A1==1 && A2==1) send_msg_3();
etc.

or make a unique number from the two inputs, between 0 and 3

byte code = A1 + A2*2;
if(code == 0) action_1(); //neither A1 nor A2 are HIGH
if(code == 1) action_2(); //only A1 is HIGH
if(code == 2) action_3(); //only A2 is HIGH
etc.

with three inputs, make a unique number between 0 and 7, with up to 8 actions.

byte code=A1 + A2*2 + A3*4;

Finally, I would guess that these messages are not supposed to be sent continuously, based on the states of the inputs. In that case, you need to determine when an input has changed and then take appropriate action.

Conveniently, the Arduino IDE has a built in State Change Detection example to study. You will need at least one state variable to store the last configuration, for comparison with a new set of inputs.