I'm sure this is a very basic coding mistake...

I'm picking up a reading from A0. If the value is 0, I want the value of the serial to be written to the Serial Monitor.

I also want it only to write to the Serial Monitor for the first time that the value falls to 0.

I don't understand why, when the value for SensorValue = 0, the if is not performed.

This is my code:

boolean must_print = true;
int sensorValue = 0;

// the setup routine runs once when you press reset:
void setup() {
// initialize serial communication at 9600 bits per second:

// the loop routine runs over and over again forever:
void loop() {
  // read the input on analog pin 0:
  sensorValue = analogRead(A0);

  if (sensorValue = 0) {

     if (must_print = true)
     must_print = false;
    must_print = true

   delay(500);        // delay in between reads for stability


  if (sensorValue = 0) {

That assigns zero to sensorValue, which then tests false. You want:

  if (sensorValue == 0) {
  • No problem. And in case you didn't spot it, you have the same problem with if (must_print = true). That should also be ==.
    – Nick Gammon
    Jul 6 '15 at 21:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.