Skip to content

Commit aae5bfe

Browse files
committed
Rewrite example in a meaningful way ... toogling the boolean switchButton property allows to switch between two coordinates and colour sets
1 parent aacce67 commit aae5bfe

File tree

2 files changed

+25
-45
lines changed

2 files changed

+25
-45
lines changed

examples/MultiValue_example/MultiValue_example.ino

+23-43
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,30 @@
1-
#include "arduino_secrets.h"
21
/*
3-
Sketch generated by the Arduino IoT Cloud Thing "Test_MultiValue"
4-
https://create-dev.arduino.cc/cloud/things/06012290-ec85-4f5c-aa00-81c0525efa0c
5-
6-
Arduino IoT Cloud Properties description
7-
8-
The following variables are automatically generated and updated when changes are made to the Thing properties
9-
10-
bool switchButton;
11-
12-
Properties which are marked as READ/WRITE in the Cloud Thing will also have functions
13-
which are called when their values are changed from the Dashboard.
14-
These functions are generated with the Thing and added at the end of this sketch.
2+
This sketch demonstrates how to use more complex cloud data types such as a colour or coordinates.
153
164
This sketch is compatible with:
17-
- MKR 1000
18-
- MKR WIFI 1010
5+
- MKR 1000
6+
- MKR WIFI 1010
7+
- MKR GSM 1400
8+
- MKR NB 1500
9+
- MKR WAN 1300/1310
10+
- ESP 8266
1911
*/
2012

13+
#include "arduino_secrets.h"
2114
#include "thingProperties.h"
2215

2316
void setup() {
24-
// Initialize serial and wait for port to open:
17+
/* Initialize serial and wait up to 5 seconds for port to open */
2518
Serial.begin(9600);
26-
// This delay gives the chance to wait for a Serial Monitor without blocking if none is found
27-
delay(1500);
19+
for(unsigned long const serialBeginTime = millis(); !Serial && (millis() - serialBeginTime > 5000); ) { }
2820

29-
// Defined in thingProperties.h
21+
/* This function takes care of connecting your sketch variables to the ArduinoIoTCloud object */
3022
initProperties();
3123

32-
// Connect to Arduino IoT Cloud
24+
/* Initialize Arduino IoT Cloud library */
3325
ArduinoCloud.begin(ArduinoIoTPreferredConnection);
3426

35-
/*
36-
The following function allows you to obtain more information
37-
related to the state of network and IoT Cloud connection and errors
38-
the higher number the more granular information you’ll get.
39-
The default is 0 (only errors).
40-
Maximum is 4
41-
*/
42-
setDebugMessageLevel(2);
27+
setDebugMessageLevel(DBG_INFO);
4328
ArduinoCloud.printDebugInfo();
4429
}
4530

@@ -51,27 +36,22 @@ float hueGreen = 80.0, satGreen = 100.0, briGreen = 100.0;
5136

5237
void loop() {
5338
ArduinoCloud.update();
54-
// Your code here
55-
56-
switchButton = !switchButton;
57-
if (switchButton) {
58-
location = Location(latMov, lonMov);
59-
color = Color(hueRed, satRed, briRed);
60-
} else {
61-
location = Location(latArd, lonArd);
62-
color = Color(hueGreen, satGreen, briGreen);
63-
}
64-
delay(5000);
6539
}
6640

67-
6841
void onSwitchButtonChange() {
69-
// Do something
70-
digitalWrite(LED_BUILTIN, switchButton);
42+
if (switchButton)
43+
{
44+
location = Location(latMov, lonMov);
45+
color = Color(hueRed, satRed, briRed);
46+
}
47+
else
48+
{
49+
location = Location(latArd, lonArd);
50+
color = Color(hueGreen, satGreen, briGreen);
51+
}
7152
}
7253

7354
void onColorChange() {
74-
// Do something
7555
Serial.print("Hue = ");
7656
Serial.println(color.getValue().hue);
7757
Serial.print("Sat = ");

examples/MultiValue_example/thingProperties.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ void initProperties() {
1818
#endif
1919
ArduinoCloud.setThingId(THING_ID);
2020
#if defined(BOARD_HAS_WIFI) || defined(BOARD_HAS_GSM) || defined(BOARD_HAS_NB)
21-
ArduinoCloud.addProperty(switchButton, READWRITE, ON_CHANGE, onSwitchButtonChange);
21+
ArduinoCloud.addProperty(switchButton, WRITE, ON_CHANGE, onSwitchButtonChange);
2222
ArduinoCloud.addProperty(location, READ, ON_CHANGE);
2323
ArduinoCloud.addProperty(color, READWRITE, ON_CHANGE, onColorChange);
2424
#elif defined(BOARD_HAS_LORA)
25-
ArduinoCloud.addProperty(switchButton, 1, READWRITE, ON_CHANGE, onSwitchButtonChange);
25+
ArduinoCloud.addProperty(switchButton, 1, WRITE, ON_CHANGE, onSwitchButtonChange);
2626
ArduinoCloud.addProperty(location, 2, READ, ON_CHANGE);
2727
ArduinoCloud.addProperty(color, 3, READWRITE, ON_CHANGE, onColorChange);
2828
#endif

0 commit comments

Comments
 (0)