Go Down

Topic: make values common in all menus (Read 455 times) previous topic - next topic

UKHeliBob

Quote
Is there a way to return a value to global scope?
You do not need to return a global variable from a function as it will be available throughout the program

Quote
in my " resting Voltage Menu" I want to change the "float resting Voltage " in the global scope.
Just change its value and you can use it in any part of the program.
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

groundFungus

#16
Sep 13, 2017, 05:36 pm Last Edit: Sep 13, 2017, 05:37 pm by groundFungus
Remember to remove the data type specifier (float) from the variable inside the function or you end up with 2 of the same variable, one global and one local to the function.

carlosfandango

I'm not quite sure what you both mean...

Am I right in thinking that if I'm in one of my menus I am able to change the value of a global float?

The reason I need to do that is because every menu has to change a global float value so my main code will execute with the newly changed parameters. does that make sense? 

 

groundFungus

The reason for making a variable of global scope is so that the variable is accessible from any where in the sketch.  So, yes, you can change the variable in any menu and get its value in any menu.

UKHeliBob

Quote
Am I right in thinking that if I'm in one of my menus I am able to change the value of a global float?
Yes, that is the whole point of making a variable global.
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

carlosfandango

Thanks, that makes sense.

how can I change the value of restingVoltage in the global scope via one of the menus?

This code doesn't work and I can't figure out why.

Code: [Select]
void menuItem2() { // Function executes when you select the 2nd item from main menu
  
  int activeButton = 0;
  int button;
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("VOlTS NOW =");
  lcd.setCursor(0,1);
  lcd.print ("REST SET");
  
 
  menuState2=true;
  

  while(menuState2==true){
  
  int sensorValue1 = analogRead(A2);
  voltage1 = sensorValue1  * (50.00 / 1024);
  lcd.setCursor (10,0);
  lcd.print(voltage1);
  lcd.setCursor (10,1);
  lcd.print(restingVoltage);
  analogRead(0);
  if(readKey <195){                //up button
    restingVoltage = 13.4 ;       // sets restingVoltage to 13.4
  }
    else if
    (readKey <380) {               //down button
      restingVoltage = 12.0;       // sets restingVoltage to 13.4
    }

  
  delay(100);
  
    readKey = analogRead(0);
    if (readKey < 790) {        
      delay(100);
      readKey = analogRead(0);
      
    button = evaluateButton(readKey);
    switch (button) {
      case 4:                  // This case will execute if the "back" button is pressed
        button = 0;
        activeButton = 1;
        menuState2=false;
        break;
    }
  }
}
} [code]
[/code]

UKHeliBob

Quote
This code doesn't work
Tells us nothing


What should it do ?
What does it do ?
Why did you not post the complete program ?
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

carlosfandango

I've posted the full code on the first page. I've just updated it to where I am now.

using my LCD menu and buttons I am trying to change the value of "restingVoltage" which is a float in the global scope. Once the value has changed via my menu and buttons the code in my "home" menu will run with the newly set values.

UKHeliBob

Does the program ever call the menuItem2() function ?
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

carlosfandango

No it doesn't.
Your comment gave me some food for though and I've got it working now, so thank you!

carlosfandango

One last thing that would be useful to know, or if you can point me in the right direction to find out...

When I hold the up or down button in a menu, I want the value of X to change like this :
13.6, 13.7, 13.8, 13.9 ,14.0, 14.1 and so on....

But at the money when I hold a button it changes like this:
13.6, 14.6, 15.6, 16.6....

I've played about with it for most of the day and don't seem to be getting very far, or at least, I know how it doesn't work!

Code: [Select]
void menuItem2() { // Function executes when you select the 2nd item from main menu
  float x;
  x = 13.6;
  int activeButton = 0;
  int button;
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("VOlTS NOW =");
  lcd.setCursor(0,1);
  lcd.print ("REST SET");
  menuState2=true;

// this will run while menuState2 is true
  while(menuState2==true){
  int sensorValue1 = analogRead(A2);
  voltage1 = sensorValue1  * (50.00 / 1024);
  
// Setup and Print Values to LCD
  lcd.setCursor (10,0);
  lcd.print(voltage1);
  lcd.setCursor (10,1);
  lcd.print(restingVoltage);
  
  readKey = analogRead(0);
  if(readKey == 99){          //holding the UP button
    (restingVoltage =x++);    // will count from 13.6
     restingVoltage = x;      // the value stopped at will be the value of X and restingVoltage
    }
    else if (readKey == 256) {    // holding the DOWN button
          (restingVoltage =x--);  //  will count from X down
     restingVoltage = x;          // the value stopped at will be the value of X and restingVoltage
    }
     delay(100);
 
    readKey = analogRead(0);
    if (readKey < 790) {
      
      delay(100);
      readKey = analogRead(0);
    button = evaluateButton(readKey);
    switch (button) {
      case 4:  // This case will execute if the "back" button is pressed
        button = 0;
        activeButton = 1;
        menuState2=false;
        
        break;
    }
  }
}
} [code]
[/code]

UKHeliBob

Code: [Select]
   (restingVoltage =x++);    // will count from 13.6
adds 1 to the value of x and assigns it to restingVoltage

try
Code: [Select]
   (x+= 0.1);    // will count from 13.6
restingVoltage = x;
 

or you cold just change the value of restingVoltage and not use an intermediate variable.
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

PaulS

Code: [Select]
 float x;
  x = 13.6;

Why do you feel the need to use two lines of code to declare and initialize a value?

Code: [Select]
 float x = 13.6;

Code: [Select]
   (restingVoltage =x++);    // will count from 13.6
(Why) (are) (there) (parentheses) (in) (this) (statement) (?)

Why do you think that x = x + 1 should add 0.1?
The art of getting good answers lies in asking good questions.

carlosfandango

PaulS:

 I have no idea. I don't normally but thanks for pointing it out.

Again, I'm not sure - Thanks for pointing out that it was unnecessary.

I didn't think that, I just used that code as a way I could test the value was actually being changing so when I upped my input voltage the voltage was compared to the new changed values.
I asked for help to be able to add 0.1 because my other attempts didn't work.

Thanks

Go Up