Yes, you did.
You still have lastState
as local variable and you need one instance of a lastState
variable per state you want to keep track of (aka per sensor).
Your doorvalue()
is missing a return value for the case you submit an unsupported command.
And I will repeat what I keep saying to others too:
When you have code blocks that do the absolutely same thing but only differ in variable names, then you should use arrays, loops and possibly enums to reduce code by reusing the active part of the code and just substitute the variables.
Potential candidates for that kind of cleanup:
pinMode(BackDoor,INPUT);
pinMode(Front,INPUT);
pinMode(Garage,INPUT);
pinMode(Basement,INPUT);
pinMode(Bedroom,INPUT);
// and
BedValue = analogRead(Bedroom);
BaseValue = analogRead(Basement);
GValue = analogRead(Garage);
BDvalue = analogRead(BackDoor);
FDvalue = analogRead(Front);
// and multiple instances of this block
if (BaseValue < 3000 && lastState != true && armedcommand==1) {
lastState = true;
Particle.publish("Door_Open","Basement",60,PRIVATE);
delay(5000); // to cater for event rate limit
}
else if (BaseValue >= 3000) {
lastState = false;
}
// and
if (command=="garage"){
return GValue;
}
else if (command=="backdoor") {
return BDvalue;
}
else if (command=="frontdoor"){
return FDvalue;
}
else if (command=="bedroomdoor"){
return BedValue;
}
else if (command=="basementdoor"){
return BaseValue;
}
So your line count could easily be reduced by two thirds (I boiled it down to ~40 lines).