help debuggiing simple program

Hi all I'm making a simple program to control a camera slider it's in very early development stage so don't judge :stuck_out_tongue: I'm keeping it as simple as possible as I'm completely new to Arduino just trying to get to grips with the programming.

basically I have a int that controls the multiplication factor for when a button is pushed to increment the value of another int.

It works fine but my if statement seems to be failing it should look back to 1 when it reaches 5 but instead it loops back to 2. I'm sure it must be a problem with the flow of my code but I haven't been able to identify where. the function in question is at the bottom and called multiply.

I've checked in the serial monitor and the int does loop back to 1 but for some reason it doesn't actually set the multiplier back to 1. Hope that makes sense.

Any help would be greatly appreciated, I'll dump my code below.

#include <LiquidCrystal.h>
const int rs = 8, en = 9, d4 = 4, d5 = 5, d6 = 6, d7 = 7;// custom pin out for my LCD!!!
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

int Total_time = 0;
int Exp_time = 0;
int Step_time = 0;
int NoExp = 0;
int NoSteps = 0;
int Step_range = 0;

int hours = 0;
int mins = 0;

int MenuNo = 1;
int multipliers = 1;
int mult = 1;

void setup() {

  Serial.begin(9600);
  lcd.begin(16, 2);
  lcd.setCursor(1, 0);
 // lcd.print("Welcome to my");
  lcd.setCursor(1, 1);
  //lcd.print("camera slider");
  //delay(3000);
  lcd.clear();
  lcd.setCursor(1, 0);
 // lcd.print("Enter timelapse");
  lcd.setCursor(4, 1);
  //lcd.print("type");
  delay(2000);
  lcd.clear();
  MenuItems();
}

void loop() {
  Buttons();
}


void Buttons(){
int x;
x = analogRead (0);
  
    if (x < 80){
          
          delay(200);
         // lcd.print("right key");
         serialtest();

         if (MenuNo == 1){
            Total_time = Total_time + multipliers;
            hours = Total_time /60;
            mins = Total_time % 60;
            MenuItems();
          }else if (MenuNo == 2){
            Exp_time = Exp_time + multipliers;
            MenuItems();
          }else if (MenuNo == 3){
            NoExp = NoExp + multipliers;
            MenuItems();
          }else if (MenuNo == 4){
            Step_time = Step_time + multipliers;
            MenuItems();
          }
             
      
   }else if (x < 200){
       
          delay(200);
          //lcd.print("up key");
    if (MenuNo >0 && MenuNo < 7){
          MenuNo ++;
          MenuItems();
    }       
      
   } else if (x < 400){
          
          delay(200);
          //lcd.print("down key");
     if (MenuNo >0 && MenuNo < 7){
          MenuNo --;
          MenuItems();
          
      }    
     
   }else if (x < 600){
          
          delay(200);
          //lcd.print("left key");
          mult++;
          multiply();
          MenuItems();
          if (MenuNo == 1){

          }
          if (MenuNo == 2){
          
          }
          if (MenuNo == 3){
          
          }
          if (MenuNo == 4){
          
          }
               
      
   }else if (x < 800){
         
          delay(400);
          //lcd.print("select key");

         if (MenuNo == 1){
           
     
          }
   
       }  
}



void MenuItems(){

if (MenuNo == 1){
      
      lcd.clear();
      lcd.print("Run time");
      lcd.setCursor(13,0);  
      lcd.print(multipliers);
      lcd.setCursor(11,0);
      lcd.print("X");
      lcd.setCursor(0,1);
      lcd.print("Time : "); 
      lcd.setCursor(7,1); 
      lcd.print(Total_time); 
      lcd.setCursor(7,1); 
      lcd.print(hours);
      lcd.print(":");       
      lcd.print(mins); 
      
  }else if (MenuNo == 2){
      lcd.clear();
      lcd.print("Exposure time");
      lcd.setCursor(14,1);  
      lcd.print(multipliers);
      lcd.setCursor(13,1);
      lcd.print("X");  
      lcd.setCursor(0,1);
      lcd.print("Time : "); 
      lcd.setCursor(7,1); 
      lcd.print(Exp_time); 
      
  }else if (MenuNo == 3){
      lcd.clear();
      lcd.print("# exp");
      lcd.setCursor(14,1);  
      lcd.print(multipliers);
      lcd.setCursor(13,1);
      lcd.print("X");  ;  
      lcd.setCursor(0,1);
      lcd.print("# : "); 
      lcd.setCursor(7,1); 
      lcd.print(NoExp);
      
  }else if (MenuNo == 4){
      lcd.clear();
      lcd.clear();
      lcd.clear();
      lcd.print("Run time");  
      lcd.setCursor(0,1);
      lcd.print("Time : "); 
      lcd.setCursor(7,1); 
      lcd.print(Total_time);
         
       
  }else if (MenuNo == 5){
      lcd.clear();
      lcd.clear();
      lcd.clear();
      lcd.print("Run time");  
      lcd.setCursor(0,1);
      lcd.print("Time : "); 
      lcd.setCursor(7,1); 
      lcd.print(Total_time);
      
  }else if (MenuNo == 6){
      lcd.clear();
      lcd.clear();
      lcd.clear();
      lcd.print("Start shot");  
 }
}


void multiply(){     
  
      if (mult == 1){
        multipliers = 1;
        } 
        else if (mult == 2){
        multipliers = 5;
        }
        else if (mult == 3){
        multipliers = 10;
        }
        else if (mult == 4){
        multipliers = 15;
        }
        else if (mult == 5){
        multipliers = 30;
        } 

         if (mult < 1){
          mult = 5;
        }else if (mult > 5) {
          mult = 1;
   }
}

void serialtest (){

       Serial.print("hours : "); 
       Serial.println(hours);
       Serial.print("mins : ");
       Serial.println(mins);
       Serial.print("total time : ");
       Serial.println(Total_time);
       Serial.print("# exp : "); 
       Serial.println(NoExp);
       Serial.print("exp time : "); 
       Serial.println(Exp_time);

  }

Which if-statement are you referring to?

This?

        if (mult < 1){
          mult = 5;
        }else if (mult > 5) {
          mult = 1;

Or this?

if (MenuNo >0 && MenuNo < 7){
          MenuNo ++;
          MenuItems();
    }

For the second one, only MenuNo : 1,2,3,4,5,6 would go into the if-statement and for MenuNo=1, MenuNo++ would give 2.

Thanks for the reply sorry for not getting back to you sooner it is the first one. After testing with the serial monitor I have found it to be working fine it's actually the if statement that deals with the switching of the values that seems to not be working. I'll add it below, for the time being I have simply made the statement loop back to zero instead of 1 which has sorted the problem but I'm not happy with it. Must be not updating at the right time but I can't see why.

void multiply(){     
  
      if (mult == 1){
        multipliers = 1; // this is the part in question that seems to not be working.
        } 
        else if (mult == 2){
        multipliers = 5;
        }
        else if (mult == 3){
        multipliers = 10;
        }
        else if (mult == 4){
        multipliers = 15;
        }
        else if (mult == 5){
        multipliers = 30;
        } 

         if (mult < 1){
          mult = 5;
        }
        
        if (mult > 5) {
          mult = 0; // shouldn't work come back and fix at some point
   }
}

Hi:

Try moving your boundary limit check and correction for "mult" variable before you test the value rather than after. You are using a range of 2-6 as your mult++ happens after the correction. Moving to 0-5 changed that to a range of 1-6 which works since you ignore the 6 anyway. But correct first and I think that will fix your problem.

Cheers!

//Check and correct mult to range 1-5 first
  if (mult < 1) {
    mult = 5;
  } else if (mult > 5) {
    mult = 1;
  }
//Now use the corrected range value of mult  
  if (mult == 1) {
    multipliers = 1;
  }
  else if (mult == 2) {
    multipliers = 5;
  }
  else if (mult == 3) {
    multipliers = 10;
  }
  else if (mult == 4) {
    multipliers = 15;
  }
  else if (mult == 5) {
    multipliers = 30;
  }

wzaggle:
Hi:

Try moving your boundary limit check and correction for "mult" variable before you test the value rather than after. You are using a range of 2-6 as your mult++ happens after the correction. Moving to 0-5 changed that to a range of 1-6 which works since you ignore the 6 anyway. But correct first and I think that will fix your problem.

Cheers!

Hi thanks for getting back to me that's done the trick appreciate the help.