Arduino artnet to hw sketch

From Institute of Intermedia
Jump to: navigation, search

code

#include <Ethernet.h>
#include <Udp.h>
#include <Servo.h>
#include <DmxSimple.h>
/* UdpReceive.pde: Example how to receive packets over UDP 
 * protoze jsem nenasel jine publikovane odpovidajici reseni,
 * pouzil jsem knihovny Udp a DmxSimle a Servo
 * modifikace jakub.hybler@gmail.com 12/29/2009
 */
/* ETHERNET CONFIGURATION *************************************/
/* ARDUINO: set MAC, IP address of Ethernet shield, its gateway,
and local port to listen on for incoming packets */
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //MAC address to use
byte ip[] = { 192, 168, 10, 166 }; // Arduino's IP address
byte gw[] = { 192, 168, 10, 1 };   // Gateway IP address
int localPort = 6454; //local port to listen on
/***************************************************************/
#define MIN_PULSE_WIDTH       500     // the shortest pulse sent to a servo  
#define MAX_PULSE_WIDTH      2400  // the longest pulse sent to a servo 
#define MAX_SIZE 128 //maximum packet size
int cont = 8 ; //cislo kanalu ovlada 180 st otocku serva
byte packetBuffer[MAX_SIZE]; //buffer to hold incoming packet
int packetSize; //holds received packet size
byte remoteIp[4]; //holds recvieved packet's originating IP
int remotePort; //holds received packet's originating port
int polx;
int poly;
int i;
Servo myservox;  // create servo object to control a servo
Servo myservoy;  // create servo object to control a servo
void setup() {
  Ethernet.begin(mac,ip,gw);
  Udp.begin(localPort);
  Serial.begin(9600); 
  myservox.attach(6); 
  myservoy.attach(7); 
  pinMode(5, OUTPUT);
  //myservo.setMinimumPulse(544);
}
void loop() {  
  //if there's data available, read a packet
  if(Udp.available()) {
    packetSize = Udp.readPacket(packetBuffer,MAX_SIZE,remoteIp,(uint16_t *)&remotePort);
    if(packetSize <= MAX_SIZE) {
      for(i=0; i<packetSize; i++) {
        if(i >= 18) {
        //DmxSimple.write(i-17, packetBuffer[i]); 
      if((i-17 == cont) && (packetBuffer[i]!= polx))  {
      polx = packetBuffer[i];     // scale it to use it with the servo (value between 0 and 180) 
      polx = map(polx, 0, 254, 0, 162); 
      myservox.write(polx); }  
      if(i-17 == cont+1)  {
      poly = packetBuffer[i];     // scale it to use it with the servo (value between 0 and 180) 
      poly = map(poly, 0, 254, 5, 179); 
      myservoy.write(poly); }     
      if(i-17 == cont+2)  { 
      analogWrite(5, packetBuffer[i] );
        } 
       }
     }  
   } else {
     //PANIC - packet too long!
     // we've already clobbered mem past our buffer boundary
   Serial.println("eeee too long packet");
   }
 }
}