diff --git a/M5/ArtnetJoystickControl/ArtnetJoystickControl.ino b/M5/ArtnetJoystickControl/ArtnetJoystickControl.ino new file mode 100644 index 0000000..4cda7a1 --- /dev/null +++ b/M5/ArtnetJoystickControl/ArtnetJoystickControl.ino @@ -0,0 +1,65 @@ +#include +#include + +// ============================== +// Sketch configuration +// ============================== +const char* wifi_ssid = ""; +const char* wifi_psk = ""; +const char* artnet_target = ""; +const int artnet_universe = 0; + +const int artnet_channel_x = 0; +const int artnet_channel_y = 0; + +// These are valid for M5Stack +const int joystick_pin_x = 0; +const int joystick_pin_y = 0; +// ============================== + +ArtnetWifi Artnet; + +void setup() { + // Initialize serial + Serial.begin(115200); + + // Connect to Wifi + Serial.printf("Connecting to WiFi (SSID: %s)...", wifi_ssid); + WiFi.begin(wifi_ssid, wifi_psk); + if (WiFi.waitForConnectResult() != WL_CONNECTED) + { + Serial.println("Failed to connect. Stopping."); + while(true) delay(1000); + } + Serial.println("Connected!"); + + // Initialize the Artnet library + Artnet.begin(artnet_target); + Artnet.setLength(512); // Full DMX packet + Artnet.setUniverse(artnet_universe); + + // Setup pins + pinMode(joystick_pin_x, INPUT); + pinMode(joystick_pin_y, INPUT); + // Make analogRead resolution only 8 bits, + // so we can use the resulting value directly in Artnet (0-255) + analogReadResolution(8); +} + +void loop() { + // First get the current joystick position via analogRead + int joy_x = analogRead(joystick_pin_x); + int joy_y = analogRead(joystick_pin_y); + + // Optionally modify the value range using map from 0-255 to something else + //joy_x = map(joy_x, 0, 255, 100, 200); + //joy_y = map(joy_y, 0, 255, 100, 200); + + // Then set the corresponding Artnet channels + Artnet.setByte(artnet_channel_x, joy_x); + Artnet.setByte(artnet_channel_y, joy_y); + // And send the packet + Artnet.write(); + + delay(100); +}