Notifications
Clear all

Boolean behaviour at start up

16 Posts
5 Users
1 Likes
2,351 Views
(@billche)
Member
Joined: 2 years ago
Posts: 6
Topic starter  

Hi everyone,

This is Bill from Brisbane Australia, a new member here and a Noob to coding too.

This question is related to the video of Arduino IoT Cloud 2021, I follow the instruction and made my first experiment with ESP32 board, all works fine from computer or mobile except all subroutine code executed once at boot up. Below are the code for your easy reference.

Variables added set up at IoT Cloud

float humidity;
float temperature;
bool lDoor;
bool mDoor;
bool rDoor;

coding for one of the boolean subroutine

void onLDoorChange() {
// Add your code here to act upon LDoor change

Serial.println("LDoor signal");
digitalWrite(LeftDoor, HIGH);
Serial.println("Pin 32 high");
delay(500);
digitalWrite(LeftDoor, LOW);
Serial.println("Pin 32 low");
}

 In order to stop this from execution during boot up I did the following

int lDoorinitiated = 0;

void onLDoorChange() {
// Add your code here to act upon LDoor change
if (lDoorinitiated == 1){
Serial.println("LDoor signal");
digitalWrite(LeftDoor, HIGH);
Serial.println("Pin 32 high");
delay(500);
digitalWrite(LeftDoor, LOW);
Serial.println("Pin 32 low");
}
else if (lDoorinitiated == 0){
lDoorinitiated = 1;
}

} 

It works but I think it is clumsy. Any other way to tackle this boot up issue?

Bill


   
Quote
Sciretech
(@sciretech)
Member
Joined: 2 years ago
Posts: 6
 

How are you calling onDoorChange() in the main loop?  The odd thing here is the sub is being called at startup. Is it ibeing called n your setup?


   
ReplyQuote
(@yurkshirelad)
Member
Joined: 3 years ago
Posts: 493
 

Hi Bill, welcome from Brisbane (I'm jealous! 😀) ! Can you post your full code (if it's not too big) so we can get more context of what you're trying to do.


   
ReplyQuote
(@billche)
Member
Joined: 2 years ago
Posts: 6
Topic starter  

Hi,

 

Thanks for the quick response. Below is the full coding.

/*
Sketch generated by the Arduino IoT Cloud Thing "Test1"
 https://create.arduino.cc/cloud/things/2518de9a-67ed-494b-80fd-a71f55cc28cd 

Arduino IoT Cloud Variables description

The following variables are automatically generated and updated when changes are made to the Thing

float humidity;
float temperature;
bool lDoor;
bool mDoor;
bool rDoor;

Variables which are marked as READ/WRITE in the Cloud Thing will also have functions
which are called when their values are changed from the Dashboard.
These functions are generated with the Thing and added at the end of this sketch.
*/

#include "thingProperties.h"

#include "DHT.h"
#include "DHT_U.h"

// DHT-11
#define DHTPIN 4 // DHT-11 Output Pin connection
#define DHTTYPE DHT11 // DHT Type is DHT 11

// Control Pins set up
int LeftDoor = 32;
int MiddleDoor = 33;
int RightDoor = 27;
int rDoorinitiated = 0;
int mDoorinitiated = 0;
int lDoorinitiated = 0;

// Setup DHT sensor
DHT dht(DHTPIN, DHTTYPE);

void setup() {

dht.begin();
delay(2000);

Serial.begin(9600);
delay(2000);

// Defined in thingProperties.h
initProperties();

pinMode(LeftDoor, OUTPUT);
pinMode(MiddleDoor, OUTPUT);
pinMode(RightDoor, OUTPUT);

// Connect to Arduino IoT Cloud
ArduinoCloud.begin(ArduinoIoTPreferredConnection);

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

void loop() {
ArduinoCloud.update();
// Your code here
delay(2000);
humidity= dht.readHumidity();
Serial.print("Humidity ");
Serial.println(humidity);
temperature=dht.readTemperature();
Serial.print("Temperature ");
Serial.println(temperature);

}

/*
Since LDoor is READ_WRITE variable, onLDoorChange() is
executed every time a new value is received from IoT Cloud.
*/
void onLDoorChange() {
// Add your code here to act upon LDoor change
if (lDoorinitiated == 1){
Serial.println("LDoor signal");
digitalWrite(LeftDoor, HIGH);
Serial.println("Pin 32 high");
delay(500);
digitalWrite(LeftDoor, LOW);
Serial.println("Pin 32 low");
}
else if (lDoorinitiated == 0){
lDoorinitiated = 1;
}

}

/*
Since MDoor is READ_WRITE variable, onMDoorChange() is
executed every time a new value is received from IoT Cloud.
*/
void onMDoorChange() {
// Add your code here to act upon MDoor change
if (mDoorinitiated == 1){
Serial.println("MDoor signal");
digitalWrite(MiddleDoor, HIGH);
Serial.println("Pin 33 high");
delay(500);
digitalWrite(MiddleDoor, LOW);
Serial.println("Pin 33 low");
}
else if (mDoorinitiated == 0){
mDoorinitiated = 1;
}


}

/*
Since RDoor is READ_WRITE variable, onRDoorChange() is
executed every time a new value is received from IoT Cloud.
*/
void onRDoorChange() {
// Add your code here to act upon RDoor change
if (rDoorinitiated == 1){
Serial.println("RDoor signal");
digitalWrite(RightDoor, HIGH);
Serial.println("Pin 27 high");
delay(500);
digitalWrite(RightDoor, LOW);
Serial.println("Pin 27 low");
}
else if (rDoorinitiated == 0){
rDoorinitiated = 1
}

}

   
ReplyQuote
(@yurkshirelad)
Member
Joined: 3 years ago
Posts: 493
 

Hmm, I don't know enough about how the Arduino IoT cloud works to fully understand the code. For example, I don't yet know how your onRDoorChange function is invoked by the cloud, so I'll have to defer any comments until I've done some more reading. Sorry. 🙁

A tiny comment - set a default value for booleans when you instantiate them.

bool lDoor = false;

   
ReplyQuote
(@davee)
Member
Joined: 3 years ago
Posts: 1671
 

Hi @billche,

  Apologies, I haven't looked IoT Cloud either, but I can't see what you are trying to achieve by your xDoorInitiated code ... it just seems to say, first time this function called, do nothing, except change the variable value, that does nothing else. 

Have I missed something obvious, or is there some hidden side effect?

Also, a minor point:

if a variable can only have two values (say 0 or 1, or true and false), then you don't need to check it again after the 'else',

i.e. change

else if (rDoorinitiated == 0){

to just

  else {

 

Best wishes.


   
ReplyQuote
Sciretech
(@sciretech)
Member
Joined: 2 years ago
Posts: 6
 

I'm also not familiar with the Iot Cloud thing based approach, gut if I was debugging this I would check this comment:

 

Variables which are marked as READ/WRITE in the Cloud Thing will also have functions
which are called when their values are changed from the Dashboard.

 

To prevent the sub being called for the first time, afer you able to set the Door bools to false in the Dashboard?


   
ReplyQuote
(@yurkshirelad)
Member
Joined: 3 years ago
Posts: 493
 

If your callback function onRDoorChange is expecting to be called when the variable in the Cloud is changed, you first need tocall addProperty. This tells the cloud to notify your microcontroller when it changes, otherwise nothing will happen. I'll re-read your posts to make sure I understand your question properly, but is my first thought. This is just example code from the Arduino site.

ArduinoCloud.addProperty(Variable_Name_01, READWRITE, ON_CHANGE, onVariableName01Change);
This post was modified 2 years ago by YurkshireLad

   
ReplyQuote
(@yurkshirelad)
Member
Joined: 3 years ago
Posts: 493
 

Apologies, I just realized you might be calling addProperty in initProperties.


   
ReplyQuote
(@billche)
Member
Joined: 2 years ago
Posts: 6
Topic starter  

Hi @davee,

Yes, the Doorinitiated variable is to ignore the first time execution of the subroutine at boot up but I really don't like it and believed it is unnecessary. I may missed something.

Suggestion on the "else if" good and will change it accordingly.

Hi @sciretech,

I tried to set the boolean to 0 but not work.

Hi @yurkehirelad,

The addProperty in initProperties was automatically included by the IDE as below:

void initProperties(){

ArduinoCloud.setBoardId(DEVICE_LOGIN_NAME);
ArduinoCloud.setSecretDeviceKey(DEVICE_KEY);
ArduinoCloud.setThingId(THING_ID);
ArduinoCloud.addProperty(humidity, READ, ON_CHANGE, NULL);
ArduinoCloud.addProperty(temperature, READ, ON_CHANGE, NULL);
ArduinoCloud.addProperty(lDoor, READWRITE, ON_CHANGE, onLDoorChange);
ArduinoCloud.addProperty(mDoor, READWRITE, ON_CHANGE, onMDoorChange);
ArduinoCloud.addProperty(rDoor, READWRITE, ON_CHANGE, onRDoorChange);

}

Appreciate for any further advise.

Bill


   
ReplyQuote
(@yurkshirelad)
Member
Joined: 3 years ago
Posts: 493
 

Bill, are you saying that onLDoorChange is called at startup?


   
ReplyQuote
(@billche)
Member
Joined: 2 years ago
Posts: 6
Topic starter  
Posted by: @yurkshirelad

Bill, are you saying that onLDoorChange is called at startup?

No, you read all the code. The subroutine didn't call at start up.


   
ReplyQuote
(@davee)
Member
Joined: 3 years ago
Posts: 1671
 

Hi @billche,

Yes, the Doorinitiated variable is to ignore the first time execution of the subroutine at boot up but I really don't like it and believed it is unnecessary. I may missed something.

If you can't see a reason for ignoring the first time execution, then I would recommend doing a trial with it removed (and maybe checking any documentation or other reason that persuaded you to put it in first)... you can always restore it if it is needed. This way, you will either get a better understanding of your program or a neater and more focussed program, either of which I would score as a win.

If it turns out you do need to ignore the first call, then that is something to explain in a comment ... imagine you have to revisit the code in a year or ten ... would you remember why it is there?

Writing a program is similar to writing text generally. You start by putting a rough 'story' together, then 'tune and prune' it to get the best result. 

Best wishes, Dave


   
ReplyQuote
 Biny
(@binaryrhyme)
Member
Joined: 2 years ago
Posts: 269
 

You could consider making a Door class, with an Initialize() method, and then anything else you want to do..., like CheckStatus(),  Lock(), unLock(), and yes, OnDoorChange(). Then you would just call the Initialize() method on startup, bind any events, etc. You're new to coding, so should you embark on this path, I will apologize in advance for throwing you in the deep end, but you'll eventually thank me. 😉 A best practice is to use smaller functions that do what they say, and not overload them with a lot of different behaviour if you can avoid it.

I googled down this tutorial, but there are many others. I'm assuming the class construct will compile fine for the ESP32.

Programming With Classes and Objects on the Arduino - Circuit Basics

I edit my posts to fix typos, correct grammar, or improve clarity. On-screen keyboards are evil.


   
ReplyQuote
(@billche)
Member
Joined: 2 years ago
Posts: 6
Topic starter  
Posted by: @davee

Hi @billche,

Yes, the Doorinitiated variable is to ignore the first time execution of the subroutine at boot up but I really don't like it and believed it is unnecessary. I may missed something.

If you can't see a reason for ignoring the first time execution, then I would recommend doing a trial with it removed (and maybe checking any documentation or other reason that persuaded you to put it in first)... you can always restore it if it is needed. This way, you will either get a better understanding of your program or a neater and more focussed program, either of which I would score as a win.

If it turns out you do need to ignore the first call, then that is something to explain in a comment ... imagine you have to revisit the code in a year or ten ... would you remember why it is there?

Writing a program is similar to writing text generally. You start by putting a rough 'story' together, then 'tune and prune' it to get the best result. 

Best wishes, Dave

Thanks Dave,

I cannot ignore the first time execution as it form part of the garage door automation. During power interruption and restoration, I cannot let it operate during this start up.

I can put a hardware delay, say 20S at start up or as per the code to ignore the first time execution but still believed I had missed something.

Bill


   
ReplyQuote
Page 1 / 2