I am trying to add an Arm Function to my simple security system using the particle.function to arm or disarm my system. I can’t seem to get the function result to be part of the loop. Any suggestions.
Here is the code.
int BackDoor = A0;
int Front= A3;
int Garage = A1;
int Basement = A2;
int Bedroom = A4;
int LED = D7;
int BDvalue;
int FDvalue;
int GValue;
int BaseValue;
int BedValue;
int power = A5;
int isitarmed;
int armedcommand;
void setup() {
pinMode(BackDoor,INPUT);
pinMode(Front,INPUT);
pinMode(Garage,INPUT);
pinMode(Basement,INPUT);
pinMode(Bedroom,INPUT);
pinMode(power,OUTPUT);
Particle.variable("Bdoor", &BDvalue, INT);
Particle.variable("Fdoor", &FDvalue, INT);
Particle.variable("Gdoor", &GValue, INT);
Particle.variable("Armed", &isitarmed, INT);
analogWrite(power, HIGH);
Particle.function("Arm",Armed);
}
void loop() {
BedValue = analogRead(Bedroom);
delay (1000);
BaseValue = analogRead(Basement);
delay (1000);
GValue = analogRead(Garage);
delay (1000);
BDvalue = analogRead(BackDoor);
delay (1000);
FDvalue = analogRead(Front);
delay (1000);
isitarmed=armedcommand;
Particle.variable("Armed", &isitarmed, INT);
if (isitarmed = 1) {
if (BaseValue <3000) {
Particle.publish("Door_Open","Basement",60,PRIVATE);
delay(10000);
}
else {
}
if (GValue <3000) {
Particle.publish("Door_Open","Garage",60,PRIVATE);
Particle.variable("Bdoor", &BDvalue, INT);
delay(10000);
}
else {
}
if (BDvalue <3000) {
Particle.publish("Door_Open","Back_Door",60,PRIVATE);
Particle.variable("Gdoor", &GValue, INT);
delay(10000);
}
else {
}
if (FDvalue <3000) {
Particle.publish("Door_Open","Front_Door",60,PRIVATE);
Particle.variable("Fdoor", &FDvalue, INT);
delay(10000);
}
else {
}
if (BedValue <3000) {
Particle.publish("Door_Open","Bedroom_Door",60,PRIVATE);
delay(10000);
}
else {
}
}
}
int Armed(String command){
if (command=="on") {
armedcommand=1;
return 3;
}
else {
armedcommand=0;
return 2;
}
}
Yes, I will see the return calls. But If I go to disarm it in the particle app it will show the return, but it will not change “isitarmed” to 0. So it stays armed the whole time.
The suggestion from @Ric is the one that will fix your main problem.
You have particle.variable() commands scattered around in your code.
These should only be at the beginning of your setup() function.
This will make your code more efficient at the least.
It may even prevent a rare problem.
Please also take a look at your Armed() function.
If you give the “on” command, it appears to work fine.
But, what happens if you give it the command “breakin” or anything else other than “off”?
I am guessing this behaviour is not what you want…
I took the liberty to "slightly" reformat your code (see how to embed code blocks) and removed some superfluous blank lines. You can make your code much more readable by adopting a consistent indentation scheme than with spreading out your code over more lines than necessary by sprinkling it with blank lines.
About the use of Particle.variable()
Actually sprinkling Particle.variable() inside loop() is not only less efficient, it's actually wrong and will actually break the variables. Additionally (currently) any change to the cloud "interface" (set of Particle.variable(), Particle.function() & Particle.subscribe() statements to define the interface) some time after Particle.connect() won't have any effect on the cloud side as the changes are only communicated to the cloud within a few seconds after the connection was established.
BTW, the three parameter call is deprecated. Look at the docs to see how it's done nowadays.
For that reason some C/C++ devs adopt a slightly confusing but protecting syntax with equality checks.
// instead of
if (someVar == 10)
// they always write it the other way round
if (10 == someVar)
// and that way a missing = in
if (10 = someVar)
// would fail at compile time with a clear error message.
(obviously only protects you when using literals or const variables on the left side)
BTW, statements like if (someVar = 10) would produce a compiler warning, but when you have no errors you will never get to see them. In such a case, you can try to add a deliberate syntax error (e.g. a stray { after your last }). This way you will be presented with an error message and in Web IDE you can then hit SHOW RAW to actually see the compile output including the warnings.
@jzalar, My biggest other suggestion is what I have learned about @Scruffr’s posts.
Re-read his post a second time. Try to really understand it. Then read it again… there is a huge wealth of knowledge there.
Your alarm system is simple, as you said. When you get it going, you will get the chance to observe how it behaves. Then think about how you would like it to behave; let that guide you in developing it into a system that is great.