Updating Class Variables with Switch Case

Hello to all :slight_smile:

Basically I have included the fade and blink with out delay sketches and placed them into classes. Then included a pin change interrupt for a button attached to A5 that when pressed increments thru switch cases.

I would like to update the values of brightness, fadeAmount and interval depending on the values that are set within the currently selected case.

So...

Question 1: Is it possible to update these values from within these switch cases?

After a bit of research I thought that I may be able to use pointers.
I have used pointers in other code with functions successfully but have not been able to include them while using a class.
I am unsure how and where to reference the variables that I would like to update and how/where to pass them into the class or if it's even possible to update them at all.

Question 2: If pointers are the way to do this. Can anyone please provide a working example in this context?

Question 3: If there is a better way to do both these tasks (fade/flash) at the same time and be able to change these variables depending on how many pushes of a button how can I do this and keep my code fairly clean and easy to maintain?

Thank you for your time

Suggestions are greatly appreciated :slight_smile:

Board - Uno
IDE - Arduino 1.6.7

Button pin - A0
Led pins - 12,11,9,8

#define modebutton A5
int mode = 0;

class fade {
  public:
    int led;
    int brightness;
    int fadeAmount;
    fade(int p1, int b, int f)
    {
      led = p1;
      pinMode(led, OUTPUT);
      brightness = b;
      fadeAmount = f;
    }
    void Update()
    {
      analogWrite(led, brightness);
      brightness = brightness + fadeAmount;
      if (brightness == 0 || brightness == 255) {
        fadeAmount = -fadeAmount ;
      }
    }
};
fade nine(9, 0, 1); //How do I pass new brightness & fade values/addresses to this?
fade eleven(11, 125, 1);//Is this the right place to send them?

class flash {
  public:
    int ledPin;
    int ledState;
    int interval;
    unsigned long previousMillis;
    flash(int p2, int i)
    {
      ledPin = p2;
      pinMode(ledPin, OUTPUT);
      ledState = LOW;
      interval = i;
      previousMillis = 0;
    }
    void Update()
    {
      unsigned long currentMillis = millis();
      if (currentMillis - previousMillis >= interval) {
        previousMillis = currentMillis;
        if (ledState == LOW) {
          ledState = HIGH;
        } else {
          ledState = LOW;
        }
        digitalWrite(ledPin, ledState);
      }
    }
};
flash eight(8, 500); // How do I update this interval -
flash twelve(12, 250); // Without affecting this one?

void setup() {
  Serial.begin(9600);
  pinMode(modebutton, INPUT);
  digitalWrite(modebutton, HIGH);
  Interrupt();
}

void loop() {
  Serial.println(mode);
  switch (mode) {

    case 0:
//----------Update Brightness, Fade Amount and Interval Here----------//

//interval of 8 = 250;
//interval of 9 = 1000;
//brightness of 11 = 100;
//fadeAmount of 11 = 2; 
//brightness of 12 = 255;
//fadeAmount of 12 = -1;

    break;

    case 1: break;
    case 2: break;
    case 3: break;
    case 4:
//----------Update Brightness, Fade Amount and Interval Again Here----------//
    break;
    case 5: break;
    case 6: break;
    case 7: break;
    case 8: break;
    case 9:
//-------Update Brightness, Fade Amount and Interval, From Any Case-------//
    break;
  }
  if (mode > 9) {
    mode = 0;
  }
  eight.Update();// I have tried sending addresses/values of variable from here
  nine.Update(); // but have had no luck :(
  eleven.Update();
  twelve.Update();
}
void Interrupt() {
  cli();
  PCICR = 0x02;
  PCMSK1 = B00100000;
  sei();
}

ISR(PCINT1_vect) {
  if (digitalRead(modebutton) == 0) {
    mode++;
  } delay(50);
}

bwod_fade.ino (2.35 KB)

Disclaimer: I'm not a C++ programmer.

As the variables are public, you can directly access them to my knowledge. E.g. set the brightness of the 'nine' variable to value 11:

  nine.brightness = 11;

sterretje:
Disclaimer: I'm not a C++ programmer.

As the variables are public, you can directly access them to my knowledge. E.g. set the brightness of the 'nine' variable to value 11:

  nine.brightness = 11;

and if they are private, you can create a public setter to access the private variables from outside the class:

nine.setBriteness(125);

Doing it via a setter and a getter is the usual way of doing it because you can do some validation of the data. Or flag it as new etc. You're more in control so to speak. But if they are public data types you can just access them from outside the class like sterretje said.

Thank you all for your help :slight_smile:

This now works pretty much how I'd hoped it would.
I had written my own very long and overly complicated code that was really just trying to achieve what these two examples did so effortlessly.
So I put aside what I had done went back to these examples and tried again in a simpler environment and still did not accomplish what I needed too.
I was sure that declaring the variables as public should have allowed me access to them from the main loop but could not work out how to get that new information where I needed it to go.

eight.interval = 200;
nine.fadeAmount = 1;

Worked brilliantly and after filling the cases I only saw an increase of 1% to program memory. Everything else I tried ate up dynamic memory.

Before posting I had not seen getters and setters before and I will do some more reading/testing and hopefully start using getters/setters properly so I can apply some validation and better logic to the tasks at hand.
At the moment I am running 4 strips of 24, 3mm LEDS @ 2v 30mA a strip (old outdoor solar powered lights) in a few different colours and they look great.
One day I hope to include an IR sketch that I have been working with, a charger circuit and a solar panel with either a real time clock or a day night sensor.
Maybe even one with X-mas jingles for the Christmas tree this year.
Hopefully I can make a small shield and place a few of these around the house and play with modes and settings from the remote control.

Thankyou for your help and for pointing me in the right direction!

Thoughts and suggestion greatly appreciated :slight_smile:

bwod_fade2.ino.ino (4.68 KB)

Before posting I had not seen getters and setters before

digitalRead(), analogRead(), analogWrite() and digitalWrite() are getters and setters. There in nothing magic about getters and setters.

looks great

some little code shortening tips:

        if (ledState == LOW) {
          ledState = HIGH;
        } else {
          ledState = LOW;
        }

how about:

ledState = !ledState;

and:

  pinMode(modebutton, INPUT);
  digitalWrite(modebutton, HIGH);

how about:

pinMode(modebutton, INPUT_PULLUP);