Hi!
I'm trying to control a leading edge dimmer that takes a 4 byte serial instruction from my Arduino Mega. (https://www.tindie.com/products/bugrovs2012/serial-interface-ac-dimmer-16a-triac-leading-edge/)
When I output the required bytes using Serial.write to control the power level (e.g. 0xFF 0x00 0x81 0xFF), the board works fine - so I know it's not the board that's the problem. I'm using this triac board to vary the heat output from an electric heater bank in order to maintain a preset air temperature, using a PID control loop driven from an SHT31 temperature/humidity sensor. Everything else in the code is running fine, but the problem I have is that I can't find the correct way to output the required heater power that is calculated by the PID loop (a value between 0 and 255) using the Serial.write command. I've tried converting the PID value to Hex, combining that (as a String) to the "0x" part of the byte, and otherwise generally trawled through the forum here to try different approaches of using byte or array structures instead of a String, but I still can't figure out how to send the control byte (the last of the four bytes - the other three stay fixed) over the serial connection correctly. Hopefully this is something simple that I'm not grasping!
My last attempt at the code is as follows (only the excerpt pertaining to problem, the rest is just standard PID loop and SHT3x code)
//Define PWM range between 0 and 255
if(PID_value < 0)
{ PID_value = 0; }
if(PID_value > 255)
{ PID_value = 255; }
String PID_HEX = String(PID_value, HEX);
PID_HEX.toUpperCase();
String Heater_power = String("0x" + PID_HEX);
byte SendData[5];
Heater_power.getBytes(SendData, sizeof(SendData));
Serial.write(0xFF);
Serial.write(0x00);
Serial.write(0x81);
// Serial.write(0xFF);
Serial.write(SendData, sizeof(SendData));
previous_error = PID_error; //Store the previous error for next loop
delay(3000);
}