Advertisement
Justinberger

DMX2021.ino

Feb 9th, 2021
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.53 KB | None | 0 0
  1. /**
  2.    Arduino DMX Controller
  3.    Playful Technology (c) 2019
  4.  
  5.    Demonstrates how an Arduino + DMX shield can be used as a device to trigger
  6.    lighting and other stage effects, either based on direct input (e.g. button press)
  7.    or via a JSON payload received on serial port connection, such as
  8.    might be transmitted by Node-RED.
  9.  
  10.    Tested using the Conceptinetics Arduino DMX shield available from
  11.    https://www.tindie.com/products/Conceptinetics/25kv-isolated-dmx-512-shield-for-arduino-r2/
  12.    but should work with other DMX shields, or even DIY DMX-shields using MAX485 chip as described at
  13.    https://playground.arduino.cc/DMX/DMXShield/
  14. */
  15.  
  16. // INCLUDES
  17. // Modified version of the DMX library based on https://github.com/PaulStoffregen/DmxSimple
  18. #include "src/DMXSimple/DMXSimple.h"
  19. // JSON serialisation/desserialisation library download from https://arduinojson.org/
  20. #include <ArduinoJson.h>
  21.  
  22. // CONSTANTS
  23. // The pin on which Arduino will transmit DMX packets
  24. const byte dmxTransmitPin = 48;
  25. // Used to switch between Master/Slave modes
  26. //const byte modeSelectPin = 2;
  27.  
  28. void setup() {
  29.   // Serial connection is used to receive DMX request as JSON payload from another device, e.g. Node-RED
  30.   Serial.begin(9600);
  31.  
  32.   // Print out some help text to show example payload expected
  33.   Serial.println("Send DMX channel/values as JSON payload in the following format:");
  34.   Serial.println("{\"channels\":[");
  35.   Serial.println("  {\"channel\":1,\"value\":255},");
  36.   Serial.println("  {\"channel\":3,\"value\":128}");
  37.   Serial.println("]}");
  38.  
  39.   /*  // If using the Conceptinetics Arduino DMX shield, set to "DMX Master" mode
  40.     // (i.e. sets the line driver to write mode rather than slave mode)
  41.     pinMode(modeSelectPin, OUTPUT);
  42.     digitalWrite(modeSelectPin, HIGH);*/
  43.  
  44.   // Specify the pin that the Arduino will use to communicate with the DMX shield
  45.   DmxSimple.usePin(dmxTransmitPin);
  46.  
  47.   // By default, DmxSimple only sends up data in DMX channels that have actually been
  48.   // used. Some DMX devices require the whole 512byte message to be sent, which can be
  49.   // done as follows:
  50.   DmxSimple.maxChannel(512);
  51.  
  52. }
  53.  
  54. void loop() {
  55.   // For more complex effects, we'll process a JSON encoded DMX string
  56.   // containing an array of channel:value pairs received over the serial port
  57.   if (Serial.available()) {
  58.     // Create a new JSON document
  59.     DynamicJsonDocument doc(512);
  60. /*StaticJsonDocument<512> doc;
  61.     // set timeout to 10 seconds
  62. //    Serial.setTimeout(10000);
  63. deserializeJson(doc, Serial);*/
  64.     // Parse any incoming data received over the serial connection
  65.     DeserializationError error = deserializeJson(doc, Serial);
  66.     // Check that the data is valid
  67.     if (error) {
  68.       Serial.print(F("deserializeJson() failed: "));
  69.       Serial.println(error.c_str());
  70.       return;
  71.     }
  72.  
  73. //deserializeJson(doc, Serial);
  74.  
  75. /*for (JsonObject elem : doc["channels"].as<JsonArray>()) {
  76.  
  77.   int channel = elem["channel"]; // 1, 3
  78.   int value = elem["value"]; // 255, 128
  79.  
  80.       Serial.println(channel);
  81.       Serial.println(value);
  82.       // Write each value to the correct channel
  83.       DmxSimple.write(channel,value);
  84.       }*/
  85.  
  86.    
  87.  
  88.     // Parse the array of channel:values from the received string
  89.     JsonArray array = doc["channels"].as<JsonArray>();
  90.     for (JsonVariant v : array) {
  91.       Serial.println(v["channel"].as<int>());
  92.       Serial.println(v["value"].as<int>());
  93.       // Write each value to the correct channel
  94.       DmxSimple.write(v["channel"].as<int>(), v["value"].as<int>());
  95.     }
  96.   }
  97. }
  98.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement