Dashboard Switch will not stay ON, keeps returning to OFF position

Hi All,

I have a Thing on the IoT Cloud named Device1.

I have a Device on my Thing also named Device1.

On my Thing, i created a Variable called Pump.

A sketch was generated automatically when the variable was created.

At the bottom of the sketch, after loop(), there is a function called onPumpChange().

The whole sketch is like this:

#include "thingProperties.h"

void setup() {
  Serial.begin(9600);
  delay(1500); 
  
  pinMode(7, OUTPUT);

  initProperties();

  ArduinoCloud.begin(ArduinoIoTPreferredConnection);
  
  setDebugMessageLevel(2);
  ArduinoCloud.printDebugInfo(); }


void loop() {
  ArduinoCloud.update(); }


void onPumpChange()  {
  if(pump = true) {
    digitalWrite(7, HIGH); }
  if(pump = false) {
    digitalWrite(7, LOW);} }
        

I created a Dashboard called Greenhouse.

In this dashboard, i have a single ON/OFF switch, called Pump - the same name as the variable Pump, associate with Device1. The switch called Pump is linked to the variable called Pump.

It ran fine the very first time.

Subsequently, after some code edits, the Dashboard switch keeps turning OFF by itself. The Pin 7 digital output never triggers anymore. The relay connected does not activate.

I deleted everything and rebuilt from scratch 3 times. It never worked again - other than that very first time.

I am confounded. I can't spot my mistake.

Is there a way for me to share my code via a link to my account? I am new to this.

This kit is intended to trigger the misting pump in my greenhouse when the humidity falls below a setpoint.

I would be most grateful for any help I can get.

Ashok,

Oops

if(pump = false) {

And again

Please remember to use code tags when posting code

Yes. Thanks. I have just edited my post.

Thanks for the code tags. Do you know what theMemberformerlyknownAs is talking about? :slight_smile:

No, the code's still broken in the same way...

Ah, well I can dream, can't I? By the way, something like:

if (blah) { ... }
if (not blah) { ... }

was recognized as a common pattern over 60 years ago, and the 'else' was invented for that.

if (blah) { ... }
else { ... }

Since things can't be both true and false at the same time.

Look up '=' in the reference section!

1 Like

OMG!! Thanks so much!! It works!!
I modified the portion to:

void onPumpChange()  {
  if(pump) {
    digitalWrite(7, HIGH); }
  if(!pump) {
    digitalWrite(7, LOW);} }

I'll try to understand the problem in my original statement later. For now, your suggested correction works! I can continue! Thank you!!

If pump is not true, there's no need to test it again to see if it is false.

1 Like

That horse

() is really dead.
() has already bolted.
() is a different colour.
() was a gift.
() can reach the other side of the stream perfectly well.
(X) won't drink.
() is not a unicycle.
() is a horse, of course, of course.
() is wooden.
() is not really very high
() is behind the cart

void onPumpChange()  {
    digitalWrite(7, pump); }

(OP doesn't believe me and is trying it) :slight_smile:

1 Like

In my sketch, I see my error in my IF statement for Boolean operators.

However, i don't understand why the Switch on the Dashboard kept going OFF.

Did you look up the '=' operator as I suggested?

1 Like

Thank you. Your style is so much more elegant than my two IF statements.

You're welcome. The "textbook" preference would be for the if-else form. I just wanted to show you what is possible. You should investigate the '=' problem, you can't avoid it, it will hit you again if you do.

1 Like

@ashoka58sg, your topic has been moved to a more suitable location on the forum. Installation and Troubleshooting is not for problems with your project. See About the Installation & Troubleshooting category.

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