This is amazing, i really appreciate the help. I used the code you provided and only the else is returning. Also, the code is running constantly without a magnet even being sensed.
#define W D2 //the waist sensor is on this pin
#define H D5 //the hip sensor is on this pin
int w_state=HIGH;//sets the waist measurement state for a grove hall effect sensor
int h_state=HIGH;//sets the hip measurement state for a grove hall effect sensor
int w_count=70;//our testing rig has an initial waist size of 70cm
int h_count=82;//our testing rig has an initial hip size of 82cm
bool w_isNearMagnet = W;
bool h_isNearMagnet=H;
int w_sensorValue;
int h_sensorValue;
void setup() {
pinMode(D2, INPUT);
pinMode(D5, INPUT);
}
void loop() {
sensor1();
sensor2();
}
void sensor1() {
if (w_isNearMagnet)//if the Grove sensor is near the magnet?
measure();
}
void sensor2() {
if (h_isNearMagnet)//if the Grove sensor is near the magnet?
measure();
}
bool measure() {
h_sensorValue = digitalRead(H);
w_sensorValue = digitalRead(W);
if(h_sensorValue == LOW)//if the hip sensor value is LOW
{
h_count +=1 ;//count each low state and add it to the intial hip size.
calc_ratio();
}
else
{
return false;//no,return false
}
if(w_sensorValue == LOW)//if the waist sensor value is LOW
{
//COUNT(w_count + COUNT);//count each low state and add it to the intial waist size.
w_count+=1;
calc_ratio();
}
else
{
return false;//no,return false
}
}
float calc_ratio() {
if (h_sensorValue != 0) { // <-- make sure the divisor is NOT zero
float ratio = w_sensorValue / h_sensorValue;
char txt[16];
snprintf(txt, sizeof(txt), "%.2f", ratio);
Particle.publish("ratio", txt, PRIVATE);
return (ratio);
}
else {
Particle.publish("ratio", "80", PRIVATE);
return -1;
}
}