variable value from IF statement

hello everyone.

i have a problem with a variable.

in this IF statement i use it get “hits” value to voltsp variable .

upswitchState = upPin;
  if (upswitchState != prevSwitchState) {
    if (upswitchState == 1) {
      hits = hits + 0.1;

   voltsp = hits;

      virtuino.vMemoryWrite (13, hits);
    }
  }

in this IF statement i use to control the faction between Real Voltage to the set-point(voltsp)

the problem is that i don’t get any value from this variable (voltsp) in this IF statement

if (rv < voltsp)
  {
    digitalWrite (CamRelay, LOW);
    virtuino.vMemoryWrite(20, 0);

  } else {
    digitalWrite (CamRelay, HIGH);
    virtuino.vMemoryWrite(20, 1);

  }

Here is the Full code:

is still in “debug mode”, so there are few thing that are not in use.

Thank you for any help.

#include <Wire.h>
#include "VirtuinoBluetooth.h"
#include "DHT.h"
#include <SoftwareSerial.h>
//---------------------------------------------------------------------------------
SoftwareSerial bluetoothSerial =  SoftwareSerial(8, 9);
VirtuinoBluetooth virtuino(bluetoothSerial, 9600);
//---------------------------------------------------------------------------------
#define DHTPIN 4
#define DHTTYPE DHT11
int sensorPin = A0;
DHT dht(DHTPIN, DHTTYPE);
int CamRelay = 10;

float hits = 12.4;       // counter for the number of button presses
float upswitchState = 0;     // current state of the button
float downswitchState = 0;  // current state of the button
float prevSwitchState = 0;    // variable to hold previous value of the switchpin
float Display = 0;


float vout = 0.0;
float vin = 0.0;
float R1 = 10000.0;
float R2 = 7500.0;


//************************************************
//***********_________SETUP______________***********
//************************************************

void setup() {
  bluetoothSerial.begin(9600); //To comunicate with the app via BT
  dht.begin(); //initialize DHT sensor

  pinMode (CamRelay, OUTPUT);

  Serial.begin(9600);

  //---------------------------------------------------------------------------------

  float prevSwitchState = 12.4;
  virtuino.vMemoryWrite (13, prevSwitchState);
}

//************************************************
//***********_________LOOP______________***********
//************************************************

void loop() {
  float t = dht.readTemperature();
  float h = dht.readHumidity();
  //  digitalWrite (CamRelay, HIGH);
  virtuino.run();

  //---------------------------------------------------------------------------------
  float hic = dht.computeHeatIndex(t, h, false);
  float sensorValue;
  sensorValue = analogRead(sensorPin);
  vout = (sensorValue * 5.0) / 1024.0;
  vin = vout / (R2 / (R1 + R2));
  float rv = (vin * 1.21);
  Serial.println("rv");
  Serial.println(rv);

  //---------------------------------------------------------------------------------
  virtuino.vMemoryWrite(1, t);
  virtuino.vMemoryWrite(2, h);
  virtuino.vMemoryWrite(3, rv);
  //---------------------------------------------------------------------------------
  int upPin = virtuino.vDigitalMemoryRead(12);
  int downPin = virtuino.vDigitalMemoryRead(11);
  const int buttonPin = 6;
  float voltsp ;
  //---------------------------------------------------------------------------------
  //---------------------------------------------------------------------------------


  if (rv < voltsp)
  {
    digitalWrite (CamRelay, LOW);
    virtuino.vMemoryWrite(20, 0);

  } else {
    digitalWrite (CamRelay, HIGH);
    virtuino.vMemoryWrite(20, 1);

  }
  //---------------------------------------------------------------------------------
  //---------------------------------------------------------------------------------

  // read the pushbutton up input pin:
  upswitchState = upPin;
  if (upswitchState != prevSwitchState) {
    if (upswitchState == 1) {
      hits = hits + 0.1;

      voltsp = hits;

      virtuino.vMemoryWrite (13, hits);
    }
  }

  // save the current switch state as the last state
  prevSwitchState = upswitchState;

  // read the pushbutton down input pin:
  downswitchState =  downPin;


  //   compare the buttonState to its previous state
  if (downswitchState != prevSwitchState) {
    if (downswitchState == 1) {
      hits = hits - 0.1;
      Serial.print("Hits:-  " );
      Serial.println(hits);
      voltsp = hits;
      Serial.print("Voltage Set Point: ");
      Serial.println(voltsp);
      virtuino.vMemoryWrite (13, hits);
    }
  }


  // save the current switch state as the last state
  prevSwitchState = downswitchState;
  virtuino.vDelay(150);

}

the problem is that i don’t get any value from this variable (voltsp) in this IF statement

if (rv < voltsp)
  {
    digitalWrite (CamRelay, LOW);
    virtuino.vMemoryWrite(20, 0);

  } else {
    digitalWrite (CamRelay, HIGH);
    virtuino.vMemoryWrite(20, 1);

  }

Why would you expect to get a value ? volstp is not changed by that program segment.

  float voltsp ;
  //---------------------------------------------------------------------------------
  //---------------------------------------------------------------------------------
  if (rv < voltsp)

What do you expect the value of voltsp to be ?

If you have problems with the execution of the if clause then it is a good idea to print the values being tested before doing so and to print a message in the conditional code so that you know that it has been executed.

the voltsp is a set-point that i choose from my phone app (virtuino).
every time i hit + in my app it add 0.1 to hits.
this value i want put in voltsp to be a set-point and then to use it in the formula “if (rv < voltsp)” to make my action.

i can see it in the serial monitor only in this IF statement .

 upswitchState = upPin;
  if (upswitchState != prevSwitchState) {
    if (upswitchState == 1) {
      hits = hits + 0.1;

   voltsp = hits;

      virtuino.vMemoryWrite (13, hits);
    }
  }

in the other IF statement i cant see it in the serial monitor.

hope you can understand me, sorry for my English.

Make a simple sketch that only receives a value from the phone. It will be easier to debug.