i have like half of the code from it, yeah
better you say it in first post.
so, what piece is from you ?
and, is this weird idea with second arduino came from ChatGpt too?
the slave code is from me, the I2C communication, the EEPROM and for the display
i tried to do the buttons too but made some mistakes so i've let it run over the code for the buttons
all the code for dmx is gpt tho
did you tested your "slave"-sketch? does it what you expected?
i did, yeah
tested it with this code on the "master" and it did what i wanted without any problems
#include <Wire.h>
#include <math.h>
// Constants for the sine wave
const float pi = 3.14159265;
const int amplitude = 127;
const int offset = 128;
const int period = 360;
void setup() {
Wire.begin();
}
void loop() {
for (int i = 0; i < period; i++) {
int pwmValue1 = offset + amplitude * sin((2 * pi * i) / period);
int pwmValue2 = offset + amplitude * sin((2 * pi * (i + period / 3)) / period);
int pwmValue3 = offset + amplitude * sin((2 * pi * (i + 2 * period / 3)) / period);
// address 0x01
sendPWMValues(0x01, pwmValue1, pwmValue2, pwmValue3);
// address 0x02
sendPWMValues(0x02, pwmValue1, pwmValue2, pwmValue3);
delay(10);
}
}
void sendPWMValues(byte address, int value1, int value2, int value3) {
Wire.beginTransmission(address);
Wire.write(value1);
Wire.write(value2);
Wire.write(value3);
Wire.endTransmission();
}
cool, use this sketch in slave and let check what shows serial monitor
#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() {
Serial.begin(115200);
Serial.println("start\n");
// 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) {
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();
Serial.print(pwmValue1);
Serial.print('\t');
Serial.print(pwmValue2);
Serial.print('\t');
Serial.println(pwmValue3);
Serial.flush();
}
this
1 181 200
1 183 199
1 185 197
1 187 195
1 189 193
1 191 191
1 193 189
1 195 187
1 197 185
1 199 183
1 200 181
1 202 179
1 204 177
2 206 175
2 207 173
2 209 171
3 211 169
3 212 167
4 214 165
4 216 163
5 217 160
5 219 158
6 220 156
7 222 154
7 223 152
8 225 150
9 226 147
10 228 145
11 229 143
11 230 141
12 232 139
13 233 136
14 234 134
15 235 132
16 236 130
18 237 128
19 239 125
20 240 123
21 241 121
22 242 119
23 243 116
25 244 114
26 244 112
27 245 110
29 246 108
30 247 105
32 248 103
33 248 101
35 249 99
36 250 97
38 250 95
39 251 92
41 251 90
43 252 88
44 252 86
46 253 84
48 253 82
49 253 80
51 254 78
53 254 76
55 254 74
56 254 72
58 254 70
60 254 68
62 254 66
64 255 64
66 254 62
68 254 60
70 254 58
72 254 56
74 254 55
76 254 53
78 254 51
80 253 49
82 253 48
84 253 46
86 252 44
88 252 43
90 251 41
92 251 39
95 250 38
97 250 36
99 249 35
101 248 33
103 248 32
105 247 30
108 246 29
110 245 27
112 244 26
114 244 25
116 243 23
119 242 22
121 241 21
123 240 20
125 239 19
128 237 18
130 236 16
132 235 15
134 234 14
136 233 13
139 232 12
141 230 11
143 229 11
145 228 10
147 226 9
150 225 8
152 223 7
154 222 7
156 220 6
158 219 5
160 217 5
163 216 4
165 214 4
167 212 3
169 211 3
171 209 2
173 207 2
175 206 2
177 204 1
179 202 1
181 200 1
183 199 1
185 197 1
187 195 1
189 193 1
191 191 1
193 189 1
195 187 1
197 185 1
199 183 1
200 181 1
202 179 1
204 177 1
206 175 2
207 173 2
209 171 2
211 169 3
212 167 3
214 165 4
216 163 4
217 160 5
219 158 5
220 156 6
222 154 7
223 152 7
225 150 8
226 147 9
228 145 10
229 143 11
230 141 11
232 139 12
233 136 13
234 134 14
235 132 15
236 130 16
237 127 18
239 125 19
240 123 20
241 121 21
242 119 22
243 116 23
244 114 25
244 112 26
245 110 27
246 108 29
247 105 30
248 103 32
248 101 33
249 99 35
250 97 36
250 95 38
251 92 39
251 90 41
252 88 43
252 86 44
253 84 46
253 82 48
253 80 49
254 78 51
254 76 53
254 74 55
254 72 56
254 70 58
254 68 60
254 66 62
255 64 64
254 62 66
254 60 68
254 58 70
254 56 72
254 55 74
254 53 76
254 51 78
253 49 80
253 48 82
253 46 84
252 44 86
252 43 88
251 41 90
251 39 92
250 38 95
250 36 97
249 35 99
248 33 101
248 32 103
247 30 105
246 29 108
245 27 110
244 26 112
244 25 114
243 23 116
242 22 119
241 21 121
240 20 123
239 19 125
237 18 128
236 16 130
235 15 132
234 14 134
233 13 136
232 12 139
230 11 141
229 11 143
228 10 145
226 9 147
225 8 150
223 7 152
222 7 154
220 6 156
219 5 158
217 5 160
216 4 163
214 4 165
212 3 167
211 3 169
209 2 171
207 2 173
206 2 175
204 1 177
202 1 179
200 1 181
199 1 183
197 1 185
195 1 187
193 1 189
191 1 191
189 1 193
187 1 195
185 1 197
183 1 199
181 1 200
179 1 202
177 1 204
175 2 206
173 2 207
171 2 209
169 3 211
167 3 212
165 4 214
163 4 216
160 5 217
158 5 219
156 6 220
154 7 222
152 7 223
150 8 225
147 9 226
145 10 228
143 11 229
141 11 230
139 12 232
136 13 233
134 14 234
132 15 235
130 16 236
127 18 237
125 19 239
123 20 240
121 21 241
119 22 242
116 23 243
114 25 244
112 26 244
110 27 245
108 29 246
105 30 247
103 32 248
101 33 248
99 35 249
97 36 250
95 38 250
92 39 251
90 41 251
88 43 252
86 44 252
84 46 253
82 48 253
80 49 253
78 51 254
76 53 254
74 55 254
72 56 254
70 58 254
68 60 254
66 62 254
64 64 255
62 66 254
60 68 254
58 70 254
56 72 254
55 74 254
53 76 254
51 78 254
49 80 253
48 82 253
46 84 253
44 86 252
43 88 252
41 90 251
39 92 251
38 95 250
36 97 250
35 99 249
33 101 248
32 103 248
30 105 247
29 108 246
27 110 245
26 112 244
25 114 244
23 116 243
22 119 242
21 121 241
20 123 240
19 125 239
18 128 237
16 130 236
15 132 235
14 134 234
13 136 233
12 139 232
11 141 230
11 143 229
10 145 228
9 147 226
8 150 225
7 152 223
7 154 222
6 156 220
5 158 219
5 160 217
4 163 216
4 165 214
3 167 212
3 169 211
2 171 209
2 173 207
2 175 206
1 177 204
1 179 202
1 181 200
1 183 199
1 185 197
1 187 195
1 189 193
1 191 191
1 193 189
1 195 187
1
so... working fine? ig
cool, then let see what this master do
#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);
sendPWMValues(255, 255, 255); // Example: send same PWM values to both devices
// Example: Display DMX address on the display
display.showNumberDec(dmxValue, 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 & 0xFF); // Send low byte of PWM value for pin 1
Wire.write(pwmValue2 & 0xFF); // Send low byte of PWM value for pin 2
Wire.write(pwmValue3 & 0xFF); // Send low byte of PWM value for pin 3
Wire.endTransmission();
}
putting this out constantly
255 255 255
255 255 255
255 255 255
255 255 255
255 255 255
so that's also how it should be for what i see
what shows display on master?
it showed 0001 for a short time but went 0000 after a few seconds and doesn't change
went to 255 now suddenly after plugging dmx out and in again
is dmx_slave.getStartAddress()
is dmx_slave.getChannelValue(dmxAddress)
k, yeah, thought so
do you can send some value thru DMX ? so it is shown in display, 222 for example?
it's receiving the signal, the display shows that
now the slave only needs to get it too
new master
#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
Serial.begin(115200);
Serial.println("start\n");
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);
delay(1000);
}
void loop() {
//handleButtonPresses();
// Example: Map DMX values to PWM values and send
int dmxAddress = 1; // Example channel 1
int dmxValue = dmx_slave.getChannelValue(dmxAddress);
sendPWMValues(dmxValue, dmxValue, dmxValue); // Example: send same PWM values to both devices
// Example: Display DMX address on the display
display.showNumberDec(dmxValue, 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 & 0xFF); // Send low byte of PWM value for pin 1
Wire.write(pwmValue2 & 0xFF); // Send low byte of PWM value for pin 2
Wire.write(pwmValue3 & 0xFF); // Send low byte of PWM value for pin 3
Wire.endTransmission();
Serial.print(pwmValue1);
Serial.print('\t');
Serial.print(pwmValue2);
Serial.print('\t');
Serial.println(pwmValue3);
Serial.flush();
}
sadly no serial bc with the dmx library apparently that won't work and gives me a compiling error. (had that earlier and took some time to find out why)
i'll try it without that
you send only one value for one channel, right?
so, you must send on three channels for each PWM, right?
you can put discrete LED in Uno board to same 3,5,6 pins and see on it what value is corresponding
it should send the value of the 1st dmx channel (the dmx start address) to PWM pin 1, the one from the second (dmx start address +1) to the second pwm pin and the third is dmx start adress +2