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;
}
}
}