How to take a value from braces?

I am trying to make a device that records a certain value within the touch of a button, then when taken to a serial monitor, will print the value.

An example of the specific code would be:

 if (ButtonState == 1)
  {
    float Value1 = Value2;
  }
  Serial.println(Value1);
}

So how would I go about taking "Value1" from the inside of the braces and printing it?

One way is to define Value1 as a global variable.

Or

Make a local variable Value1 in your function.

I looked at multiple different tutorials and forums but they didn't seem to work, so how do I make the variable global, or what do you mean by making Value 1 in my function?

i think you're asking about "variable scope".

your code both defines and initializes "Value1" within the scope of the conditional code delimited by the braces of the if condition. If you don't have another definition for Value1 before the if statement, your code should generate an error because Value1 is not outside the braces.

if you defined Value1 outside the braces, Value1 would be valid in your print

float Value1;
if (ButtonState == 1)
{
    Value1 = Value2;
}
Serial.println(Value1);

of course you define Value1 outside the function containing your code

float Value1;

void
myFunc (void)
{
    if (ButtonState == 1)
    {
        float Value1 = Value2;
    }

    Serial.println(Value1);
}

Read:

Ok so I tried the global variable thing but it didn’t seem to actually print the variable from inside the braces, I will give an example of what my code looks like and can you tell me what I did wrong?

int Value1 = 8;
int Value2 = A0;
float Value3;

void setup()
{
  Serial.begin(9600);
  pinMode(Value1, INPUT);
}

void loop()
{
  int Value4 = digitalRead(Value1);
  if (Value4 == 1)
  {
    float Value5 = ((analogRead(Value2)) “equation”;
    float Value3 = (Value5 “equation) “equation”;
  }
  Serial.println(Value3);
}
int Value1 = 8;
int Value2 = A0;

float Value3;
float Value5;

void setup()
{
  Serial.begin(9600);
  pinMode(Value1, INPUT);
}

void loop()
{
  int Value4 = digitalRead(Value1);
  if (Value4 == 1)
  {
    Value5 = ((analogRead(Value2)) "equation";
    Value3 = (Value5 "equation) "equation";
  }
  Serial.println(Value3);
}

I will give an example of what my code looks like and can you tell me what I did wrong?

You are inventing syntax, and your code does not compile. :frowning:

I'm just filling in the variables with random words, as an example.

Still isn't able to read the variable that is inside of the braces, I am extremely confused.

Let’s see your sketch as it is now.

Go back a couple of steps...
Read or search to understand the ‘scope’ of variables.

The ‘scope’ determines where a variable is available to be set or read.
Scope is determined by ‘where,’ that particular variable is declared within the program structure.

int Value1 = 8; //global variable Value1 available anywhere in the program
int Value2 = A0; //global variable Value2 available anywhere in the program
float Value3; //global variable Value3 available anywhere in the program

void setup()
{
  Serial.begin(9600);
  pinMode(Value1, INPUT);
}

void loop()
{
  int Value4 = digitalRead(Value1); //local variable VALUE4 only available in the function
  if (Value4 == 1)
  {
    float Value5 = analogRead(Value2);  //local variable Value5 only available inside the braces
    float Value3 = Value5 * 2;  //local variable Value3 only available inside the braces
  }
  Serial.println(Value3); //prints the global variable Value3 as the other one with the same name is out of scope
}