Add Artnet joystick control program
This commit is contained in:
65
M5/ArtnetJoystickControl/ArtnetJoystickControl.ino
Normal file
65
M5/ArtnetJoystickControl/ArtnetJoystickControl.ino
Normal file
@@ -0,0 +1,65 @@
|
|||||||
|
#include <WiFi.h>
|
||||||
|
#include <ArtnetWifi.h>
|
||||||
|
|
||||||
|
// ==============================
|
||||||
|
// 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);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user