Counter with limits

Hi everyone
I have a question that I do not know how to solve, it is very simple and I did not think it would not work
I want to create this counter and then with a switch function, make a program change in a block of 16 buttons that I already have programmed in a project
I want that when the counter reaches 6, it stops counting or, failing that, it continues to send 6, and the same when it reaches 0, to stop counting or to continue sending a 0

int octavas = 3;
int octavaUp = 2;
int octavaDown = 7;

int vbUp = 0;
int pvbUp = 0;

int vbDown = 0;
int pvbDown = 0;

unsigned long lastdebouncetime = 0;
unsigned long debouncedelay = 50;

void setup() {
  Serial.begin(9600);
  pinMode (octavaUp , INPUT_PULLUP);
  pinMode (octavaDown , INPUT_PULLUP);

}

void loop()
{
  vbUp = digitalRead(octavaUp);
  vbDown = digitalRead(octavaDown);
  if ((millis() - lastdebouncetime) > debouncedelay) {

    if (vbUp != pvbUp) {
      lastdebouncetime = millis();

      if (vbUp == LOW) {
        octavas++;
        Serial.println(octavas);
        if (octavas == 6)
        {
          octavas = 6;
        }
      }      pvbUp = vbUp;

    }
    if (vbDown != pvbDown) {
      
      lastdebouncetime = millis();

      if (vbDown == LOW) {
        octavas--;

        Serial.println(octavas);
        if (octavas == 0)
        {
          octavas = 0;
        }
      }

      pvbDown = vbDown;
    }
  }
}

One problem I see is this:

        if (octavas == 6)
        {
          octavas = 6;
        }

It should be >=, not ==.

1 Like

If its zero make it zero? makes no sense.

1 Like

And how could I stop adding when it reaches 6 or stop subtracting when it reaches 0
or even if you keep pressing 0 and 6 are the minimum and maximum value

elementary, my dear Watson

        if (octavas > 6)
        {
          octavas = 6;
        }

Hello,
take a view to CONSTRAIN instruction inside the Arduino reference.

Try this and ckeck it out for your project:

// BLOCK COMMENT
// https://www.learncpp.com/cpp-tutorial/
// https://forum.arduino.cc/t/counter-with-limits/903228
#define ProjectName "Counter with limits"
// CONSTANT DEFINITION
// you may need to change these constants to your hardware and needs
constexpr byte Input_[] {A0, A1};
constexpr unsigned long  ScanButtonTime {20};
// VARIABLE DECLARATION AND DEFINTION
enum {OctavaUp , OctavaDown };
unsigned long currentTime;
struct TIMER {
  unsigned long duration;
  unsigned long stamp;
};
TIMER scanButton{ScanButtonTime, 0};
struct BUTTON {
  int name_;
  byte pin;
  bool state_;
} buttons[] {
  {OctavaUp, Input_[OctavaUp], 0},
  {OctavaDown, Input_[OctavaDown], 0},
};
int octavas {3};

// FUNCTIONS
bool checkTimer(TIMER & time_) {
  if (currentTime - time_.stamp >= time_.duration ) {
    time_.stamp = currentTime;
    return true;
  } else return false;
}

void setup() {
  Serial.begin(9600);
  Serial.println(F("."));
  Serial.print(F("File   : ")), Serial.println(__FILE__);
  Serial.print(F("Date   : ")), Serial.println(__DATE__);
  Serial.print(F("Project: ")), Serial.println(ProjectName);
  pinMode (LED_BUILTIN, OUTPUT);
  for (auto Input : Input_) pinMode(Input, INPUT_PULLUP);
  Serial.println(octavas);
}
void loop () {
  currentTime = millis();
  digitalWrite(LED_BUILTIN, (currentTime / 500) % 2);
  if (currentTime - scanButton.stamp >= scanButton.duration) {
    scanButton.stamp = currentTime;
    for (auto &button_ : buttons) {
      bool stateNew = !digitalRead(button_.pin);
      if (button_.state_ != stateNew) {
        button_.state_ = stateNew;
        if (stateNew)  {
          switch (button_.name_) {
            case OctavaUp:
              octavas++;
              break;
            case OctavaDown:
              octavas--;
              break;
          }
          octavas = constrain(octavas, 0, 6);
          Serial.println(octavas);
        }
      }
    }
  }
}

Have a nice day and enjoy coding.

thank you very much to all
I understand that I am overlooking something super simple
when I get home I read everything and I tell you
Thanks again

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.