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– Code Gorilla2017年05月10日 07:46:18 +00:00Commented May 10, 2017 at 7:46
-
Yes the second sketch works just fine and uses the same pin as the firstbeewrangler– beewrangler2017年05月10日 14:48:26 +00:00Commented May 10, 2017 at 14:48
-
Ermm... yes...dohCode Gorilla– Code Gorilla2017年05月10日 15:09:57 +00:00Commented May 10, 2017 at 15:09
1 Answer 1
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– beewrangler2017年05月10日 14:55:00 +00:00Commented May 10, 2017 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– Code Gorilla2017年05月10日 15:09:20 +00:00Commented May 10, 2017 at 15:09 -
As stated above by Mark Smith I had my LED variable scope wrong. His suggestion has fixed my problem. Thank youbeewrangler– beewrangler2017年05月10日 20:51:45 +00:00Commented May 10, 2017 at 20:51