If / else parameter won't compile

Dear all,

First post but I’ve been tinkering with these things for a while. Must admit that my expertise is more medical than electrical.

I can’t get this to compile for some reason, and despite scouring the internet, can’t figure it out. Any pointers? It works fine if I just publish on a loop, so I’m missing something in the if / else syntax construction I’ve now added.

void loop() {

        distance = rangefinder.getDistanceCM();
        percentage=100-((distance*100)/heightTank);
    {
    if (percentage >= 10){
        Particle.publish("saltLevel", String(percentage)"%", 60, PRIVATE);
    }
    else {
        Particle.publish("saltLevel", String(percentage)"% - refill due", 60, PRIVATE);
    }
    
    delay(60000);

}

You have mismatched braces. The second ‘{’ in your code should not be there.

1 Like

Thanks rvnash, but this still won’t compile:

void loop() {

    distance = rangefinder.getDistanceCM();
    percentage=100-((distance*100)/heightTank);
    
    if (percentage >= 10){
      Particle.publish("saltLevel", String(percentage)"%", 60, PRIVATE);
    }
    else {
      Particle.publish("saltLevel", String(percentage)"% - refill due", 60, PRIVATE);
    }

delay(60000);
}

start commenting lines out line by line until it does then you will know which one is the problem.

That code looks syntactically correct to me, but without seeing the rest of the source code in your project it is hard to know for sure. Is that the entire file? Where are distance and percentage declared, for example? Perhaps you could shared the entire source file?

Mikemoy,

Thanks - that really helped. The issue was with the “%” in the publish lines. This works now:

void loop() {

    distance = rangefinder.getDistanceCM();
    percentage=100-((distance*100)/heightTank);
    
    if (percentage >= 10){
      Particle.publish("saltLevel", String(percentage) + "%", 60, PRIVATE);
    }
    else {
      Particle.publish("saltLevel", String(percentage) + "% - refill due", 60, PRIVATE);
    }

delay(60000);
}

Oh, i didn't noticed the first time, this is a bit screwed up. the right syntax to concatenate strings together is using the overridden "+" operator, like this:
Particle.publish("saltLevel", String(percentage)+"%", 60, PRIVATE);

2 Likes