3

This code works and it can fade three LEDs without any problem, obviously you can even add more of them : )

// Pins Definition
const int button1 = 13;
const int button2 = 12;
const int button3 = 11;
const int redled = 2;
const int greenled = 3;
const int blueled = 4;


// Timing States
unsigned long ElapsedLoopTime;
unsigned long Now;

class LED
{
    // Parameters
    unsigned long ParamStartFeed = 300;
    unsigned long ParamDebounce = 50;
    float ParamMinLux = 25.5;
    float ParamMaxLux = 255.0;
    float ParamFeedRamp;

    // Pins
    int led;
    int buttonpin;

    // Status Variables
    bool INDownFront;
    bool LastINValue;
    bool LedStatus;
    float Lux = 127.0;
    bool Direction = true;
    unsigned long LastPressed;
    bool INStatus;
    bool Pressed;
    float Delta;

  public:

    void Initialize(int pin, int ledpin)
    {
      led = ledpin;
      buttonpin = pin;
      pinMode(led, OUTPUT);
      pinMode(buttonpin, INPUT);
      LastINValue = digitalRead(buttonpin);
      LastPressed = millis();
      ElapsedLoopTime = millis();
    }

    void Execute(float ParamFeedRamp)
    {
      INStatus = digitalRead(buttonpin);

      INDownFront = LastINValue && !INStatus;
      Pressed = ((Now - LastPressed) > ParamStartFeed) && INStatus;

      if (INDownFront && (Now - LastPressed  < ParamStartFeed) && (Now - LastPressed  > ParamDebounce))
      {
        LedStatus = !LedStatus;
      }

      if (!INStatus) LastPressed = Now;
      LastINValue = INStatus;

      if (Pressed && LedStatus)
      {
        Delta = (ParamMaxLux - ParamMinLux) * ElapsedLoopTime / ParamFeedRamp;

        if (Direction)
        {
          Lux += Delta;
        }
        else
        {
          Lux -= Delta;
        }

        if  (Lux > 255) {
          Lux = 255;
          Direction = false;
        }
        if  (Lux < 25) {
          Lux = 25;
          Direction = true;
        }

      }
      analogWrite(led, Lux * LedStatus);
    }
};

LED ledblue;
LED ledgreen;
LED ledred;

void setup()
{
  Serial.begin(115200);
  ledred.Initialize(button3, redled);
  ledgreen.Initialize(button2, greenled);
  ledblue.Initialize(button1, blueled);
}

void loop()
{
  Now = millis();
  ledred.Execute(3000); // 3000ms
  ledgreen.Execute(3000); // 3000ms
  ledblue.Execute(3000); // 3000ms
  ElapsedLoopTime = millis() - Now;
}
  • Comments are not for extended discussion; this conversation has been [moved to chat](https://chat.stackexchange.com/rooms/126307/discussion-on-question-by-alessandromrc-fader-oop-problem). – Majenko Jun 10 '21 at 13:05

1 Answers1

1

I don't really know about the

// Pins
byte led;
byte buttonpin;

but maybe you can try

// Pins
int led;
int buttonpin;
Nathan Jiang
  • 55
  • 10
  • 1
    I changed them to integers but there's no change in the speed of the program, but I saw that if I change the position of Now and ElaspedLoopTime in the main Loop it will get faster but it doesn't completely work – alessandromrc Jun 11 '21 at 07:05
  • 1
    I don't agree that it isn't an answer. It looks like an answer. I just happens to not solve the problem. – Nick Gammon Jun 11 '21 at 07:46
  • 1
    @NickGammon I fixed everything right now so I changed the title to [Solved], this is my first time in a forum so I don't really know if i can close the question or not – alessandromrc Jun 11 '21 at 07:52
  • 1
    @alessandromrc For example see https://meta.stackexchange.com/questions/116101/is-it-ok-to-add-solved-to-the-title-of-a-question – Dave Newton Jun 11 '21 at 17:43
  • 1
    @DaveNewton Thanks! – alessandromrc Jun 12 '21 at 16:19
  • I have a tutorial similar to this: https://www.instructables.com/How-to-Create-an-Arduino-Library/ – Austin Jun 12 '21 at 17:51