analogWrite help

Hi,
I have a basic Serial interface working, where a user can input values and they are saved to eeprom.
This is working as expected.
The value they enter will then be used to generate a PWM value on an analog pin with AnalogWrite.

If I want a value less than 100, the way I have coded the serial interface requires a 0 to be added before the value. IE 050.

The question which I cannot find an answer to googleing is:

can I use analogWrite(A1,050) ? for example, will it have the same effect as analogWrite(A1,50)?
or do I need to clip the leading 0 off before using it in analogWrite?

thanks in advance

You can use it but it won't do what you expect because a leading zero is an indication that the value is in Octal. Try Serial.println(050); to see what I mean

However, without seeing your full sketch it is impossible to say whether the user entering 050 will cause a problem or not because we don't know what data types you are using and how they are stored to EEPROM

1 Like

Try it see if it works.

can you post that code? seems unnecessary

I will do, but i thought it may throw a spanner in the works and thought id ask before I get that far in the code and therefore be able to tackle it before spending hours scratching my head over it not working right.

here's one approach


void
loop (void)
{
    if (Serial.available ()) {
        char buf [90];
        int n = Serial.readBytesUntil ('\n', buf, sizeof(buf)-1);
        buf [n] = '\0';

        char cmd [10];
        int  val;
        sscanf (buf, "%s%d", cmd, & val);

        Serial.print (" cmd ");
        Serial.print (cmd);
        Serial.print (", val ");
        Serial.println (val);
    }
}

void
setup (void)
{
    Serial.begin (9600);
}

Probably, but until we see the code we don't know what @spruce_m00se is actually doing with it

File under I learned something today…

atoi() will work on "0x12bc", "12345" and "0177" and do what you'd expect. Or mess you right up. :expressionless:

Maybe things would have been easier if we only had eight fingers.

a7

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.