This week we change studios and show you how to use either an Ethernet shield or Ethernet Arduino to connect to the network. We then take it one step further and show you how to use the new found connectivity to communicate using the Open Sound Control protocol (OSC) to mute channels on a sound board over the ethernet connection.
Downloads
Example Code
Most of the code I walked through in this episode was from the built in example code that comes in the Arduino IDE. The one that I created just prior to the show is below.
X32 Mute
#include <SPI.h>
#include <Ethernet.h> // version IDE 0022
#include <Z_OSC.h>
int Button1 = 2;
int Button2 = 4;
int Button3 = 7;
int But1_g = 3;
int But1_r = 5;
int But2_g = 6;
int But2_r = 9;
int But3_g = 10;
int But3_r = 11;
int nextPin = 0;
int lights[] = {But1_g, But1_r, But2_g, But2_r, But3_g, But3_r};
byte myMac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte myIp[] = { 10, 232, 1, 254 };
byte destIp[] = { 10, 232, 1, 22 };
int destPort = 10023;
int MUTE = 63;
int NOMUTE = 1;
int MUTED = 0;
int UNMUTED = 1;
int GroupMode = 0;
int ChannelMode = 0;
char oscAdr[] = "/bus/07/mix/on";
char oscAdr2[] = "/ch/09/mix/on";
char oscAdr3[] = "/ch/09/mix/fader";
Z_OSCClient client;
void setup(){
Serial.begin(9600);
pinMode(Button1, INPUT_PULLUP);
pinMode(Button2, INPUT_PULLUP);
pinMode(Button3, INPUT_PULLUP);
for (int l=0; l<6; ++l)
pinMode(lights[l], OUTPUT);
Ethernet.begin(myMac ,myIp);
startupCycle();
}
void loop(){
if (!digitalRead(Button1)) {
if (GroupMode == MUTED) {
UnMuteGroup();
GroupMode = UNMUTED;
} else {
MuteGroup();
GroupMode = MUTED;
}
}
if (!digitalRead(Button3)) {
if (ChannelMode == MUTED) {
UnMuteChannel();
ChannelMode = UNMUTED;
} else {
MuteChannel();
ChannelMode = MUTED;
}
}
if (ChannelMode == MUTED) {
digitalWrite(But3_r, true);
digitalWrite(But3_g, false);
} else {
digitalWrite(But3_r, false);
digitalWrite(But3_g, true);
}
if (GroupMode == MUTED) {
digitalWrite(But1_r, true);
digitalWrite(But1_g, false);
} else {
digitalWrite(But1_r, false);
digitalWrite(But1_g, true);
}
delay(200);
}
void MuteGroup(){
Z_OSCMessage message;
message.setAddress(destIp,destPort);
message.setZ_OSCMessage(oscAdr ,"i", false);
client.send(&message);
}
void UnMuteGroup(){
Z_OSCMessage message;
message.setAddress(destIp,destPort);
message.setZ_OSCMessage(oscAdr ,"i", true);
client.send(&message);
}
void MuteChannel(){
Z_OSCMessage message;
message.setAddress(destIp,destPort);
message.setZ_OSCMessage(oscAdr2 ,"i", false);
client.send(&message);
}
void UnMuteChannel(){
Z_OSCMessage message;
message.setAddress(destIp,destPort);
message.setZ_OSCMessage(oscAdr2 ,"i", true);
client.send(&message);
}
void startupCycle()
{
for (int l=0; l<25; ++l)
{
turnAllOff();
//Serial.println(lights[nextPin]);
digitalWrite(lights[nextPin], LOW);
//digitalWrite(11, LOW);
++nextPin;
if (nextPin > 5)
nextPin = 0;
delay(200);
}
turnAllOn();
}
void turnAllOff()
{
for (int l=0; l<6; ++l)
digitalWrite(lights[l], HIGH);
}
void turnAllOn()
{
for (int l=0; l<6; ++l)
digitalWrite(lights[l], LOW);
}