/********************************************************************************************************** * * Title : DMX Switch pack with 2 relays and dipswitch for addressing * Version : 1.0 * Last updated : 08-02-2013 * Target : ATmega328 @ 16Mhz. * Author : Guillaume A. Galenkamp * ***********************************************************************************************************/ #include // library: http://www.mathertel.de/Arduino/DMXSerial.aspx #define led 4 // RxD led connected to Arduino pin 4 #define relay1 3 // Relay 1 connected to Arduino pin 3 #define relay2 2 // Relay 2 connected to Arduino pin 2 #define dipSwitch9 5 // Dipswitch 9 connected to Arduino pin 5 byte valueCH1; // Buffer CH1 byte valueCH2; // Buffer CH2 int dipPins[] = {9, 8, 7, 6, 5, 4, 3, 2}; // DIP switch pins int startAddress; int i,j; void setup() { pinMode(led_PIN, OUTPUT); // Set digital pin as output for the DMX OK led pinMode(relay1_PIN, OUTPUT); // Set digital pin as output for relay 1 pinMode(relay2_PIN, OUTPUT); // Set digital pin as output for relay 2 for (int i=0; i <= 7; i++) { // Configures the digital pins to behave as an input (Arduino pins 4~13) pinMode(dipPins[i], INPUT); } DMXSerial.init(DMXReceiver); // Set to DMX Receiver } // Create address from DIP Switch int address(){ for(i=0; i<=7; i++){ j = (j << 1) | digitalRead(dipPins[i]); // read the input pin } return j; //return address } void readAddress() { startAddress = dipswitch(), DEC; // Read the 8 bit start addres dip9_State = digitalRead(dipSwitch9); // Add the other 256 later if (dip9_State == HIGH) { startAddress = startAddress + 256; } else { startAddress = startAddress + 0; } if (startAddress == 0) { // If dipswitches are off, set start address to 1 startAddress ++; } startAddress = constrain(startAddress, 0, 511); // contrain it to 511 ch. because of 2ch. input } // The infinite loop void loop() { readAddress(); // Read the start address // Output 1 if (valueCH1 >= 127) { // DMX value (50%) at which relays switches on (0..255) digitalWrite(relay1, HIGH); } else { digitalWrite(relay1, LOW); } // Output 2 if (valueCH2 >= 127) { digitalWrite(relay2, HIGH); } else { digitalWrite(relay2, LOW); } // Calculate how long no data backet was received unsigned long lastPacket = DMXSerial.noDataSince(); // turn data led on when packets are received if (lastPacket < 1000) { digitalWrite(led, HIGH); } else { // Turn off data led, when no data was received after 1 second. digitalWrite(led, LOW); } } // End of loop