variable common to all objects in a class

Hello

I am learning to make my own class/ libraries and have written a small library for my multiplexer. I wander if there is a way to set variables in a class that are common to all objects of the class. for example when I set the S0,S1 and S2 pins I want them to be working for all objects of the class. I pasted below my cpp and h file, which works but if I need to change the pins _S0, _S1, S2 is either by making a function (ie. setPins, which is not going to be helpful becuase I would need to do it for all objects) or editing the cpp file which is not ideal. I have tried to use #define in my header file but if I do that the whole thing breaks down.

#include "Mux_library.h"
#include "Arduino.h"

Mux::Mux(uint8_t zInputPin) {
  _zInputPin = zInputPin;
  _S0 = 4;
  _S1 = 3;
  _S2 = 2;
  pinMode(_zInputPin, INPUT_PULLUP);  
  pinMode(_S0, OUTPUT);
  pinMode(_S1, OUTPUT);
  pinMode(_S2, OUTPUT);
}

boolean Mux::PinPressed(uint8_t pin)
{
  _pin = pin;
  int _outcome;
  digitalWrite(_S0, (_pin & (1 << 0)));
  digitalWrite(_S1, (_pin & (1 << 1)));
  digitalWrite(_S2, (_pin & (1 << 2)));
  _muxReading = digitalRead(_zInputPin);
  _muxButton[_pin] = _muxReading;
  unsigned long _currentMillis = millis ();
  if (_muxButton[_pin] != _muxButtonPState[_pin])
  {
    if ((_currentMillis - _lastDebounceTime[_pin]) > DEB_DELAY) {
      _muxButtonPState[_pin] = _muxButton[_pin];
      _lastDebounceTime[_pin] = _currentMillis;
      if (_muxButton[_pin] == LOW) {
        _outcome = HIGH;
      }
      else {
        _outcome = LOW;
      }
    }
  }
  return _outcome;
}
#ifndef Mux_h
#define Mux_h

#include "Arduino.h"

#define DEB_DELAY 25 
#define _PIN_NUM 8

class Mux
{
  private:
    uint8_t _S0;
    uint8_t _S1;
    uint8_t _S2;
    uint8_t _zInputPin;
    uint8_t _pin;

    boolean _muxReading;
    boolean _muxButton[_PIN_NUM] = {HIGH};
    boolean _muxButtonPState[_PIN_NUM] = {HIGH};
    unsigned long _lastDebounceTime[_PIN_NUM] = {0};

  public:
    Mux(uint8_t zInputPin);
    boolean PinPressed(uint8_t pin);

};

#endif

Make your S_n variables static (q.v.). Then they're class wide, not bound to a particular instance.

The keyword you need is "static"

Great! Thanks a lot

it seems I am getting the same result as when using #define to set the pin numbers…

This is baffling me because the code works perfectly when I declare the variables in this way:

_S0 = 4;
_S1 = 3;
_S2 = 2;

but as soon as I make these variable static or use #define, or simply replace them with literals (4,3,2), the code does not work anymore. I am probably getting something wrong in the code, but it still should work considering that when I use the variables for each object they are still the same value (4,3,2!!!) and the code work perfectly.

This is my sketch:

#include "Mux_library.h"

#define PIN_NUM 8

int counter[PIN_NUM] = {0};

Mux MyMux1(7);
Mux MyMux2(6);
Mux MyMux3(5);

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

}

void loop() {

  for (int i = 0; i < PIN_NUM; i++)
  {
    if (MyMux1.PinPressed(i))
    {
      counter[i] += 1;
      Serial.print("button pressed!");
      Serial.print("\t MUX 1 - PIN "); Serial.print(i);
      Serial.print(" - Counter: "); Serial.println(counter[i]);
    }
    if (MyMux2.PinPressed(i))
    {
      counter[i] += 1;
      Serial.print("button pressed!");
      Serial.print("\t MUX 2 - PIN "); Serial.print(i);
      Serial.print(" - Counter: "); Serial.println(counter[i]);
    }
    if (MyMux3.PinPressed(i))
    {
      counter[i] += 1;
      Serial.print("button pressed!");
      Serial.print("\t MUX 3 - PIN "); Serial.print(i);
      Serial.print(" - Counter: "); Serial.println(counter[i]);
    }
  }

this is the outcome when using the static members - detecting button presses even when no button is pressed:

button pressed! MUX 2 - PIN 1 - Counter: -21708
button pressed! MUX 3 - PIN 1 - Counter: -21707
button pressed! MUX 1 - PIN 3 - Counter: -21709
button pressed! MUX 2 - PIN 3 - Counter: -21708
button pressed! MUX 3 - PIN 3 - Counter: -21707
button pressed! MUX 1 - PIN 5 - Counter: -21709
button pressed! MUX 2 - PIN 5 - Counter: -21708
button pressed! MUX 3 - PIN 5 - Counter: -21707
button pressed! MUX 1 - PIN 7 - Counter: -21709
button pressed! MUX 2 - PIN 7 - Counter: -21708
button pressed! MUX 3 - PIN 7 - Counter: -21707