Merging two variables and sending via serial.write

Hello,
I am building a PC FAN tester based on XY-LPWM PWM generator and arduino.
The arduino and PWM generator are communicating via serial.

SoftSerial.begin(9600);
SoftSerial.write("D075");

Above code works and the duty cycle changes.
Commends for PWM generator are “D” + XXX where XXX is duty cycle.
For example D050 means duty cycle 50%, D100 means 100% duty cycle and so on.

I’m using the potentiometer to set a value from 0-100 and then send this value over serial to PWM generator.

pwm = map(analogRead(A7),0,1023,0,100);

So I need to add letter “D” in front of the pwm variable and send it to PWM generator.
And I do not know how to do it.

I tried the code below but it doesn’t work.

SoftSerial.write("D0");   
SoftSerial.write(pwm);

I have trouble understanding the serial.print and serial.write, but it has to be serial.write as the pwm generator communicates on bytes.

Of course I will need to add “D0” when the pwm is <100 and “D” when it is = 100 but that’s easy.

Here’s more info on the PWM generator:

Is it possible to send just “D” and pwm in separate .write commands so that PWM controller would see it as one command?
Or I should make a new variable? If yes, how to do it?

 SoftSerial.print("D0");   
SoftSerial.print(pwm);

Edit:
“write” sends a single byte/character, or array of bytes or string.
“print” converts values to string (assuming it isn’t string anyway), and then sends as an array of bytes/characters.

A serial connection sends everything as bytes/characters. Therefore your assumption that you cannot use “print” is incorrect.

When you use “write” with a numeric variable type it sends a single byte/character in the range 0..255. The value of the numeric modulo 256.

For the reason explained above by @pcbbc, try changing this line:

SoftSerial.write(pwm);

to:

SoftSerial.print(pwm);

Yes, in communication with PC it would effect in receiving what I need.
But the PWM generator works different and

SoftSerial.print("D075")

does not work with it.
As I mentioned above, I do not fully understand the serial communication, but the generator reads bytes, not ASCII codes.
Maybe my substantiation is wrong, but I tested it and I know for sure that serial.print does not work.

OK, well if you're set on using write then you need to convert the value of pwm to a string using something like atoi():
http://www.cplusplus.com/reference/cstdlib/atoi/
Then concatenate it to the pin name string using something like strcat():
http://www.cplusplus.com/reference/cstring/strcat/

Make sure to size your buffer array so it can hold the largest possible string plus the null terminator.

hawli:
Yes, in communication with PC it would effect in receiving what I need.
But the PWM generator works different and

SoftSerial.print("D075")

does not work with it.
As I mentioned above, I do not fully understand the serial communication, but the generator reads bytes, not ASCII codes.
Maybe my substantiation is wrong, but I tested it and I know for sure that serial.print does not work.

When it comes to serial printing and strings I can't see any difference between print and write. I can see that you may have done something with multiple prints that the generator didn't like but otherwise I'm confused. Can you verify that this really doesn't work?

SoftSerial.print("D075");

Obviously, serial.print works :slight_smile:
Thanks for help.

Try this then:

void setup()
{
int pwm= map(analogRead(A5),0,1023,0,100);
char buf[8];

Serial.begin(115200);
sprintf(buf,"D%03d",pwm);
Serial.print("Fan setting: ");
Serial.println(buf);
}

void loop()
{
}

I solved the problem by simple code:

if (pwm == 100){
  SoftSerial.print("D");   
  SoftSerial.print(pwm);   
}
else {
  SoftSerial.print("D0");   
  SoftSerial.print(pwm);   
}
1 Like