1

Can anybody tell me why these analogWrite() statements are being ignored. The code is entering the "if"statements because the Serial.println() statements are being executed.

    class Pwm
{
  unsigned long previousMillis = 0;
  unsigned long currentMillis;
  int LED;
  int DELAY;
  int up;
  int down;
  int x;

  public:
  Pwm(int led, int Delay) {

    int LED = led;
    pinMode(LED, OUTPUT);

    DELAY = Delay;
    up = 1;
    down = 0;
    x = 0;

  }

  void Update() {
    currentMillis = millis();
    if (currentMillis - previousMillis >= DELAY && up == 1) {
    analogWrite(LED, x);
    x++;
    previousMillis = currentMillis;
    Serial.println(x);
    if (x == 256) {
      up = 0;
      down = 1;
    }
  }
  if (currentMillis - previousMillis >= DELAY && down == 1) {
    analogWrite(LED, x);
    x--;
    previousMillis = currentMillis;
    Serial.println(x);
    if (x == 0) {
      up = 1;
      down = 0;
    }
   }
  }

};

// create Flasher Objects
Flasher red(12, 1000, 250);
//Flasher yellow(11, 500, 500);
//Flasher green(10, 250, 1000);
//Flasher blue(8, 250, 500);

// create PWM Objects
Pwm pin9(9,8);

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

void loop()
{
  red.Update();
  //yellow.Update();
  //green.Update();
  //blue.Update();
  pin9.Update();
}

if I load this code everything works fine. I only have issues with the OOP method.

int LED = 9;
int DELAY = 8;
int up = 1;
int down = 0;
int x;
unsigned long currentMillis;
unsigned long previousMillis = 0;

void setup() {
  pinMode(LED, OUTPUT);
}

void loop() {
  currentMillis = millis();
  if (currentMillis - previousMillis >= DELAY && up == 1) {
    analogWrite(LED, x);
    x++;
    previousMillis = currentMillis;
    if (x == 256) {
      up = 0;
      down = 1;
    }
  }
  if (currentMillis - previousMillis >= DELAY && down == 1) {
    analogWrite(LED, x);
    x--;
    previousMillis = currentMillis;
    if (x == 0) {
      up = 1;
      down = 0;
    }
  }
}
  • Does pin 9 on you board support PWM? – Code Gorilla May 10 '17 at 7:46
  • Yes the second sketch works just fine and uses the same pin as the first – beewrangler May 10 '17 at 14:48
  • Ermm... yes...doh – Code Gorilla May 10 '17 at 15:09
3

The constructor in your Pwm class declares a local variable LED which it sets equal to the parameter led. You use the member variable LED in other methods, so you probably meant to set the member variable of the same name, not create a local.

Remove the int from before LED = led in the constructor.

  • So by creating the local variable LED in the constructor the update() method doesnt see pin 9 being set to output? Please correct me if I'm wrong. I can't make changes to my sketch until later. I will let you know how it turns out. – beewrangler May 10 '17 at 14:55
  • @beewrangler Yes that's correct. C++ lets you declare two variables with the same name at different 'scopes' (some compilers warn you). Put simply scope is between the braces {}, you know of global variables and then function variables, they are two examples of scope. You have define LED as a class member (class scope) and then LED as a function variable (function scope). The lowest level of scope takes precedence, function level, and when you are outside the constructor, but inside the class (i.e. update()) then the class member is used. Good spot Mark. – Code Gorilla May 10 '17 at 15:09
  • As stated above by Mark Smith I had my LED variable scope wrong. His suggestion has fixed my problem. Thank you – beewrangler May 10 '17 at 20:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.