Coding Troubleshooting for Ultrasonic Sensor and Water Pump

My concept is the water motor pump is automatically turned on/off when water level in the tank reached certain level. At the same time, I can also turn on/off the water pump manually. However, I’m facing some difficulty in the coding. Below is my coding.

int trigPin = D4;
int echoPin = A5;
int relay = D1;
int echovalue = 0;
float cm = 0.00;


void setup()
{
    Serial.begin(9600);
    pinMode(trigPin,OUTPUT);
    pinMode(echoPin,INPUT);
    pinMode(relay,OUTPUT);
    Particle.function(relay,relayToggle);
    Particle.variable("echovalue",echovalue);
    Particle.variable("cm",cm); 
}

void loop() 
{
    
    digitalWrite(trigPin, LOW);
    delayMicroseconds(5);
    digitalWrite(trigPin, HIGH);
    delayMicroseconds(10);
    digitalWrite(trigPin, LOW);
    echovalue = pulseIn(echoPin, HIGH);
    cm = echovalue/58.138;
    delay(1000);
    if (cm < 4) {  // This is where the LED On/Off happens
        digitalWrite(relay,LOW);}
    else if(cm>10){
        digitalWrite(relay,HIGH);
    }
    Particle.publish("Reading", String(cm) + "cm");
    
}

int relayToggle(String command) {

    if (command=="on") {
        digitalWrite(relay,HIGH);
        return 1;
    }
    else if (command=="off") {
        digitalWrite(relay,LOW);
        return 0;
    }
    else {
        return -1;
    }
}

I wish can get some explanation from anyone so that i can make correction accordingly. Your help is very appreciated. :smile:

Can I get a breakdown of:

What it’s supposed to do

Versus

What it’s doing

? I think we can all be more help then.

1 Like

This one is odd, as the first parameter should actually be a string and not an integer (which `int relay = D1;´ is).
There should be an error message about this.

This also poses a problem since cm should be double but is int - there should be an error message about this too.

Post the error messages will help more than this

Hi ScruffR. First of all, thanks for your explanation. :smile:
Below is the error messages.

Yup, exactly as I expected and, message clearly states.
The function you are calling (Particle.function() aka CloudClass::function()) expectes a certain data type (const char*) but you are providing an incompatible other (int).

Once you correct that, the code should at least build.

1 Like

What supposed to do:

  1. Motor pump is automatically turned on/off when water level in the tank reached certain level. (Using Photon)
  2. User can manually turn on/off the pump using android phone. (The apps I had done by using MIT app inventor)

What is doing:
There is some errors in the coding that i posted before. There might be some conflict happened. I need some help to understand what is wrong.

I tried to get you to see the problem, but it might have been too subtle

// this is wrong!
// Particle.function(relay,relayToggle);
// it should be something like this
 Particle.function("toggleRelay",relayToggle);

I just noticed i missed out that part. Thanks for your help. The code is verified. :wink:

Hi ScruffR, I’m facing some problem again.

My code was verified before this, however, the relay manage to on for few seconds only (for manually control part). It will not wait until I click the OFF button then only it turn off.
Hope can get some idea from you. Thanks. :wink:

I can’t quite follow :confused:
What’s the current code you have there?

But you have to think what happens after return from your relayToggle() and repeat going through loop() again.

Below is the current code. I just want to turn the relay on or off. Does it need to go through loop() function?

int trigPin = D4;
int echoPin = A5;
int relay = D1;
int echovalue = 0;
double cm = 0.00;


void setup()
{
    Serial.begin(9600);
    pinMode(trigPin,OUTPUT);
    pinMode(echoPin,INPUT);
    pinMode(relay,OUTPUT);
    Particle.function("relay",relayToggle);
    Particle.variable("echovalue",echovalue);
    Particle.variable("cm",cm); 
}

void loop() 
{
    
    digitalWrite(trigPin, LOW);
    delayMicroseconds(5);
    digitalWrite(trigPin, HIGH);
    delayMicroseconds(10);
    digitalWrite(trigPin, LOW);
    echovalue = pulseIn(echoPin, HIGH);
    cm = echovalue/58.138;
    delay(1000);
    if (cm < 4) {  
        digitalWrite(relay,LOW);}
    else if(cm>20){
        digitalWrite(relay,HIGH);
    }
    
}

int relayToggle(String command) {

    if (command=="on") {
        digitalWrite(relay,HIGH);
        return 1;
    }
    else if (command=="off") {
        digitalWrite(relay,LOW);
        return 0;
    }
    else {
        return -1;
    }
}

That is the standard behaviour and reason for the naming of this function loop() - it will always loop round and round.
If you don’t want the things in loop() to happen, you either take it out of loop() or make it conditional (keyword here would be if ()).

Look at your loop() and consider what it’ll do (over and over - before and after you called relayToggle() - btw, that’s not the most fitting name, because this function doesn’t really toggle the relay but rather sets its state).