Arduino PWM lag Serial

Hi,

Code:

int inSerial;

void setup()
{
Serial.begin(9600);
TCCR2B = TCCR2B & B11111000 | B00000101;
}

void loop()
{
while (Serial.available()==0){}
inSerial = Serial.parseInt();
analogWrite(3, inSerial);
Serial.println(inSerial);
}

When I send for example 127 ( 50% duty cycle ) through the serial monitor, the change is applied after about 1 second. No matter what hardware clock I use (which PWM pin), frequency or serial speed - the lag still exist.

Thanks in advance.

parseInt timeout is. . . ?
Serial monitor line-ending is . . ?

is it the print or the voltage that lags?

why not

if (Serial.available())  {
  }

i'm not sure if available() indicates a complete line has been received of just the next byte.

gciurpita:
is it the print or the voltage that lags?

why not

if (Serial.available())  {

}




i'm not sure if available() indicates a complete line has been received of just the next byte.

You could check the documentation. It’s pretty clear.

or ask the airplane

airplane? What?

TheMemberFormerlyKnownAsAWOL:
parseInt timeout is. . . ?
Serial monitor line-ending is . . ?

parseInt timeout - not defined
Serial monitor line-ending - No line ending

The idea is to be able to change duty cycle through serial communication (in this case, the serial monitor) and keep it until I change it again whenever I want. I probably need to use another technique to convert information through serial communication to integer.

I will be grateful for the examples.

as “themember…” said, parseint() accepts all characters up until the first non-digit. hence a timeout. you might try appending a non-digit to see what happens.

airplane? What?

execute the action on the airplane to see what it does … try it out

Have a look at the examples in Serial Input Basics - simple reliable non-blocking ways to receive data. There is also a parse example to illustrate how to extract numbers from the received text.

...R

See this: