Switching betwen if's

So i need to be able to switch betwen parts of code with only one button how do i can do this to work with more than 2 parts of code. Actualy i made sucesfully switching betwen 2 codes.

code dont look too much into it, its garbage

#include <OneWire.h>
#include <Wire.h>
#include <DallasTemperature.h>
#include <LiquidCrystal_I2C.h> 
#include "DHT.h"

DHT dht;
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);

#define DHT11_PIN 2 
#define ONE_WIRE_BUS_1 0
#define ONE_WIRE_BUS_2 1 
int max = 2;
int min = 2;
int maxt = 23;
int mint = 21;
int inp1,inp2,inp3,inp4,inp5,humidity;

OneWire oneWire_left(ONE_WIRE_BUS_1);
OneWire oneWire_right(ONE_WIRE_BUS_2);

DallasTemperature sensor_left(&oneWire_left);
DallasTemperature sensor_right(&oneWire_right);

int zmint,zmaxt,zmin,zmax,zmien;

bool x;
int tempz,tempa;
 unsigned long aktualny = 0;
 unsigned long poprzedni= 0;
 unsigned long roznica = 0;

byte menus=2;

void setup() {
  // put your setup code here, to run once:
  dht.setup(DHT11_PIN);
  Serial.begin(9600);
  lcd.backlight();
  lcd.setCursor(0,0);
  lcd.clear();
  lcd.begin(16,2);
  sensor_right.begin();
  sensor_left.begin(); 
  pinMode(2,INPUT_PULLUP);
  pinMode(3,INPUT_PULLUP);
  pinMode(4,INPUT_PULLUP);
  pinMode(5,INPUT_PULLUP);
  pinMode(6,INPUT_PULLUP);
  pinMode(7,OUTPUT);
zmien =3;
}

void loop() {
  // put your main code here, to run repeatedly:
aktualny = millis();

  roznica = aktualny - poprzedni;
  if (roznica >= 5000UL) {
    unsigned long x;
    //Zapamietaj aktualny czas
    x = millis();
    Serial.println(x);
    sensor_right.requestTemperatures();
    sensor_left.requestTemperatures();
    humidity = dht.getHumidity();
    x = millis();
    Serial.println(x);
    poprzedni = aktualny;
    //Wyslij do PC
    Serial.println(aktualny);
    
  }
  float temp_right;
  float temp_left;

  temp_right = sensor_right.getTempCByIndex(0);
  temp_left = sensor_left.getTempCByIndex(0);
  
  int averge = (temp_right + temp_left)/2;
  int left = temp_left;
  int right = temp_right;
if(averge +max >= maxt)
{
  Serial.println("OTWORZYC");
  inp2=1;
  inp1=0;
}
if(averge +min <= mint)
{
 
  Serial.println("ZALACZYC");
  inp1=1;
  inp2=0;
}
  delay(200);
   
  if(digitalRead(5)==LOW) //this works with no porblems
  {
  if(menus==1)
  {
  menus=2;
  lcd.clear();  
  }
  else
  {
  menus=1;
  lcd.clear();  
  }  
  }

if(menus== 1)
{
lcd.print("T1 ");
    lcd.print(temp_left,1);
    lcd.print("");
    lcd.print(" T2 ");
    lcd.print(temp_right,1);
    lcd.print("");
    lcd.setCursor(0,1);
    
    lcd.print(inp1);
    lcd.print(" ");
    lcd.print(inp2);
    lcd.print(" ");
    lcd.print(inp3);
    lcd.print(" ");
    lcd.print(inp4);
    lcd.print(" ");
    lcd.print(inp4);
    lcd.print(" ");
    lcd.print("RH-");
    lcd.print(humidity);
    lcd.print("%");
    lcd.setCursor(0,0);

}
if(menus==2) 
{
  
  if(digitalRead(6)==1) // here i have problem
  {
    switch(zmien)
    {
    case 3:{
    if(digitalRead(3)==1)
 {
  zmin +=1;
 }
 if(digitalRead(4) == 1)
 {
  zmin -=1;
 }

 if(digitalRead(2) ==0)
{
min =zmin;
}
zmien=4;
    }
    break;
    case 4:
          if(zmien ==4)
    {
    if(digitalRead(3)==1)
 {
  zmax +=1;
 }
 if(digitalRead(4) == 1)
 {
  zmax -=1;
 }

 if(digitalRead(2) ==0)
{
max =zmax;
}
zmien=1;
    }
    break;
    }        
  }
    
    lcd.setCursor(5,1);
    lcd.print("   ");
    lcd.setCursor(11,0);
    lcd.print("   ");
    lcd.setCursor(0,0);
    lcd.print(mint);
    lcd.print("||");
    lcd.print(maxt);
    lcd.print("||");
    lcd.print(min);
    lcd.print("||");
    lcd.print(max);
    lcd.setCursor(0,1);
    lcd.print(zmint);
    lcd.print("||");
    lcd.print(zmaxt);
    lcd.print("||");
    lcd.print(zmin);
    lcd.print("||");
    lcd.print(zmax);
    lcd.setCursor(0,0);

}
}

XenuXE:
So i need to be able to switch betwen parts of code with only one button how do i can do this to work with more than 2 parts of code. Actualy i made sucesfully switching betwen 2 codes.

A common way to do that is to have a variable that is incremented with each button press and the appropriate function is called depending on the value of the variable. Something like this pseudo code

if (buttonPressed == true) {
   codeSelector ++;
   if (codeSelector > maxCodeOptions) {
      codeSelector = 0;
   }
}
if (codeSelector == 0) {
 functionA();
}
else if (codeSelector == 1) {
  functionB();
}

Separately ... If you use the AutoFormat tool it will make your code a great deal easier to read.

...R
Planning and Implementing a Program

oh thanks i will use it

well now it switch but every time i click button for increasing or decreasing value it changes first on one and leater on second how can i fix it ?

#include <OneWire.h>
#include <Wire.h>
#include <DallasTemperature.h>
#include <LiquidCrystal_I2C.h>
#include "DHT.h"

DHT dht;
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);

#define DHT11_PIN 2
#define ONE_WIRE_BUS_1 0
#define ONE_WIRE_BUS_2 1
int max = 2;
int min = 2;
int maxt = 23;
int mint = 21;
int inp1, inp2, inp3, inp4, inp5, humidity;

OneWire oneWire_left(ONE_WIRE_BUS_1);
OneWire oneWire_right(ONE_WIRE_BUS_2);

DallasTemperature sensor_left(&oneWire_left);
DallasTemperature sensor_right(&oneWire_right);

int zmint, zmaxt, zmin, zmax, zmien;

bool x;
int tempz, tempa;
unsigned long aktualny = 0;
unsigned long poprzedni = 0;
unsigned long roznica = 0;

byte menus = 2;

void setup() {
  // put your setup code here, to run once:
  dht.setup(DHT11_PIN);
  Serial.begin(9600);
  lcd.backlight();
  lcd.setCursor(0, 0);
  lcd.clear();
  lcd.begin(16, 2);
  sensor_right.begin();
  sensor_left.begin();
  pinMode(2, INPUT_PULLUP);
  pinMode(3, INPUT_PULLUP);
  pinMode(4, INPUT_PULLUP);
  pinMode(5, INPUT_PULLUP);
  pinMode(6, INPUT_PULLUP);
  pinMode(7, OUTPUT);
  zmien = 0;
}

void loop() {
  // put your main code here, to run repeatedly:
  aktualny = millis();

  roznica = aktualny - poprzedni;
  if (roznica >= 5000UL) {
    unsigned long x;
    //Zapamietaj aktualny czas
    x = millis();
    Serial.println(x);
    sensor_right.requestTemperatures();
    sensor_left.requestTemperatures();
    humidity = dht.getHumidity();
    x = millis();
    Serial.println(x);
    poprzedni = aktualny;
    //Wyslij do PC
    Serial.println(aktualny);

  }
  float temp_right;
  float temp_left;

  temp_right = sensor_right.getTempCByIndex(0);
  temp_left = sensor_left.getTempCByIndex(0);

  int averge = (temp_right + temp_left) / 2;
  int left = temp_left;
  int right = temp_right;
  if (averge + max >= maxt)
  {
    Serial.println("OTWORZYC");
    inp2 = 1;
    inp1 = 0;
  }
  if (averge + min <= mint)
  {

    Serial.println("ZALACZYC");
    inp1 = 1;
    inp2 = 0;
  }
  delay(200);

  if (digitalRead(5) == LOW)
  {
    if (menus == 1)
    {
      menus = 2;
      lcd.clear();
    }
    else
    {
      menus = 1;
      lcd.clear();
    }
  }

  if (menus == 1)
  {
    lcd.print("T1 ");
    lcd.print(temp_left, 1);
    lcd.print("");
    lcd.print(" T2 ");
    lcd.print(temp_right, 1);
    lcd.print("");
    lcd.setCursor(0, 1);

    lcd.print(inp1);
    lcd.print(" ");
    lcd.print(inp2);
    lcd.print(" ");
    lcd.print(inp3);
    lcd.print(" ");
    lcd.print(inp4);
    lcd.print(" ");
    lcd.print(inp4);
    lcd.print(" ");
    lcd.print("RH-");
    lcd.print(humidity);
    lcd.print("%");
    lcd.setCursor(0, 0);

  }
  if (menus == 2)
  {

    if (digitalRead(6) == 1) {
      zmien ++;
      if (zmien > 5) {
        zmien = 0;
      }
    }

    if (zmien == 0)
    {
      minC();
    }

    else if (zmien == 1)
    {
      maxC();
    }


    lcd.setCursor(5, 1);
    lcd.print("   ");
    lcd.setCursor(11, 0);
    lcd.print("   ");
    lcd.setCursor(0, 0);
    lcd.print(mint);
    lcd.print("||");
    lcd.print(maxt);
    lcd.print("||");
    lcd.print(min);
    lcd.print("||");
    lcd.print(max);
    lcd.setCursor(0, 1);
    lcd.print(zmint);
    lcd.print("||");
    lcd.print(zmaxt);
    lcd.print("||");
    lcd.print(zmin);
    lcd.print("||");
    lcd.print(zmax);
    lcd.setCursor(0, 0);

  }
}
void maxC()
{
  if (digitalRead(3) == 1)
  {
    zmax += 1;
  }
  if (digitalRead(4) == 1)
  {
    zmax -= 1;
  }

  if (digitalRead(2) == 0)
  {
    max = zmax;
  }

}
void minC()
{
  if (digitalRead(3) == 1)
  {
    zmin += 1;
  }
  if (digitalRead(4) == 1)
  {
    zmin -= 1;
  }

  if (digitalRead(2) == 0)
  {
    min = zmin;
  }
}

With this

   if (digitalRead(6) == 1) {

you are probably getting false readings. Your code needs to check for when the button changes from 0 to 1 - to make sure the user actually took his/her finger off the button.

Using "magic numbers" in a program - the 6 in this example - makes it very hard to read or maintain. Much better to define variables to hold the pin numbers - for example

byte menuButtonPin = 6;

and then

previousMenuButtonState = menuButtonState;
menuButtonState = digitalRead(menuButtonPin);
if (menuButtonState == 1 and menuButtonState != previousMenuButtonState) {
   zmien ++;
   if (zmien > 5) {
        zmien = 0;
   }
}

By the way, menuButton is just my guess at an appropriate name

If you use variables with meaningful names for things, not only does it make the code self-explanatory but if you decide to use a different I/O pin you only need to change a value at the top of the program rather than searching right through to find everywhere that pin 6 is used.

It would also make the code much neater if you do all the digitalRead()s one after the other at the start of loop and save the values for use elsewhere in the program. Better still, create a function called readButtons() and call that from loop().

...R