me-no-dev / RasPiArduino

Arduino Framework for RaspberryPI
326 stars 76 forks source link

analogWriteSetup doesn't work inside setup #88

Open faustinoaq opened 5 years ago

faustinoaq commented 5 years ago

Hi @me-no-dev thank you for this project!

I have an issue with analogWriteSetup:

#define PIN 18

void setup() {
  analogWriteSetup(250, 1024); // 250Hz and 1024 steps
}

void loop() {
  analogWrite(PIN, 260); // testing 260 steps
}

PIN 18 should output around 450mV but it outputs only 20mV

faustinoaq commented 5 years ago

After checking your source code, I realize analogWriteInit() overwrites my setup, see:

https://github.com/me-no-dev/RasPiArduino/blob/ae0f60cf7de43e9222e18f2df1683cd1409dcc15/cores/piduino/wiring_pwm.c#L54-L55

so I did this:

#define PIN 18

void setup() {
  analogWrite(PIN, 1); // executes analogWriteInit early 
  analogWriteSetup(250, 1024); // 250Hz and 1024 steps
}

void loop() {
  analogWrite(PIN, 260); // testing 260 steps
}

This way I can get the right analog setup and the expected output.