So i created a class and when i created objects and use them, they seem to share their variables between each other ( the currentReading, previousReading and the time variable). how can i create objects that have their own variables so that type of conflict cant happen?

code : ( read the buttons and print them in the lcd screen)

#include <LiquidCrystal.h>

LiquidCrystal lcd(6, 5, 3, 2, 1, 0);

class button {
    void SetupButton(int pino);
    int Read();

    int _pin;

void button::SetupButton(int pin) {
  pinMode(pin, INPUT_PULLUP);
  _pin = pin;


int button::Read() {
  static boolean currentReading, previousReading = 1;
  static unsigned long time;

  currentReading = digitalRead(_pin);// reading
  if (previousReading - currentReading  == 1) { // when reading gos from 1 to 0
    time = millis();

  if (currentReading - previousReading == 1 && (50 < (millis() - time) < 500)) {     // when reading gos back to 1 before 500ms
    previousReading = currentReading;
    return 1;

  if (3000 > (millis() - time) && (millis() - time) > 500 && !currentReading) { // when reading keeps in 0 after 500ms
    previousReading = currentReading;
    return 2;

  if ((millis() - time) > 3000 && !currentReading) { // // when reading keeps in 0 after 3000ms
    previousReading = currentReading;
    return  3;

  previousReading = currentReading; // current reading becomes the previous

  return 0;

button up;
button down;

void setup() {
  lcd.begin(16, 2);

void loop() {

  lcd.setCursor(0, 0);


ps : i already know how to do this without using classes but doing it with classes makes the code much cleaner so i am trying to achieve this

closed as off-topic by per1234, gre_gor, Juraj, sempaiscuba, Greenonline Oct 29 '18 at 14:56

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question does not appear to be about Arduino, within the scope defined in the help center." – per1234, gre_gor, Juraj, sempaiscuba, Greenonline
If this question can be reworded to fit the rules in the help center, please edit the question.

  • you can test the code in tinkercad.com/things/8oXS6FQNK6A-dazzling-jaban – Lucas Alexandre Oct 27 '18 at 0:44
  • Your class has only 1 attribute _pin - which is private. If you want to have variables for each instance these should be in your class. Your code doesn't seem to create any class instances. – Milliways Oct 27 '18 at 1:30
  • So if i create one more attribute for each variable ( like _currentReading,_previousReading, _time ) and use them in the fuctions will this solve my problem? – Lucas Alexandre Oct 27 '18 at 1:36
  • This would be the first step, but you need to create instances of your class e.g. up = button(4); It is normal to pass essential attributes in the constructor. – Milliways Oct 27 '18 at 1:41
  • thank you very much! i just created the new attributes and changed all the variables by it attribute counterpart and it worked flawlessy, post it as an answer so i can upvote it. – Lucas Alexandre Oct 27 '18 at 1:50

I was tempted to re-write your code (I am quite rusty on c++), but you may be better to start with an existing class.

There is a Button class on the Arduino site. See https://playground.arduino.cc/Code/Button

You could use this or study to see a more usual c++ implementation and adapt your code.

Virtually ALL c++ classes include a constructor. I would use instead of SetupButton

void button::button(int pin) {
  pinMode(pin, INPUT_PULLUP);

and add uint8_t previousReading; and unsigned long time; to your class as private variables.

Not the answer you're looking for? Browse other questions tagged or ask your own question.