#include <Arduino.h>
/*
Writes DMX data to channel 1
By: Dryw Wade
SparkFun Electronics
Date: 10/3/2023
License: GNU. See license file for more information but you can
basically do whatever you want with this code.
This example runs two servos and a number of LED's off of 5 DMX channels
Feel like supporting open source hardware?
Buy a board from SparkFun! https://www.sparkfun.com/products/15110
Hardware Connections:
Connect a Thing Plus board to the SparkFun DMX Shield, and connect a DMX XLR-3
cable between the shield and another device that outputs DMX data. You can use
a second board and shield running Example 2!
*/
// Inlcude DMX library
#include <SparkFunDMX.h>
// #include <esp32-hal-adc.h>
// #include <Ps3Controller.h>
#define JOYSTICK_PIN_X 35
#define JOYSTICK_PIN_Y 32
// #define DEBUG 1
// #if DEBUG
// #define Serial.print() Serial.print(__VA_ARGS__);
// #define Serial.println() Serial.println(__VA_ARGS__);
// #else
// #define serialprint(...)
// #define Serial.println(...)
// #endif
// #define POT_ADC A5
#define LED_BUILTIN 15
#define DMX_CHANNEL 1
#define MAX_CHANEL 9
#define ANALOG_RES 8
#define BIT_DEPTH 255
int buttonState = HIGH;
// #define POWERPIN0 A0 // select the output pin for the power relay
#define BUTTON_PIN 0
#define POWERPIN2 A2 // select the input pin for the potentiometer
#define POWERPIN3 A3 // select the input pin for the potentiometer
#define POWERPIN4 A4 // select the input pin for the potentiometer
#define motorPin A5
// Pin Definitions for ESP32 WROOM
#define CLOCK 5
// #define DATA0 19
#define DATA1 18
#define DATA2 27
#define PAN_CHANNEL 0
#define TILT_CHANNEL 1
#define GOBO_COLOR 2
#define GOBO_Channel 3
#define L_OPMODE 1
#define L_PATTERN 2
#define L_RZAXIS 3
#define L_RXAXIS 4
#define L_RYAXIS 5
#define L_XOFFSET 6
#define L_YOFFSET 7
#define L_XSIZE 8
#define L_YSIZE 9
#define L_DRAWEFCT 10
#define L_SPEED 11
#define L_COLOR 12
#define L_CSPEED 13
// Create serial port to be used for DMX interface. Exact implementation depends
// on platform, this example is for the ESP32
HardwareSerial dmxSerial(2);
// Enable pin for DMX shield (Free pin on Thing Plus or Feather pinout)
int enPin = 21;
// Number of DMX channels, can be up tp 512
uint16_t numChannels = 9;
// Create a counter as example data
int xAxis_1 = 128;
int yAxis_2 = 128;
int color_3 = 5;
int gobo_4 = 3;
int strobe_5 = 255;
int lux_6 = 255;
int speed_7 = 1;
int goFunction_8 = 0;
int effect_9 = 0;
bool PowerPin0 = 0;
bool PowerPin2 = 0;
bool PowerPin3 = 0;
bool PowerPin4 = 0;
bool motorPinVal = 0;
int gobo[9] = {xAxis_1, yAxis_2, color_3, gobo_4, strobe_5, lux_6, speed_7, goFunction_8, effect_9};
int laser[13] = {150, 1, 0, 0, 0, 0, 127, 127, 0, 127, 200, 128};
int player = 1;
int battery = 0;
SparkFunDMX dmx;
int magnitude = 4;
int minmax(int startValue, int change, int max = 255, int mult = 10)
{
startValue = ((startValue + (change / mult)) > max) ? max
: ((startValue + (change / mult)) < 0) ? 0
: (startValue + (change / mult));
return startValue;
}
void dmxSend()
{
// if (!Ps3.isConnected())
// return;
buttonState = !(buttonState);
digitalWrite(17, buttonState);
for (int chan = 0; chan < 9; chan++)
{
Serial.print(gobo[chan]);
Serial.print(" | ");
dmx.writeByte(gobo[chan], chan + 1);
}
Serial.println();
}
// void notify()
// {
// if (Ps3.event.button_up.up)
// {
// // Serial.println("Released the up button");
// gobo[GOBO_COLOR] = minmax(gobo[GOBO_COLOR], 7, 127, 1);
// // dmxSend();
// }
// // if (Ps3.event.button_down.right)
// // Serial.println("Started pressing the right button");
// if (Ps3.event.button_up.right)
// {
// gobo[GOBO_Channel] = minmax(gobo[GOBO_Channel], 7, 56, 1);
// // dmxSend();
// }
// if (Ps3.event.button_up.down)
// {
// gobo[GOBO_COLOR] = minmax(gobo[GOBO_COLOR], -7, 127, 1);
// // dmxSend();
// }
// if (Ps3.event.button_up.left)
// {
// gobo[GOBO_Channel] = minmax(gobo[GOBO_Channel], -7, 56, 1);
// }
// if (Ps3.event.button_down.select)
// // Serial.println("Started pressing the select button");
// // if (Ps3.event.button_up.select)
// {
// // Serial.println("Released the select button");
// PowerPin2 = !PowerPin2;
// digitalWrite(POWERPIN2, PowerPin2);
// // dmxSend();
// };
// if (Ps3.event.button_down.start)
// {
// // Serial.println("Released the start button");
// PowerPin0 = !PowerPin0;
// digitalWrite(POWERPIN0, PowerPin0);
// // dmxSend();
// }
// if (Ps3.event.button_down.ps)
// {
// gobo[0] = 128;
// gobo[1] = 128;
// gobo[2] = 28;
// gobo[3] = 10;
// gobo[4] = 0;
// // dmxSend();
// }
// if (abs(Ps3.event.analog_changed.stick.lx) + abs(Ps3.event.analog_changed.stick.ly) > 2)
// {
// gobo[PAN_CHANNEL] = minmax(gobo[PAN_CHANNEL], Ps3.data.analog.stick.lx);
// gobo[TILT_CHANNEL] = minmax(gobo[TILT_CHANNEL], Ps3.data.analog.stick.ly);
// }
// if (abs(Ps3.event.analog_changed.stick.rx) + abs(Ps3.event.analog_changed.stick.ry) > 2)
// {
// laser[L_XOFFSET] = minmax(laser[L_XOFFSET], Ps3.data.analog.stick.rx);
// laser[L_YOFFSET] = minmax(laser[L_YOFFSET], Ps3.data.analog.stick.ry);
// }
// //--------------- Analog D-pad button events ----------------
// //---------------------- Battery events ---------------------
// if (battery != Ps3.data.status.battery)
// {
// battery = Ps3.data.status.battery;
// Serial.print("The controller battery is ");
// if (battery == ps3_status_battery_charging)
// Serial.println("charging");
// else if (battery == ps3_status_battery_full)
// Serial.println("FULL");
// else if (battery == ps3_status_battery_high)
// Serial.println("HIGH");
// else if (battery == ps3_status_battery_low)
// Serial.println("LOW");
// else if (battery == ps3_status_battery_dying)
// Serial.println("DYING");
// else if (battery == ps3_status_battery_shutdown)
// Serial.println("SHUTDOWN");
// else
// Serial.println("UNDEFINED");
// }
// }
// void onConnect()
// {
// Serial.println("Connected.");
// // Ps3.setPlayer(1);
// }
void setup()
{
pinMode(LED_BUILTIN, OUTPUT);
pinMode(BUTTON_PIN, INPUT_PULLUP);
pinMode(17, OUTPUT);
pinMode(JOYSTICK_PIN_X, ANALOG);
pinMode(JOYSTICK_PIN_Y, ANALOG);
// pinMode(motorPin, OUTPUT);
Serial.begin(115200);
// buttonState = digitalRead(BUTTON_PIN);
// Ps3.attach(notify);
// Ps3.attachOnConnect(onConnect);
// Ps3.begin("78:18:81:57:05:FD");
Serial.println("Ready.");
Serial.println("SparkFun DMX Example 1 - Output");
// Begin DMX serial port
dmxSerial.begin(DMX_BAUD, DMX_FORMAT);
// Begin DMX driver
dmx.begin(dmxSerial, enPin, numChannels);
// Set communicaiton direction, which can be changed on the fly as needed
dmx.setComDir(DMX_WRITE_DIR);
Serial.println("DMX initialized!");
// pinMode(BUTTON_PIN, INPUT_PULLUP);
analogReadResolution(ANALOG_RES);
// xAxisDif = analogRead(xAxisPin);
// yAxisDif = analogRead(yAxisPin);
// dmx.writeBytes(gobo, 1, 9);
// dmx.update;
// calibrate();
}
void loop()
{
int xAxis = map(analogRead(JOYSTICK_PIN_X),0,255,-128,128);
int yAxis = map(analogRead(JOYSTICK_PIN_Y),0,255,-128,128);
// if (abs(Ps3.event.analog_changed.stick.lx) + abs(Ps3.event.analog_changed.stick.ly) > 2)
Serial.println(xAxis);
Serial.println(yAxis);
if (abs(xAxis) > 3)
gobo[PAN_CHANNEL] = minmax(gobo[PAN_CHANNEL], xAxis);
if (abs(yAxis) > 3)
gobo[TILT_CHANNEL] = minmax(gobo[TILT_CHANNEL], yAxis);
if (!digitalRead(BUTTON_PIN))
{
gobo[0] = 128;
gobo[1] = 128;
gobo[2] = 28;
gobo[3] = 10;
gobo[4] = 0;
// dmxSend();
}
// if (!Ps3.isConnected())
// return;
dmxSend();
dmx.update();
delay(1000);
}