-->
Page 2 of 4

Re: analogWrite() does nothing

PostPosted: Sun May 17, 2015 12:51 pm
by Michaelo
If your new to analogueWrite, here's a good intro video... https://www.youtube.com/watch?v=oU-WQVaHm4g

Mike

Re: analogWrite() does nothing

PostPosted: Sun May 17, 2015 2:35 pm
by ficeto
the new release will be up really soon and that and the rest of the files you need will be in there.
If you are realli impatient, you can clone the current repo, rip the hardware/esp8266com folder and replace the one in your current IDE with it. That should give you all needed files to compile with the pwm support. If any compile errors arise, then also copy the hardware/tools/esp8266/sdk folder the same way. You should be fine to use the new PWM and more.

Re: analogWrite() does nothing

PostPosted: Sun May 17, 2015 6:44 pm
by juanpintom
Hi, @Michaelo I know what analogWrite is, thank you for the video :P

@Ficeto, I tried to update the folder, but I get errors on compilation with this example:
https://github.com/esp8266/Arduino/blob/esp8266/hardware/esp8266com/esp8266/libraries/ESP8266WiFi/examples/WiFiWebServer/WiFiWebServer.ino

I get this error on compilation:
Code: Select allF:\Mis Documentos\Arduino\____ESP\arduino-1.6.1-esp8266\hardware\esp8266com\esp8266\cores\esp8266\core_esp8266_wiring_digital.c:69:29: error: expected '=', ',', ';', 'asm' or '__attribute__' before '__digitalWrite'
 extern void ICACHE_RAM_ATTR __digitalWrite(uint8_t pin, uint8_t val) {
                             ^
F:\Mis Documentos\Arduino\____ESP\arduino-1.6.1-esp8266\hardware\esp8266com\esp8266\cores\esp8266\core_esp8266_wiring_digital.c:80:28: error: expected '=', ',', ';', 'asm' or '__attribute__' before '__digitalRead'
 extern int ICACHE_RAM_ATTR __digitalRead(uint8_t pin) {
                            ^
F:\Mis Documentos\Arduino\____ESP\arduino-1.6.1-esp8266\hardware\esp8266com\esp8266\cores\esp8266\core_esp8266_wiring_digital.c:165:12: error: 'digitalRead' aliased to undefined symbol '__digitalRead'
 extern int digitalRead(uint8_t pin) __attribute__ ((weak, alias("__digitalRead")));
            ^
F:\Mis Documentos\Arduino\____ESP\arduino-1.6.1-esp8266\hardware\esp8266com\esp8266\cores\esp8266\core_esp8266_wiring_digital.c:164:13: error: 'digitalWrite' aliased to undefined symbol '__digitalWrite'
 extern void digitalWrite(uint8_t pin, uint8_t val) __attribute__ ((weak, alias("__digitalWrite")));
             ^
Error de compilaciĆ³n


Tomorrow I'll take a look, ty for your help.

Regards!

Re: analogWrite() does nothing

PostPosted: Sun May 17, 2015 8:46 pm
by Solo Pilot
Looking forward to the latest release, but in the meantime, here is a simple PWM implementation using Ticker:


Code: Select all#include <Ticker.h>

void setup() {
  pinMode(12, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  for (int val = 0; val <= 256; val += 32) {
    int value = val;
    if (value > 255)
      value = 255;
    Serial.println(value);
    pwmWrite(12, val);
    delay(2000);
  }
}

///////////////////////////////
Ticker pwmTickersSetHigh[14];
Ticker pwmTickersSetLow[14];
int pwmExponent = 3;    // larger values have smaller duty cycles and smaller resolutions
int pwmFactor = 8;      // 2^pwmExponent
int pwmRoundoff = 4;    // pwmFactor/2;
int pwmDutyCycle = 32;  // 256/pwmFactor;

void pwmWrite(int pin, int value) {
  if (pin < 0  ||  pin >= 14  || value < 0  ||  value > 255)
    return;          // invalid value

  // cancel previous Ticker for this pin, if it was set
  pwmTickersSetHigh[pin].detach();
  pwmTickersSetLow[pin].detach();

  int highTime = (value+pwmRoundoff)/pwmFactor;      // time pin is set HIGH
  if (highTime <= 0)
    digitalWrite(pin, LOW);
  else if (highTime >= pwmDutyCycle)
    digitalWrite(pin, HIGH);
  else {
    digitalWrite(pin, HIGH);
    pwmTickersSetHigh[pin].attach_ms(pwmDutyCycle, pwmSetHigh, pin);
    delay(highTime);
    digitalWrite(pin, LOW);
    pwmTickersSetLow[pin].attach_ms(pwmDutyCycle, pwmSetLow, pin);
  }
}

void pwmSetHigh(int pin) {
   digitalWrite(pin, HIGH);
}

void pwmSetLow(int pin) {
  digitalWrite(pin, LOW);
}