Using changing variables inside of functions defined in setup()

I am using an arduino uno and adafruit 16x2 LCD monitor with 5 buttons. I am trying to make a program where I can define a value to two variables (up and down buttons change one, left and right buttons change the other), whereupon those two variables control two separate timers.

I got the code mostly working, but I have a problem with how c++ is handling the variables.

This is what I have so far:

// include the LCD library code, as well as simpletimer:
#include <Wire.h>
#include <Adafruit_MCP23017.h>
#include <Adafruit_RGBLCDShield.h>
#include <SimpleTimer.h>


Adafruit_RGBLCDShield lcd = Adafruit_RGBLCDShield();


SimpleTimer timer;

//MotorDelay Function
void MotorDelayFunction() {
  Serial.println("Motor Step");        
}

//Camera Interval Delay Function
void CamDelayFunction() {
  Serial.println("Camera Shutter");  
}

  int lapserunning = 0;
  int cameradelay = 0;
int motordelay = 0;
  
//  int motordelayms = motordelay * 10;
 // int cameradelayms = cameradelay * 1000;

void setup() {

  
  // Debugging output
  Serial.begin(9600);
  // set up the LCD's number of columns and rows: 
  lcd.begin(16, 2);
  // Attatch delay variables to camera and motor delay functions
  
//RIGHT HERE. This is where I get messed up. The function sees this as essentially "0" for both camera and motor delay. It doesn't recognize the global variable.

  timer.setInterval(cameradelay, CamDelayFunction);
  timer.setInterval(motordelay, MotorDelayFunction); 
 
}

uint8_t i=0;
void loop() {
  
  
  // set the cursor to column 0, line 1
  // (note: line 1 is the second row, since counting begins with 0):
 // lcd.setCursor(0, 1);
  // print the number of seconds since reset:
//  lcd.print(millis()/1000);

  uint8_t buttons = lcd.readButtons();

  if (buttons) {
    lcd.clear();
    lcd.setCursor(0,0);
    if (buttons & BUTTON_UP) {
      lcd.print("Motor Speed:  ");
    lcd.setCursor(13,0);
      motordelay++;
      lcd.print(motordelay);
    }
    if (buttons & BUTTON_DOWN) {
      lcd.print("Motor Speed:  ");
    lcd.setCursor(13,0);
      motordelay--;
      lcd.print(motordelay);
    }
    if (buttons & BUTTON_LEFT) {
      lcd.print("CamIntvl(sec):");
    lcd.setCursor(14,0);
      cameradelay--;
      lcd.print(cameradelay);
    }
    if (buttons & BUTTON_RIGHT) {
      lcd.print("CamIntvl(sec):");
    lcd.setCursor(14,0);
      cameradelay++;
      lcd.print(cameradelay);
    }
    if (buttons & BUTTON_SELECT) {
      if(lapserunning == 1){
        lapserunning--;
      lcd.setCursor(0,0); 
      lcd.print("STOPPED");
       Serial.print("STOPPED");
      
      }
       
       else {
      
      lapserunning++;
      Serial.print("RUNNING");
      lcd.setCursor(0,0); 
      lcd.print("RUNNING");
        
      }
     
  }
  }
    if (lapserunning == 1){
         // Serial.print(cameradelay);
          // Serial.print(motordelay);

      timer.run();
  }
}

From what it looks like, because it runs in the setup() function I’m not able to hand a changing variable to it. What workarounds could I use to get around this?

I’m pretty new to c++, but I have quite a bit of electronics knowledge. This programming really gets my goat sometimes.

Any help would be appreciated! I read up on scope, how variables are handled, and I still got nothing.

Thanks!

Collins.

int cameradelay = 0;
int motordelay = 0;
...

  //RIGHT HERE. This is where I get messed up. The function sees this as essentially "0" for both camera and motor delay. It doesn't recognize the global variable.

  timer.setInterval(cameradelay, CamDelayFunction);
  timer.setInterval(motordelay, MotorDelayFunction);

What do you mean? The global variable is zero.

That global variable changes as the button presses increment and decrement the value, so anywhere inside the loop() that I recall the variable, the new value is shown. Is there any way to get the new variable up into the setup() function?

No.

Once setup finishes, what it did, is done.

//RIGHT HERE. This is where I get messed up. The function sees this as essentially "0" for both camera and motor delay. It doesn't recognize the global variable.

Yes it does recognise the global varaible - or it wouldn't compile. It recognises it at the instant that function is called, and never again. You will need to modify your timer (I'm not sure how with that one) to have the new delay value when that value changes.

Is there any way to get the new variable up into the setup() function?

Are you asking for some form of persistence across reset/power-cycles? Have a look at EEPROM handling.