Skip to content

Instantly share code, notes, and snippets.

@bevchou
Created March 6, 2018 03:42
Show Gist options
  • Save bevchou/8e2dd1a589825868548c8c4bf2340d85 to your computer and use it in GitHub Desktop.
Save bevchou/8e2dd1a589825868548c8c4bf2340d85 to your computer and use it in GitHub Desktop.
Arduino MKR1000 code to control DMX lighting in the ITP lounge for Tangible Interaction Workshop
/*
Code for wifi and sACN protocol is based on Tom Igoe's "sACN intro" example
*/
#include <SPI.h>
#include <WiFi101.h>
//#include <ESP8266WiFi.h> // This should work with the ESP8266 as well.
#include <WiFiUdp.h>
#include <sACNSource.h>
#include "arduino_secrets.h"
WiFiUDP Udp; // instance of UDP library
sACNSource myController(Udp); // Your Ethernet-to-DMX device
char receiverAddress[] = "fill in your ip address"; // sACN receiver address
int myUniverse = 1; // DMX universe
char myDevice[] = "bc"; // sender name
char myUuid[] = "cc0c6797-a4df-428c-a20e-3706f5202d6f"; // sender UUID
//pinouts
int togglePin = 9;
int potPin = A1;
int reedPin[] = {3, 4, 5, 6, 7, 8};
int numReed = 6;
int ledPin = 2; // MAKE SURE IT'S PWM PIN
//global values
int brightness = 0;
void setup() {
Serial.begin(9600);
// while you're not connected to a WiFi AP,
while ( WiFi.status() != WL_CONNECTED) {
Serial.print("Attempting to connect to Network named: ");
Serial.println(SECRET_SSID); // print the network name (SSID)
WiFi.begin(SECRET_SSID, SECRET_PASS); // try to connect
delay(2000);
}
// initialize sACN source:
myController.begin(myDevice, myUuid, myUniverse);
// When you're connected, print out the device's network status:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
// set DMX channel values to 0:
for (int dmxChannel = 1; dmxChannel < 513; dmxChannel++) {
myController.setChannel(dmxChannel, 0);
}
myController.sendPacket(receiverAddress);
//turn max % white color (this is not the brightness)
myController.setChannel(5, 255);
//turn on max % red color (this is not the brightness)
myController.setChannel(48, 255);
// REED SENSOR INPUTS
for (int i = 0; i < numReed; i++) {
pinMode(reedPin[i], INPUT_PULLUP);
}
// other inputs
pinMode(togglePin, INPUT);
//outputs
pinMode(ledPin, OUTPUT);
}
void loop() {
//read potentiometer & map reading to brightness
int potState = analogRead(potPin);
brightness = map(potState, 0, 1023, 0, 255);
//read Reed sensor
int reedState [] = {0, 0, 0, 0, 0, 0};
for (int i = 0; i < numReed; i++) {
reedState[i] = digitalRead(reedPin[i]);
//calculate channel #
int channelNum = 20 * i + 1;
// Serial.print(channelNum);
//if reed switch activated, send lights on
if (reedState[i] == LOW) {
myController.setChannel(channelNum, brightness);
// Serial.print("-on, ");
} else {
myController.setChannel(channelNum, 0);
// Serial.print("-off, ");
}
}
//read state of toggle swtich
int toggleState = digitalRead(togglePin);
//if it's on send lights on
if (toggleState == HIGH) {
//and show brightness on LED
analogWrite(ledPin, brightness);
//send data
myController.sendPacket(receiverAddress);
// Serial.println(".");
} else {
//led off
analogWrite(ledPin, 0);
// turn everything off (to zero)
for (int dmxChannel = 1; dmxChannel < 513; dmxChannel++) {
myController.setChannel(dmxChannel, 0);
}
//send data
myController.sendPacket(receiverAddress);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment