Particle.function and status of the pin

Hello
I am working on an application that allows either through the humidity level or manually (console or mobile phone) to control the speed of a three-speed fan. For this I used the particle.function. In order to avoid that more than one speed is activated in the manual function, I put a command to deactivate the other two speeds and it works (see attachment). It remains for me to understand how to send the status of the speeds off so that 0 appears on the console or on the mobile phone apps for the two speeds which are low(Lstato=Off and Hstato=Off).
Regards Valentino

 int MidspeedFunction(String args)
    {
    if (Mstato == OFF)
        {
            Mstato=ON;
            
            digitalWrite(RLowspeed,LOW);
            digitalWrite(RHighspeed,LOW);
	        digitalWrite(RMidspeed,HIGH);
	        Lstato = OFF;
            Hstato = OFF;
	        Relais="Mspeed";
            return ON;
	    }
	else if (Mstato == ON)
        {
            Mstato = OFF; // engine stopped
            digitalWrite(RMidspeed,LOW);
            Lstato=digitalRead(RLowspeed);
        return OFF;
        }
}

In setup(), just above your Particle.function() statement, make two more statements to show the two values or status in the console or app. Something like this:

Particle.variable("my_Lstato", Lstato);   
Particle.variable("my_Hstato", Hstato);   

I am assuming Lstato & Hstato are defined as integer (int). It would help to show your code.

Hopefully, these two new variables will appear in the console and the app. To refresh the data, click "GET" (console) or tap (app).

Thanks for the answer, probably I didn't express myself correctly, in manual mode I start with setting the speed Low (both in the console and on the app) near "f Lspeed appears 1 (ON)" now I change the fan speed and put Mspeed and as above in "f Mspeed 1 (ON)" appears, the fan runs at this speed while the Lspeed is zero but not on the display, see attached photo. I hope I have expressed myself correctly.
Regards Valentino

image

Hi Valentino,
I see what you are trying to accomplish now. Frankly, I do not know how to do it! But, I do have a different recommendation. I would use only one Particle.function() to command the fan manually and then one Particle.variable() to review/display the actual state of the fan.

I would try using an enum when controlling the possible states of the fan. Here is an example enum:

enum fan_states {                                         
  /* 00 */  fan_Off, 
  /* 01 */  fan_Low_Speed, 
  /* 02 */  fan_Medium_Speed, 
  /* 03 */  fan_High_Speed
};

In logic, when humidity levels change or, manually, when you want to immediately affect the fan operation, make a change in the fan state variable by choosing from one of the four choices in the enum. Here is a particle variable & function example:

Particle.variable("present_fan_state", fanState);       // value showing only one of enum values
Particle.function("change_fan_state", changeFanState);  // routine changeFanState() called

Here is what your changeFanState() routine might look like:


int changeFanState(String command)  { //  command thru particle cloud to change global fan state
  if (command.toInt() >= 0 && command.toInt() <= 3)  { //  allow only 1 of 4 possible states  
    if (g_fanState != command.toInt()) { //  command is different from present global fan state
      g_fanState = command.toInt();      // so, change the global fan state
      // do some stuff
      return 1;
    } else {
      return -89; // command same as present global fan state
    }
  } else {
      return -99; // invalid command
  }
}   //  END changeFanState  function

Here is a routine example dealing with humidity:

void changeFanStateBasedOnHumidity() { // global humidity value controls global fan state
  int changeFanState;
  if (g_humidity >= 70.0) {         //  high speed
    changeFanState = fan_High_Speed;
  } else if (g_humidity >= 60.0) {  //  medium speed
    changeFanState = fan_Medium_Speed;
  } else if (g_humidity >= 50.0)  { //  low speed
    changeFanState = fan_Low_Speed;
  } else {//  g_humidity < 50.0         turn off fan
    changeFanState = fan_Off;
  }
  if (changeFanState != g_fanState) { // result is a different state from present global fan state
    g_fanState = changeFanState; // so, change the global fan state     oops! forgot this line in original post!
    //  do some stuff
  }
} //  END changeFanStateBasedOnHumidity  
1 Like

Thanks Rob as I’m curious I’ll try your solution and let you know this anyway in the next few days.
Regards Valentino

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.