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

byte myMac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte myIp[]  = { 10, 0, 1, 37 };
int  serverPort  = 10000;

int ledPin1 = 5;
int ledPin2 = 9;
int ledPin3 = 10;

int OSCFloat1 = 0;
int OSCFloat2 = 0;
int OSCFloat3 = 0;

OSCServer server;
OSCClient client;

EthernetClient Eclient; //DHCP IP Printer Init


void setup(){ 
  
  
  // ********** Print IP from DHCP Server
  
  Serial.begin(9600);
  // start the Ethernet connection:
  if (Ethernet.begin(myMac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // no point in carrying on, so do nothing forevermore:
    for(;;)
      ;
  }
  // print your local IP address:
  Serial.print("My IP address: ");
  for (byte thisByte = 0; thisByte < 4; thisByte++) {
    // print the value of each byte of the IP address:
    Serial.print(Ethernet.localIP()[thisByte], DEC);
    Serial.print("."); 
    
    byte myIp = thisByte;
  }
  Serial.println();
  
  // ********** End IP from DHCP Server
 
 
 Ethernet.begin(myMac ,myIp); 
 server.begin(serverPort);
  
  
 server.addCallback("/GreenSlider",&func1);
 server.addCallback("/AFF/Excitement",&func2);
 server.addCallback("/EXP/Blink",&func3);
 
 
 pinMode(ledPin1, OUTPUT);  
 pinMode(ledPin2, OUTPUT); 
 pinMode(ledPin3, OUTPUT);
}

  
void loop()
{
  if(server.aviableCheck()>0){
  } 
}

void func1 (OSCMessage *_mes1)
{
   OSCFloat1 = _mes1->getArgFloat(0);
   analogWrite(ledPin1, OSCFloat1);
//   Serial.print("Data1: ");
//   Serial.println (OSCFloat1);
   delay(30); 
}

void func2 (OSCMessage *_mes2)
{
  
   OSCFloat2 = _mes2->getArgFloat(0);
   analogWrite(ledPin2, OSCFloat2);
   Serial.print("Data2: ");
   Serial.println (OSCFloat2);
   delay(30); 
}

void func3 (OSCMessage *_mes3)
{
  OSCFloat3 = _mes3->getArgFloat(0);
 /* digitalWrite(ledPin3, HIGH);
  delay(20);
  digitalWrite(ledPin3, LOW);
  delay(20);
*/
//  Switch depending on Value
   if(OSCFloat3 == 1){
   digitalWrite(ledPin3, HIGH);
   }
   else {
   digitalWrite(ledPin3, LOW);
   }

}
