Advertisement
Guest User

Untitled

a guest
Sep 15th, 2019
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "DMXSerial.h"
  2.  
  3. #define ch1Switch A0
  4. #define ch2Switch A1
  5. #define pot A2
  6. #define dmx 0
  7. #define relais1 5
  8. #define relais2 6
  9.  
  10. int dmxChannel1 = 255;
  11. int dmxChannel2 = 256;
  12.  
  13. const int chLEDs[] = {2, 3, 4, 7, 8, 9, 10, 11, 12};
  14.  
  15. bool switchChannelLights = true;
  16.  
  17. int bin1[8];
  18. int bin2[8];
  19.  
  20. unsigned long now = 0;
  21. int ledChangeDelay = 1000;
  22.  
  23. void setup() {
  24.   DMXSerial.init(DMXReceiver);
  25.   pinMode(ch1Switch, INPUT);
  26.   pinMode(ch2Switch, INPUT);
  27.   for (int i = 0; i < sizeof(chLEDs) / sizeof(int); i++) {
  28.     pinMode(chLEDs[i], OUTPUT);
  29.   }
  30. }
  31.  
  32. int currentLEDState = 1;
  33.  
  34. void loop() {
  35.   int switch1 = analogRead(ch1Switch);
  36.   int switch2 = analogRead(ch2Switch);
  37.   if (switch1 == HIGH && switch2 == HIGH) {
  38.     // Both are in edit mode
  39.     switchChannelLights = false;
  40.     dmxChannel1 = analogRead(pot);
  41.     dmxChannel2 = dmxChannel1;
  42.     for (int i = 8; i >= 0; i--) {
  43.       int k = dmxChannel1 >> i;
  44.       if (k & 1) {
  45.         bin1[i] = 1;
  46.       } else {
  47.         bin1[i] = 0;
  48.       }
  49.     }
  50.     bin2[8] = bin1[8];
  51.     for (int i = 0; i < 8; i++) {
  52.       digitalWrite(chLEDs[i], bin1[i]);
  53.     }
  54.   } else if (switch1 == HIGH && switch2 == LOW) {
  55.     // Switch1 is in edit mode
  56.     switchChannelLights = false;
  57.     dmxChannel1 = analogRead(pot);
  58.     for (int i = 8; i >= 0; i--) {
  59.       int k = dmxChannel1 >> i;
  60.       if (k & 1) {
  61.         bin1[i] = 1;
  62.       } else {
  63.         bin1[i] = 0;
  64.       }
  65.     }
  66.     for (int i = 0; i < 8; i++) {
  67.       digitalWrite(chLEDs[i], bin1[i]);
  68.     }
  69.   } else if (switch1 == LOW && switch2 == HIGH) {
  70.     // Switch2 is in edit mode
  71.     switchChannelLights = false;
  72.     dmxChannel2 = analogRead(pot);
  73.     for (int i = 8; i >= 0; i--) {
  74.       int k = dmxChannel2 >> i;
  75.       if (k & 1) {
  76.         bin2[i] = 1;
  77.       } else {
  78.         bin2[i] = 0;
  79.       }
  80.     }
  81.     for (int i = 0; i < 8; i++) {
  82.       digitalWrite(chLEDs[i], bin2[i]);
  83.     }
  84.   } else {
  85.     // None are in edit mode
  86.     switchChannelLights = true;
  87.     for (int i = 0; i < 8; i++) {
  88.       digitalWrite(chLEDs[i], bin1[i]);
  89.     }
  90.     currentLEDState = 1;
  91.     if (millis() > now + ledChangeDelay) {
  92.       now = millis();
  93.       if (currentLEDState == 1) {
  94.         currentLEDState = 2;
  95.         for (int i = 0; i < 8; i++) {
  96.           digitalWrite(chLEDs[i], bin2[i]);
  97.         }
  98.       } else {
  99.         currentLEDState = 1;
  100.         for (int i = 0; i < 8; i++) {
  101.           digitalWrite(chLEDs[i], bin1[i]);
  102.         }
  103.       }
  104.     }
  105.   }
  106.  
  107.   unsigned long lastPacket = DMXSerial.noDataSince();
  108.   int threshold = 127;
  109.   if (lastPacket < 5000) {
  110.     int state1 = DMXSerial.read(dmxChannel1);
  111.     int state2 = DMXSerial.read(dmxChannel2);
  112.     digitalWrite(relais1, state1 > threshold);
  113.     digitalWrite(relais2, state2 > threshold);
  114.   }
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement