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.
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.
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.
Motor pump is automatically turned on/off when water level in the tank reached certain level. (Using Photon)
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.
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.
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 yourloop() 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).