Send Two OSC messages depending on button state

Hi! My wish is to send a specific OSC message to Qlab over UDP when the button is pressed and to send another message when the button is released. I copied from examples and other sketches found on the internet but I keep having the same problem.
I am reading the incoming messages to my computer with OSCulator and looks like one of the two messages (when button is released) is received correctly, when I press the button I have a "OSC UDP server error" shown in OSCulator and no message is received.
I am sure it is a simple problem, but I am new to programming and I need some help!
Thank you!
Here is the code:

/*
    Make an OSC message and send it over UDP
    
    Adrian Freed
 */
#include <Ethernet.h>
#include <EthernetUdp.h>
#include <SPI.h>    
#include <OSCMessage.h>

int buttonPin1 = 12;
int buttonValue1 = HIGH;
boolean buttonChanged = false;

EthernetUDP Udp;


//the Arduino's IP
IPAddress ip(192, 168, 1, 177);
//destination IP
IPAddress outIp(192, 168, 1, 75);
const unsigned int outPort = 53000;

 byte mac[] = {  
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; // you can find this written on the board of some Arduino Ethernets or shields
void setup() {
  Serial.begin(9600);
   pinMode(buttonPin1, INPUT_PULLUP);
  Ethernet.begin(mac,ip);
    Udp.begin(8888);
}


void loop() {
OSCMessage play("/cue/1/go/");
OSCMessage stp("/cue/0/go/");

if (digitalRead (buttonPin1) != buttonValue1)
{
  buttonChanged = true;
  buttonValue1 = digitalRead(buttonPin1);
  
}

if (buttonChanged == true)
{
  buttonChanged = false;
  Serial.print("Button: ");
  Serial.println(buttonValue1);

 if (buttonValue1 == 1)
 {
  Udp.beginPacket(outIp, outPort);
  play.send(Udp);
  Udp.endPacket(); // mark the end of the OSC Packet
 play.empty(); // free space occupied by message

 delay (20);
}

if  (buttonValue1 == 0)
{
  Udp.beginPacket(outIp, outPort);
  stp.send(Udp);
  Udp.endPacket(); // mark the end of the OSC Packet
 stp.empty(); // free space occupied by message

 delay (20);
}
  
}



}

consider

// simple button processing

const byte butPin = A1;

byte butState;

// -----------------------------------------------------------------------------
void loop (void)
{
    byte but = digitalRead (butPin);

    if (butState != but)  {     // check that button state changed
        butState = but;
        delay (10);         // debounce

        if (LOW == but)     // button pressed
            Serial.println ("send msg1");
        else 
            Serial.println ("send msg2");
    }
}

// -----------------------------------------------------------------------------
void setup (void)
{
    Serial.begin (9600);

    pinMode     (butPin, INPUT_PULLUP);
    butState  = digitalRead (butPin);
}

Thank you so much for your reply, I included your suggestion and I can see that I am printing "send msg1" and "send msg2" correctly on the serial monitor according to the button state.
The problem is still the OSC, the first message ("/cue/1/start") is delivered correctly but the second one ("/cue/0/start") still does not work!
I must be missing some information about how to handle OSC messages through UDP probably. I really do not understand the problem!
Here is the new code:


/*
    Make an OSC message and send it over UDP
    
    Adrian Freed
 */
#include <Ethernet.h>
#include <EthernetUdp.h>
#include <SPI.h>    
#include <OSCMessage.h>

const int buttonPin1 = 12;
int buttonValue1 = HIGH;

EthernetUDP Udp;


//the Arduino's IP
IPAddress ip(192, 168, 1, 177);
//destination IP
IPAddress outIp(192, 168, 1, 75);
const unsigned int outPort = 53000;

 byte mac[] = {  
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; // you can find this written on the board of some Arduino Ethernets or shields
void setup() {
  Serial.begin(9600);
   pinMode(buttonPin1, INPUT_PULLUP);
  Ethernet.begin(mac,ip);
    Udp.begin(8888);
}


void loop() {

  int but = digitalRead(buttonPin1);
  
OSCMessage play("/cue/1/start");
OSCMessage stp("/cue/0/start");

if (buttonValue1 != but) {
  buttonValue1 = but;
 delay (10);

  if (1 == but) {
  Serial.println ("send msg1");
  Udp.beginPacket(outIp, outPort);
  play.send(Udp);
  Udp.endPacket(); // mark the end of the OSC Packet
  play.empty(); // free space occupied by message
  }
  else {
  Serial.println ("send msg2");
  Udp.beginPacket(outIp, outPort);
  stp.send(Udp);
  Udp.endPacket(); // mark the end of the OSC Packet
  stp.empty(); // free space occupied by message
  
  }
 
}
  delay (20);
}

OSC requires a value after the address. So the entire code for sending something with OSC would be:

OSCMessage msg(address);
msg.add(value);
Udp.beginPacket(clientIP, port);
msg.send(Udp);    // send the bytes to the SLIP stream
Udp.endPacket();  // mark the end of the OSC Packet

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.