Heyo, kinda weird thing
I'm currently working on a project that has a Master Arduino (uno) that get's a dmx signal and interprets it in values for PWM pins of the Slave arduino (nano) which should give out the PWM values and turn LEDs on and off pretty much.
The communication normaly is good, i've let it run quite a few times with just turning the leds off and on in a sinus wave, i've also tried just that with the dmx shiel already so that i know that it doesn't block the i2c communication.
But as soon as i don't let it run a manual sinus wave and try it the dmx Way it doesn't do anything.... the master arduino receives the right signals, i've seen that, but apparently it doesn't work...
my ideas would be
1: It doesn't turn the dmx values into the pwm values or
2: sth in the code with dmx is blocking the i2c communication
but i'm more thinking the 2nd point bc i've let the serial monitor run on the slave arduino and apparently it didn't receive anything from the master...
can anybody help?
Master arduino code (Arduino Uno)
#include <Wire.h>
#include <Conceptinetics.h>
#include <TM1637Display.h>
#include <EEPROM.h>
// Pins for the buttons
const int buttonMinusPin = A0; // Change to analog pin
const int buttonPlusPin = A1; // Change to analog pin
// Pins for the TM1637 Display
const int CLK = 2; // Pin for CLK
const int DIO = 3; // Pin for DIO
// Variables for the buttons
int lastButtonMinusState = HIGH;
int lastButtonPlusState = HIGH;
unsigned long lastDebounceTimeMinus = 0;
unsigned long lastDebounceTimePlus = 0;
unsigned long debounceDelay = 50;
unsigned long buttonPressStartTimeMinus = 0;
unsigned long buttonPressStartTimePlus = 0;
bool isButtonPressedMinus = false;
bool isButtonPressedPlus = false;
// DMX Slave object
#define DMX_SLAVE_CHANNELS 3 // Number of DMX channels
#define RXEN_PIN 2 // RXEN Pin (Receive enable)
DMX_Slave dmx_slave(DMX_SLAVE_CHANNELS, RXEN_PIN); // Create DMX_Slave object
// TM1637 Display object
TM1637Display display(CLK, DIO);
// EEPROM memory address for DMX address
const int eepromAddress = 0;
void setup() {
Wire.begin(); // Start I2C
pinMode(buttonMinusPin, INPUT_PULLUP);
pinMode(buttonPlusPin, INPUT_PULLUP);
pinMode(10, OUTPUT); // Define pin 10 as output
digitalWrite(10, HIGH); // Output 5V continuously
// Initialize DMX Slave
dmx_slave.enable(); // Enable DMX Slave
// Read DMX start address from EEPROM
int storedAddress = EEPROM.read(eepromAddress);
if (storedAddress < 1 || storedAddress > 512 - DMX_SLAVE_CHANNELS + 1) {
storedAddress = 1; // Default address if invalid address in EEPROM
}
dmx_slave.setStartAddress(storedAddress); // Set DMX start address
// Initialize TM1637 Display
display.setBrightness(0x0f); // Maximum brightness
// Show initial DMX address on display
display.showNumberDec(dmx_slave.getStartAddress(), true);
}
void loop() {
handleButtonPresses();
// Example: Map DMX values to PWM values and send
int dmxAddress = 1; // Example channel 1
int dmxValue = dmx_slave.getChannelValue(dmxAddress);
int pwmValue = map(dmxValue, 0, 255, 0, 1023);
sendPWMValues(pwmValue, pwmValue, pwmValue); // Example: send same PWM values to both devices
// Example: Display DMX address on the display
display.showNumberDec(dmx_slave.getStartAddress(), true);
// Wait a bit to control the update speed
delay(2); // Reduce delay to update display 5x faster
}
void handleButtonPresses() {
int buttonMinusState = analogRead(buttonMinusPin) < 512 ? LOW : HIGH;
int buttonPlusState = analogRead(buttonPlusPin) < 512 ? LOW : HIGH;
unsigned long currentTime = millis();
// Minus button
if (buttonMinusState == LOW && lastButtonMinusState == HIGH) {
lastDebounceTimeMinus = currentTime;
buttonPressStartTimeMinus = currentTime;
isButtonPressedMinus = true;
} else if (buttonMinusState == HIGH && lastButtonMinusState == LOW) {
if (isButtonPressedMinus && currentTime - buttonPressStartTimeMinus >= debounceDelay) {
int currentAddress = dmx_slave.getStartAddress();
dmx_slave.setStartAddress(max(1, currentAddress - 1));
EEPROM.write(eepromAddress, dmx_slave.getStartAddress()); // Save DMX start address in EEPROM
isButtonPressedMinus = false;
}
} else if (buttonMinusState == LOW && isButtonPressedMinus && currentTime - buttonPressStartTimeMinus >= 1000) {
unsigned long interval = map(currentTime - buttonPressStartTimeMinus, 1000, 2000, 20, 2);
interval = max(interval, 2); // Minimum interval of 2ms
if (currentTime - buttonPressStartTimeMinus >= interval) {
int currentAddress = dmx_slave.getStartAddress();
dmx_slave.setStartAddress(max(1, currentAddress - 1));
EEPROM.write(eepromAddress, dmx_slave.getStartAddress()); // Save DMX start address in EEPROM
buttonPressStartTimeMinus = currentTime;
}
}
lastButtonMinusState = buttonMinusState;
// Plus button
if (buttonPlusState == LOW && lastButtonPlusState == HIGH) {
lastDebounceTimePlus = currentTime;
buttonPressStartTimePlus = currentTime;
isButtonPressedPlus = true;
} else if (buttonPlusState == HIGH && lastButtonPlusState == LOW) {
if (isButtonPressedPlus && currentTime - buttonPressStartTimePlus >= debounceDelay) {
int currentAddress = dmx_slave.getStartAddress();
dmx_slave.setStartAddress(min(512 - DMX_SLAVE_CHANNELS + 1, currentAddress + 1));
EEPROM.write(eepromAddress, dmx_slave.getStartAddress()); // Save DMX start address in EEPROM
isButtonPressedPlus = false;
}
} else if (buttonPlusState == LOW && isButtonPressedPlus && currentTime - buttonPressStartTimePlus >= 1000) {
unsigned long interval = map(currentTime - buttonPressStartTimePlus, 1000, 2000, 20, 2);
interval = max(interval, 2); // Minimum interval of 2ms
if (currentTime - buttonPressStartTimePlus >= interval) {
int currentAddress = dmx_slave.getStartAddress();
dmx_slave.setStartAddress(min(512 - DMX_SLAVE_CHANNELS + 1, currentAddress + 1));
EEPROM.write(eepromAddress, dmx_slave.getStartAddress()); // Save DMX start address in EEPROM
buttonPressStartTimePlus = currentTime;
}
}
lastButtonPlusState = buttonPlusState;
}
void sendPWMValues(int pwmValue1, int pwmValue2, int pwmValue3) {
Wire.beginTransmission(0x01);
Wire.write(pwmValue1 >> 8); // Send high byte of PWM value for pin 1
Wire.write(pwmValue1 & 0xFF); // Send low byte of PWM value for pin 1
Wire.write(pwmValue2 >> 8); // Send high byte of PWM value for pin 2
Wire.write(pwmValue2 & 0xFF); // Send low byte of PWM value for pin 2
Wire.write(pwmValue3 >> 8); // Send high byte of PWM value for pin 3
Wire.write(pwmValue3 & 0xFF); // Send low byte of PWM value for pin 3
Wire.endTransmission();
}
Slave code:
#include <Wire.h>
// Definition of PWM pins
const int pwmPin1 = 3;
const int pwmPin2 = 5;
const int pwmPin3 = 6;
// Timeout constants
const unsigned long timeoutInterval = 500; // 500 ms timeout interval
unsigned long lastReceiveTime = 0; // Time of last received data
void setup() {
// Set PWM pins as output
pinMode(pwmPin1, OUTPUT);
pinMode(pwmPin2, OUTPUT);
pinMode(pwmPin3, OUTPUT);
Wire.begin(0x01); // Start I2C with address 0x01
Wire.onReceive(receiveEvent); // Register the receive event handler
// Turn off LEDs initially
analogWrite(pwmPin1, 0);
analogWrite(pwmPin2, 0);
analogWrite(pwmPin3, 0);
}
void loop() {
// Check if timeout has been exceeded
if (millis() - lastReceiveTime > timeoutInterval) {
// Turn off the LEDs
analogWrite(pwmPin1, 0);
analogWrite(pwmPin2, 0);
analogWrite(pwmPin3, 0);
}
}
void receiveEvent(int howMany) {
if (howMany == 3) {
int pwmValue1 = Wire.read(); // Receive PWM value for pin 1
int pwmValue2 = Wire.read(); // Receive PWM value for pin 2
int pwmValue3 = Wire.read(); // Receive PWM value for pin 3
// Set PWM values for the pins
analogWrite(pwmPin1, pwmValue1);
analogWrite(pwmPin2, pwmValue2);
analogWrite(pwmPin3, pwmValue3);
// Update the time of last received data
lastReceiveTime = millis();
}
}
sorry for the long code wall... hope somebody can help