Particle Photon digitalRead result is not always 1 or 0 but many times is -1515870811

The Particle Photon is on a breadboard with USB power directly from a computer. Running the code below to test digitalRead–does it always provides either 0 or 1. It appears to be very unreliable giving the result -1515870811 many times.

Physically, I am just moving a jumper wire between D0 and either 3V3 or ground.

This result is obtained by pulling the value using “particle get xxxx status” using the CLI.

Is this expected behavior? If it is expected, is the problem with the reliability of the digitalRead or does something happen to the result during the transfer to the cloud. Any suggestions to get around the “non-boolean” result will be greatly appreciated.

Here is the code
int pin = D0;

void setup() {
pinMode(pin,INPUT);
}

void loop() {


Particle.variable("status",digitalRead(pin));
delay(2000);
}

This is not how a variable is set up.


int pin = D0;
int value = 0;

void setup() {
  pinMode(pin, INPUT_PULLDOWN);
  Particle.variable("status", value);
}

void loop() {
  value = digitalRead(pin);
  delay(2000);
}

Your code contains two major mistakes:

  1. Particle.variable() is only called once in setup() to expose a variable for further requests
  2. As the name says you expose a (global) variable and not a function result.
  • the former has a definitive location in RAM that can be stored and used for Particle.variable()
  • the latter keeps changing location since it’s only temporarily created on the stack

BTW

“non-boolean” may be argued about - C/C++ considers zero (false) and non-zero (true) as boolean :wink: