Counter limit setting

The sketch below displays a counter value of 100 and if the user presses up/down it increments/decrements by 1 each loop.

I would like to set upper & lower limits. How is it possible?

/*
count number up and down with joystick and update uLCD display.
*/

//include displayshield library
#include <displayshield4d.h>
DisplayShield4d oled;

//Define pin assignments
const int DOWNPin = 2;
const int PRESSPin = 5;
const int UPPin = 6;

//define number variable and set to 100
int number = 100;
int up = 1;
int down = 1;
char number2[5];

void setup()
{
  //Start serial comms
  Serial.begin(57600);
  
  //initialise uLCD
  oled.Init();
  
  //Initialize the switch pins as inputs:
  pinMode(DOWNPin, INPUT);
  digitalWrite(DOWNPin, HIGH);
  pinMode(PRESSPin, INPUT);
  digitalWrite(PRESSPin, HIGH);
  pinMode(UPPin, INPUT);
  digitalWrite(UPPin, HIGH);
}

void decreaseFunction()
{
  number -= down;
  itoa(number, number2, 10);
  oled.rectangle(0, 0, 128, 128, OLED_SOLID, oled.RGB(0, 0, 0));
  oled.setfontmode(OLED_FONT_OPAQUE);
  oled.drawstringblock(5, 5, 0, oled.RGB(255, 255, 0), 4, 4, number2);
}

void increaseFunction()
{
  number += up;
  itoa(number, number2, 10);
  oled.rectangle(0, 0, 128, 128, OLED_SOLID, oled.RGB(0, 0, 0));
  oled.setfontmode(OLED_FONT_OPAQUE);
  oled.drawstringblock(5, 5, 0, oled.RGB(255, 255, 0), 4, 4, number2);
}

void resetFunction ()
{
  number = 100;
  itoa(number, number2, 10);
  oled.rectangle(0, 0, 128, 128, OLED_SOLID, oled.RGB(0, 0, 0));
  oled.drawstringblock(5, 5, 0, oled.RGB(255, 255, 0), 4, 4, number2);
}

void loop()
{
  //Variables for reading switch status
  int DOWNstate;
  int PRESSstate;
  int UPstate;
  
  //Read state of DOWN switch
  if (digitalRead(DOWNPin) == LOW)
  {
    //Check if already pressed
    if (DOWNstate == 0)
    {
      //Write to LCD
        decreaseFunction();
        DOWNstate = 1;
      }
}
else {
DOWNstate = 0;
}

  //Read state of UP switch
  if (digitalRead(UPPin) == LOW)
{
    //Check if already pressed
    if (UPstate == 0)
  {
        //Write to LCD
        increaseFunction();
        //Set pressed
        UPstate = 1;
}
} else {
          UPstate = 0;
         }

  //Read state of PRESS switch
  if (digitalRead(PRESSPin) == LOW)
{
    //Check if already pressed
    if (PRESSstate == 0)
  {
        //Write to LCD
        resetFunction();
        //Set pressed
        PRESSstate = 1;
}
} else {
          PRESSstate = 0;
         }
}

After the increment just do an if statement

if(number > upperLimit) number = upperLimit;

Having set upperLimit to the value you want.

Can you guess how to do the lower limit?

Grumpy_Mike: After the increment just do an if statement

if(number > upperLimit) number = upperLimit;

Having set upperLimit to the value you want.

Can you guess how to do the lower limit?

I can see how that works, thanks. I saw the constrain operator mentioned in some text but I cannot get it to work reliably.

Sorted. Thanks Mike!

/*
count number up and down with joystick and update uLCD display.
*/

//include displayshield library
#include <displayshield4d.h>
DisplayShield4d oled;

//Define pin assignments
const int DOWNPin = 2;
const int PRESSPin = 5;
const int UPPin = 6;

//define number variable and set to 100
int number = 100;
int up = 1;
int down = 1;
int upperLimit = 200;
int lowerLimit = 0;
char number2[5];

void setup()
{
  //Start serial comms
  Serial.begin(57600);
  
  //initialise uLCD
  oled.Init();
  
  //Initialize the switch pins as inputs:
  pinMode(DOWNPin, INPUT);
  digitalWrite(DOWNPin, HIGH);
  pinMode(PRESSPin, INPUT);
  digitalWrite(PRESSPin, HIGH);
  pinMode(UPPin, INPUT);
  digitalWrite(UPPin, HIGH);
}

void decreaseFunction()
{
  number -= down;
  if(number < lowerLimit) number = lowerLimit;
  {itoa(number, number2, 10);
  oled.rectangle(0, 0, 128, 128, OLED_SOLID, oled.RGB(0, 0, 0));
  oled.setfontmode(OLED_FONT_OPAQUE);
  oled.drawstringblock(5, 5, 0, oled.RGB(255, 255, 0), 4, 4, number2);}
}

void increaseFunction()
{
  number += up;
  if(number > upperLimit) number = upperLimit;
  {itoa(number, number2, 10);
  oled.rectangle(0, 0, 128, 128, OLED_SOLID, oled.RGB(0, 0, 0));
  oled.setfontmode(OLED_FONT_OPAQUE);
  oled.drawstringblock(5, 5, 0, oled.RGB(255, 255, 0), 4, 4, number2);
  }
}

void resetFunction ()
{
  oled.rectangle(0, 0, 128, 128, OLED_SOLID, oled.RGB(0, 0, 0));
  number = 100;
}

void loop()
{
  //Variables for reading switch status
  int DOWNstate;
  int PRESSstate;
  int UPstate;
  
  //Read state of DOWN switch
  if (digitalRead(DOWNPin) == LOW)
  {
    //Check if already pressed
    if (DOWNstate == 0)
    {
      //Write to LCD
        decreaseFunction();
        DOWNstate = 1;
      }
}
else {
DOWNstate = 0;
}

  //Read state of UP switch
  if (digitalRead(UPPin) == LOW)
{
    //Check if already pressed
    if (UPstate == 0)
  {
        //Write to LCD
        increaseFunction();
        //Set pressed
        UPstate = 1;
}
} else {
          UPstate = 0;
         }

  //Read state of PRESS switch
  if (digitalRead(PRESSPin) == LOW)
{
    //Check if already pressed
    if (PRESSstate == 0)
  {
        //Write to LCD
        resetFunction();
        //Set pressed
        PRESSstate = 1;
}
} else {
          PRESSstate = 0;
         }
}