Why is my pin reading zero in this function?

Whenever I change V11 and V12, they are printed correctly in the terminal but whenever I change V8 between 1 and 0, both values are always printed as zero. But when I change V11 and V12, they still print correctly. It’s as if they aren’t speaking to each other. Why is this and how do I fix?

My code:

BLYNK_WRITE(V11){
    float homeAngle = param.asFloat();
    Serial.println(homeAngle);
  }

BLYNK_WRITE(V12){
    float awayAngle = param.asFloat();
    Serial.println(awayAngle);
  }



//TimedInput
BLYNK_WRITE(V8)
  {

  if (param.asInt()==1)
  {
    float adjustedHA = mapfloat(homeAngle, 68, 90, 170, 40);
    servo.write(adjustedHA);
    int tempDisplay = mapfloat(servo.read(), 170, 40, 68, 90);
    Blynk.virtualWrite(V0, tempDisplay);
    Serial.println(homeAngle);
    //Serial.println(adjustedHA);
      } else {
    float adjustedAA = mapfloat(awayAngle, 68, 90, 170, 40);
    servo.write(adjustedAA);
    int tempDisplay = mapfloat(servo.read(), 170, 40, 68, 90);
    Blynk.virtualWrite(V0, tempDisplay);
    Serial.println(awayAngle);
    //Serial.println(adjustedAA);
        }
}

Please edit your post to add triple backticks at the beginning and end of your code so that it displays correctly.
Triple backticks look like this:
```

Once you’ve done that we’ll explain your error to you.

Pete.

1 Like

You have to declare float homeAngle and float awayAngle out of the function.

float homeangle , awayAngle ;
1 Like

I did that! I have them set as global variables and the issue still persists. I also tried to have the adjustedAA and adjustedHA as global variables and I’m still getting the same thing. It’s only in the longer if statement that I get a zero and I really am baffled.

1 Like

Post your updated code.

Pete.

1 Like

Here is my updated code:

float homeAngle, awayAngle, adjustedHA, adjustedAA;

BLYNK_WRITE(V11){
    float homeAngle = param.asFloat();
    Serial.println(homeAngle);
    float adjustedHA = mapfloat(homeAngle, 68, 90, 170, 40);
    Serial.println(adjustedHA);
  }

BLYNK_WRITE(V12){
    float awayAngle = param.asFloat();
    Serial.println(awayAngle);
    float adjustedAA = mapfloat(awayAngle, 68, 90, 170, 40);
    Serial.println(adjustedAA);
  }



//TimedInput
BLYNK_WRITE(V8)
  {
  if (param.asInt()==1)
  {
    float adjustedHA = mapfloat(homeAngle, 68, 90, 170, 40);
    servo.write(adjustedHA);
    int tempDisplay = mapfloat(servo.read(), 170, 40, 68, 90);
    Blynk.virtualWrite(V0, tempDisplay);
    Serial.println(homeAngle);
    //Serial.println(adjustedHA);
      } else {
    float adjustedAA = mapfloat(awayAngle, 68, 90, 170, 40);
    servo.write(adjustedAA);
    int tempDisplay = mapfloat(servo.read(), 170, 40, 68, 90);
    Blynk.virtualWrite(V0, tempDisplay);
    Serial.println(awayAngle);
    //Serial.println(adjustedAA);
        }
}  

When I change V11 and V12, I get the proper readings in the terminal, but when I alter V8 I get a zero when I change to 1 or 0. Here is what the terminal looks like:

68.00
170.00
89.00
45.91
90.00
40.00
0.00
0.00

delete float !! :smile: :wink:

4 Likes

WOW oh my goodness…You are a genius! Can you maybe explain to me why that ruined it so I can never make that mistake again?

2 Likes

because when you declare float in the function, homeangle is local and not global, which resets the homeangle variable.

1 Like

Thank you very much! This means a lot!

1 Like

You are welcome my friend :wink: