You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/hardware/09.kits/maker/iot-bundle/tutorials/the-nerd/content.md
+81-50
Original file line number
Diff line number
Diff line change
@@ -23,7 +23,7 @@ The Nerd is a desktop electronic pet that survives by eating and some sunlight.
23
23
24
24
### In a Nutshell
25
25
26
-
The Nerd will need food which you can give it by pressing its button. Otherwise, it will complain by making noise with the buzzer until you either feed it or put it in sunlight. The nerd will be connected to the Arduino Cloud, where we can visualize the amount of food the Nerd has and the level of light it is in. The Cloud will also handle the timing elements needed in the code. If the Nerd runs out of food, it will die dramatically, making a lot of noise.
26
+
The Nerd will need food which you can give it by pressing its button. Otherwise, it will die. The nerd will be connected to the Arduino Cloud, where we can visualize the amount of food the Nerd has and the level of light it is in. If the Nerd runs out of food, it will die dramatically, making a lot of noise.
27
27
28
28
### Components
29
29
@@ -81,12 +81,10 @@ Creating a new thing and dashboard is easy. First, go to the Arduino Cloud site
81
81
82
82
We will start by adding three variables:
83
83
84
-
-`nerdsFood` - `INT` - `READ & WRITE`
84
+
-`nerdsFood` - `INT` - `READ ONLY`
85
85
86
86
-`nerdsLight` - `INT` - `READ ONLY`
87
87
88
-
-`nerdsTime` - `TIME` - `READ & WRITE`
89
-
90
88
### Dashboard
91
89
92
90
The next step to deploying our project is adding a control panel using the Arduino IoT Dashboards. We can navigate to **Dashboards > Build Dashboard > ADD**, then we can add two widgets and link them to the variable as the following:
@@ -115,19 +113,38 @@ else{
115
113
}
116
114
```
117
115
118
-
And we can use the Arduino Cloud dashboard to keep track of the food numerically. We will also use a time variable from the Arduino Cloud to easily manage when the food count should go down. Here we will let it take 10 minutes before the food supply is decreased by one. The max food storage is set to 12, this can be expanded by changing the threshold in the "**if"** operator, and don't forget to update the tracker on the dashboard as well so you can accurately track the food that the Nerd has.
116
+
To keep track of time, we store a timestamp using `millis()` at a specific event, such as when the food supply is last decremented. Then, at regular intervals, we compare the current `millis()` value with this stored timestamp to determine if a certain amount of time has elapsed. For example, if we want to decrease the food supply every 10 minutes, we check if the difference between the current millis() value and the stored timestamp is greater than or equal to 600,000 milliseconds (10 minutes in milliseconds).
117
+
118
+
The max food storage is set to 12, this can be expanded by changing the threshold in the "**if"** operator, and don't forget to update the tracker on the dashboard as well so you can accurately track the food that the Nerd has.
119
+
120
+
```arduino
121
+
if (currentMillis - previousMillis >= interval) {
122
+
// save the last time you called onNerdsTimeChange
123
+
previousMillis = currentMillis;
119
124
125
+
// call the eating function
126
+
eating();
127
+
}
120
128
```
121
-
void onNerdsFoodChange(){
122
-
if(nerdsFood == 0 && justWokeUp==false){
123
-
/* DIE :( */
124
-
SOS();
125
-
}
126
-
}
129
+
130
+
```arduino
131
+
void eating() {
132
+
if(nerdsFood > 0){
133
+
nerdsFood--;
134
+
delay(200);
135
+
}
127
136
```
128
137
129
138
The Nerd will start with 2 food the first time it wakes up, then this value will be tracked by the Cloud. If it dies it will start over with 2 food as well.
When the Nerd first wakes up, this is when the device is started and the Nerd first receives sunlight. It will make a sound and blink its light. Then the variable will be checked every time you try to give the Nerd some food. The threshold of the light level can be changed if you are having trouble feeding the Nerd. You can use the Cloud to check what values you get when the Nerd is in the light, and then change the threshold here in the code:
141
158
142
159
```
143
-
if(nerdsFood < 12 && nerdsLight>150)
160
+
if(nerdsFood < 12 && nerdsLight > 300)
144
161
```
145
162
146
163
**Time tracker with the Arduino Cloud**
@@ -161,7 +178,6 @@ This tutorial is part of a series of experiments that familiarize you with the A
161
178
***Note: For the code to work you also need `thingProperties.h` which is automatically generated when creating a Cloud sketch.***
162
179
163
180
```arduino
164
-
#include <SPI.h>
165
181
#include "thingProperties.h"
166
182
167
183
// RGB LED pins
@@ -173,57 +189,62 @@ int buzzerPin = 9;
173
189
int sensorPin = A2;
174
190
int buttonPin = 2; // the number of the pushbutton pin
0 commit comments