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?
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.
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.
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?