### DMXSerial Library Setup Source: https://www.mathertel.de/Arduino/DMXSerial.aspx This is a basic setup for using the DMXSerial library. It initializes the library in DMX sending mode and updates the DMX buffer in a loop. ```cpp #include void setup() { // Initialize DMXSerial in DMXController mode DMXSerial.initController(); } void loop() { // Update DMX values here // Example: Set channel 1 to 255 DMXSerial.setChannelValue(1, 255); // DMXSerial.write(); // This is called automatically in controller mode } ``` -------------------------------- ### DmxSerialNeoPixels Example Source: https://www.mathertel.de/Arduino/DMXSerial.aspx This example shows how to receive DMX data with an Arduino and send the received values to a series of NeoPixel or WS2811 LEDs. It initializes the library in receiver mode. ```cpp #include #include #define LED_PIN 6 #define LED_COUNT 30 Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800); void setup() { DMXSerial.initReceiver(); strip.begin(); strip.show(); // Initialize all pixels to 'off' } void loop() { // Read DMX values for each LED for (int i = 0; i < LED_COUNT; i++) { int red = DMXSerial.getChannelValue(i * 3 + 1); int green = DMXSerial.getChannelValue(i * 3 + 2); int blue = DMXSerial.getChannelValue(i * 3 + 3); strip.setPixelColor(i, strip.Color(red, green, blue)); } strip.show(); } ``` -------------------------------- ### DMXReceiver Example Source: https://www.mathertel.de/Arduino/DMXSerial.aspx This example demonstrates how to receive DMX data for 3 channels, typically for an RGB LED. It initializes the library in receiver mode and reads channel values. ```cpp #include void setup() { // Initialize DMXSerial in DMXReceiver mode DMXSerial.initReceiver(); } void loop() { // Read DMX values int red = DMXSerial.getChannelValue(1); int green = DMXSerial.getChannelValue(2); int blue = DMXSerial.getChannelValue(3); // Use the received values for your LEDs or other devices // analogWrite(RED_PIN, red); // analogWrite(GREEN_PIN, green); // analogWrite(BLUE_PIN, blue); } ``` -------------------------------- ### DMXSerialFlow Example Source: https://www.mathertel.de/Arduino/DMXSerial.aspx This example demonstrates sending a complex RGB color pattern over DMX. It continuously changes 60 * 3 values for a series of 3-channel RGB devices. ```cpp #include unsigned int DMXSEND[512]; unsigned long lastUpdate = 0; void setup() { // Initialize DMXSerial in DMXController mode DMXSerial.initController(); } void loop() { unsigned long now = millis(); if (now - lastUpdate > 50) { for (int i = 1; i <= 60 * 3; i++) { DMXSEND[i] = random(256); } DMXSerial.setBuffer(DMXSEND); lastUpdate = now; } } ``` -------------------------------- ### DMXSerialSend Example Source: https://www.mathertel.de/Arduino/DMXSerial.aspx This example shows how to implement a DMX controller that continuously sends changing values for 3 channels, suitable for an RGB LED. It initializes the library in controller mode and updates values in the loop. ```cpp #include unsigned int DMXSEND[512]; unsigned long lastUpdate = 0; void setup() { // Initialize DMXSerial in DMXController mode DMXSerial.initController(); } void loop() { // Update DMX values constantly unsigned long now = millis(); if (now - lastUpdate > 50) { for (int i = 1; i <= 3; i++) { DMXSEND[i] = random(256); } DMXSerial.setBuffer(DMXSEND); lastUpdate = now; } } ``` -------------------------------- ### DMXProbe Mode Example Source: https://www.mathertel.de/Arduino/DMXSerial.aspx This example demonstrates the DMXProbe mode, where the Arduino does not actively listen for data. The receive() function must be called to check for and retrieve incoming DMX packets. ```cpp #include void setup() { // Initialize DMXSerial in DMXProbe mode DMXSerial.initProbe(); } void loop() { // Check for incoming DMX data if (DMXSerial.receive()) { // Process received DMX data int channel1Value = DMXSerial.getChannelValue(1); // ... process other channels } } ``` -------------------------------- ### State 0: Waiting for Button Press Source: https://www.mathertel.de/Arduino/OneButton Initial state where the machine waits for the button to be pressed. If pressed, it transitions to state 1 and records the start time. ```c++ if (_state == 0) { // waiting for One pin being pressed. if (buttonLevel == ButtonDown) { _state = 1; // step to state 1 _startTime = now; // remember starting time } // if ``` -------------------------------- ### Simple OneButton Sketch Source: https://www.mathertel.de/Arduino/OneButton A basic example demonstrating how to use the OneButton library to detect a double-click event and toggle an LED. Ensure the button is connected to A1 and ground, and the LED to pin 13. ```arduino /* S01_SimpleOneButton Simple OneButton sketch that shows how to ??? The circuit: * Connect a pushbutton to pin A1 (ButtonPin) and ground * and see results on pin 13 (StatusPin). * 03.03.2011 created by Matthias Hertel */ #include "OneButton.h" // Setup a new OneButton on pin A1. OneButton button(A1); // setup code here, to run once: void setup() { // enable the standard led on pin 13. pinMode(13, OUTPUT); // sets the digital pin as output // link the doubleclick function to be called on a doubleclick event. button.attachDoubleClick(doubleclick); } // setup // main code here, to run repeatedly: void loop() { // keep watching the push button: button.tick(); // You can implement other code in here or just wait a while delay(10); } // loop // this function will be called when the button was pressed 2 times in a short timeframe. void doubleclick() { static int m = LOW; // reverse the LED m = !m; digitalWrite(13, m); } // doubleclick ``` -------------------------------- ### RDM SET Identify Command Sample Source: https://www.mathertel.de/Arduino/DMXSerial2 This is a sample RDM SET Identify command sent to a device. It includes the start code, packet length, device ID, controller ID, transaction counter, response type, command details, and data. ```text >> CC 01 19 234520121102 4164FF000001 nn 01 00 0000 30 1000 01 01 cscs ``` -------------------------------- ### DMX Sending Reset Signal (Setup) Source: https://www.mathertel.de/Arduino/DMXSerial.aspx To send the DMX BREAK signal, which requires a low level for over 88 µsec, a slower baud rate is temporarily initialized. A 0-byte is then sent, followed by resetting the baud rate to the standard 250,000 baud. ```c++ // setup a slower baud rate _DMXSerialBaud(115200); // and send a 0 byte _DMXSerialWrite((uint8_t)0); ``` -------------------------------- ### Getting Current Time Source: https://www.mathertel.de/Arduino/OneButton This code demonstrates how to get the current relative time in milliseconds using the millis() function, which is crucial for timing events like long presses within the OneButton library. ```c++ unsigned long now = millis(); // current (relative) time in msecs. ``` -------------------------------- ### DMXSerial noDataSince() Usage Source: https://www.mathertel.de/Arduino/DMXSerial.aspx This function returns the number of milliseconds elapsed since the last DMX start packet was received. It is useful for detecting disconnected devices or signal loss by checking if too much time has passed without a valid packet. ```c++ DMXSerial.noDataSince() ``` -------------------------------- ### State Variables for Button Press Detection Source: https://www.mathertel.de/Arduino/OneButton Defines static variables to maintain the current state and the start time of a button press across function calls. Use these to track the button's interaction state. ```c++ int _state = 0; // starting with state 0: waiting for button to be pressed unsigned long _startTime; // will be set in state 1 ``` -------------------------------- ### Define and Print PROGMEM String Source: https://www.mathertel.de/Arduino/radio Defines a string in PROGMEM and prints it to the client. Note that this can lead to inefficient network packet usage as each character is sent individually. ```cpp #define HTTPERR_404 F("HTTP/1.1 404 Not Found\r\n") _client.print(HTTPERR_404); ``` -------------------------------- ### RDM Response Sample Source: https://www.mathertel.de/Arduino/DMXSerial2 This is a sample RDM response from a device to a SET Identify command. It mirrors the command structure but indicates a successful response. ```text << CC 01 18 4164FF000001 234520121102 nn 00 00 0000 31 1000 00 cscs ``` -------------------------------- ### Initialize OneButton Library for Menu Button Source: https://www.mathertel.de/Arduino/radio Initialize the OneButton library for a menu button connected to analog pin A10. The 'true' parameter indicates a pull-up resistor is enabled. ```cpp OneButton menuButton(A10, true); ``` -------------------------------- ### Send Multiple Client.print Calls Source: https://www.mathertel.de/Arduino/radio Sends multiple small strings using separate client.print calls, resulting in numerous small network packets. This is an inefficient method for sending data. ```cpp // Response no and send not found html void respondHeaderNotFound() { client.print(HTTPERR_404); client.print(HTTP_GENERAL); client.print(HTTP_ENDHEAD); } // respondHeaderNotFound() ``` -------------------------------- ### Configure DMXSerial to use Serial Port 1 on Arduino MEGA 2560 Source: https://www.mathertel.de/Arduino/DMXSerial.aspx To use the DMXSerial library with Serial Port 1 on an Arduino MEGA 2560, uncomment the following line in the DMXSerial_avr.h file. ```c++ #define DMX_USE_PORT1 ``` -------------------------------- ### Include LiquidCrystal_PCF8574 Library Source: https://www.mathertel.de/Arduino/radio Include this library for controlling an LCD display via the I2C bus using a PCF8574 decoder chip. Ensure this line replaces the standard LiquidCrystal library include. ```cpp #include ``` -------------------------------- ### Send Data Using StringBuffer Source: https://www.mathertel.de/Arduino/radio Collects multiple text fragments into a StringBuffer and sends them out in a single call to the client. This significantly reduces network overhead and improves performance. ```cpp // Response no and send not found html void respondHeaderNotFound() { StringBuffer sout = StringBuffer(_writeBuffer, sizeof(_writeBuffer)); sout.append(HTTPERR_404); sout.append(HTTP_GENERAL); sout.append(HTTP_ENDHEAD); client.print(_writeBuffer); } // respondHeaderNotFound() ``` -------------------------------- ### State 3: Second Click Detected - Waiting for Release Source: https://www.mathertel.de/Arduino/OneButton State after a second click has been detected within the allowed timeframe. It waits for the button to be released to confirm the double-click and then resets the state machine. ```c++ } else if (_state == 3) { // waiting for One pin being released finally. if (buttonLevel == ButtonUp) { // this was a 2 click sequence. if (_doubleClickFunc) _doubleClickFunc(); _state = 0; // restart. } // if ``` -------------------------------- ### Configure DMX Serial Baud Rate and Settings Source: https://www.mathertel.de/Arduino/DMXSerial.aspx Initializes the hardware serial port for DMX communication with specified baud rate, 8 data bits, and 2 stop bits. This function calculates and sets the baud rate registers and configures the control register for the DMX transmission mode. ```c // initialize the Hardware serial port with the given baud rate // using 8 data data bits an 2 stop bits. void _DMXSerialBaud(long baud) { uint16_t baud_setting; // calculate baud settings baud_setting = (F_CPU / 8 / baud - 1) / 2; // assign the baud_setting to the USART Baud Rate Register UBRRnH = baud_setting >> 8; UBRRnL = baud_setting; // 2 stop bits and 8 bit character size, no parity UCSRnC = (1< _startTime + _clickTicks) { // this was only a single short click if (_clickFunc) _clickFunc(); _state = 0; // restart. } else if (buttonLevel == ButtonDown) { _state = 3; // step to state 3 } // if ``` -------------------------------- ### DMX Sending Reset Signal (Data Transmission) Source: https://www.mathertel.de/Arduino/DMXSerial.aspx After the DMX BREAK signal is sent, the serial communication is switched back to the standard 250,000 baud rate to transmit the actual data bytes. A 0-byte is written to initiate this phase. ```c++ Serial.begin(250000); Serial.write((uint8_t)0); ``` -------------------------------- ### State 1: Button Pressed - Waiting for Release or Long Press Source: https://www.mathertel.de/Arduino/OneButton State where the button is held down. It waits for the button to be released (transition to state 2) or for a long press duration to be exceeded (transition to state 6 and call press function). ```c++ } else if (_state == 1) { // waiting for One pin being released. if (buttonLevel == ButtonUp) { _state = 2; // step to state 2 } else if ((buttonLevel == ButtonDown) && (now > _startTime + _pressTicks)) { if (_pressFunc) _pressFunc(); _state = 6; // step to state 6 } // if ``` -------------------------------- ### State 6: Long Press Detected - Waiting for Release Source: https://www.mathertel.de/Arduino/OneButton State after a long press has been detected. It waits for the button to be released to reset the state machine. ```c++ } else if (_state == 6) { // waiting for One pin being release after long press. if (buttonLevel == ButtonUp) { _state = 0; // restart. } // if ``` -------------------------------- ### Calculate Rotary Encoder State using Addition Source: https://www.mathertel.de/Arduino/RotaryEncoder This snippet shows a method for calculating the current state of a rotary encoder by adding the values from its two signal lines. This approach is less efficient than bitwise operations but conceptually simpler. ```cpp thisState = sig1 + 2 * sig2; ``` -------------------------------- ### DMXSerial Sending Interrupt Routine Source: https://www.mathertel.de/Arduino/DMXSerial.aspx This interrupt routine is invoked by hardware after a byte is fully transmitted. It manages the transmission of the next byte in the DMX data stream. In DMX Controller mode, it also handles restarting the reset signal transmission once a complete DMX package has been sent. ```c++ ISR(USART_UDRE_vect) { ... } ``` -------------------------------- ### Calculate Rotary Encoder State using Bitwise Operations Source: https://www.mathertel.de/Arduino/RotaryEncoder This snippet demonstrates an efficient way to calculate the current state of a rotary encoder using bitwise operations. It combines the states of two signal lines into a single number representing the encoder's position. ```cpp thisState = sig1 | (sig2 << 1); ``` -------------------------------- ### DMX Serial Port Configuration for Stop Bits Source: https://www.mathertel.de/Arduino/DMXSerial.aspx The DMX protocol requires two stop bits, which differs from the standard single stop bit used by serial ports. This setting must be adjusted directly in the hardware control registers for serial port 0. ```c++ UCSR0C = (1<>2; ``` -------------------------------- ### Interrupt Service Routine for Rotary Encoder Source: https://www.mathertel.de/Arduino/RotaryEncoder A simple Interrupt Service Routine (ISR) that calls the encoder's tick() function to check for state changes. This should be kept minimal and fast. ```C++ void checkPosition() { encoder->tick(); // just call tick() to check the state. } ``` -------------------------------- ### Rotary Encoder State Calculation Source: https://www.mathertel.de/Arduino/RotaryEncoderLibrary.aspx Calculates the current state of a rotary encoder using two signal lines. This method is effective for tracking encoder positions. ```C++ thisState = sig1 + 2 * sig2; ``` ```C++ thisState = sig1 | (sig2 << 1); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.