Skip to content

Commit 28a766d

Browse files
committed
update the nerd project
1 parent 3382bf7 commit 28a766d

File tree

1 file changed

+81
-50
lines changed
  • content/hardware/09.kits/maker/iot-bundle/tutorials/the-nerd

1 file changed

+81
-50
lines changed

content/hardware/09.kits/maker/iot-bundle/tutorials/the-nerd/content.md

+81-50
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ The Nerd is a desktop electronic pet that survives by eating and some sunlight.
2323

2424
### In a Nutshell
2525

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.
2727

2828
### Components
2929

@@ -81,12 +81,10 @@ Creating a new thing and dashboard is easy. First, go to the Arduino Cloud site
8181

8282
We will start by adding three variables:
8383

84-
- `nerdsFood` - `INT` - `READ & WRITE`
84+
- `nerdsFood` - `INT` - `READ ONLY`
8585

8686
- `nerdsLight` - `INT` - `READ ONLY`
8787

88-
- `nerdsTime` - `TIME` - `READ & WRITE`
89-
9088
### Dashboard
9189

9290
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{
115113
}
116114
```
117115

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;
119124
125+
// call the eating function
126+
eating();
127+
}
120128
```
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+
}
127136
```
128137

129138
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.
130139

140+
```arduino
141+
if(nerdsFood == 0 && justWokeUp == false && !sosTriggered){
142+
// DIE :(
143+
SOS();
144+
sosTriggered = true;
145+
}
146+
```
147+
131148
**Checking the light level**
132149

133150
To check so that our Nerd gets enough sunlight we will use a Phototransistor. Keeping track of the light level with the **nerdsLight** Cloud variable.
@@ -140,7 +157,7 @@ nerdsLight = analogRead(sensorPin);
140157
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:
141158

142159
```
143-
if(nerdsFood < 12 && nerdsLight>150)
160+
if(nerdsFood < 12 && nerdsLight > 300)
144161
```
145162

146163
**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
161178
***Note: For the code to work you also need `thingProperties.h` which is automatically generated when creating a Cloud sketch.***
162179

163180
```arduino
164-
#include <SPI.h>
165181
#include "thingProperties.h"
166182
167183
// RGB LED pins
@@ -173,57 +189,62 @@ int buzzerPin = 9;
173189
int sensorPin = A2;
174190
int buttonPin = 2; // the number of the pushbutton pin
175191
176-
bool hungry=true;
177-
bool justWokeUp=true;
192+
bool justWokeUp = true;
193+
bool sosTriggered = false;
178194
int buttonState = 0;
179195
180-
void setup() {
181-
182-
Serial.begin(115200);
183-
delay(2000);
196+
unsigned long previousMillis = 0;
197+
const long interval = 10000;
184198
185-
// Defined in thingProperties.h
199+
void setup() {
200+
Serial.begin(9600);
201+
delay(1500);
186202
initProperties();
187-
188-
// Connect to Arduino IoT Cloud
189203
ArduinoCloud.begin(ArduinoIoTPreferredConnection);
204+
setDebugMessageLevel(2);
205+
ArduinoCloud.printDebugInfo();
206+
190207
pinMode(redPin, OUTPUT);
191208
pinMode(greenPin, OUTPUT);
192209
pinMode(bluePin, OUTPUT);
193210
pinMode(buttonPin, INPUT);
211+
212+
nerdsFood = 5;
194213
}
195214
196215
void loop() {
216+
ArduinoCloud.update();
197217
218+
unsigned long currentMillis = millis();
219+
198220
nerdsLight = analogRead(sensorPin);
199221
buttonState = digitalRead(buttonPin);
200-
ArduinoCloud.update();
201-
222+
202223
// Awaking notification
203-
if(nerdsLight>150 && justWokeUp){
204-
Serial.print("woke up");
205-
if(nerdsFood == 0){
206-
nerdsFood = 2;
207-
}
208-
justWokeUp=false;
224+
if(nerdsLight < 300 && justWokeUp){
225+
226+
//Print wake up message
227+
Serial.println("woke up");
228+
209229
setColor(0, 255, 0); // green
210-
tone(buzzerPin, 31, 200); // tone(Pin, Note, Duration);
211-
delay(200);
230+
tone(buzzerPin, 262, 1000); // tone(Pin, Note, Duration);
231+
delay(2000);
212232
setColor(0, 0, 0); // off
213233
noTone(buzzerPin);
214234
delay(1000);
235+
justWokeUp = false;
215236
}
216-
217-
if (buttonState == HIGH) {
218-
if(nerdsFood < 12 && nerdsLight>150){
237+
238+
if (buttonState == HIGH && !sosTriggered) {
239+
if(nerdsFood < 12 && nerdsLight > 300){
219240
nerdsFood++;
220241
tone(buzzerPin, 40, 300); // tone(Pin, Note, Duration);
221242
delay(100);
222243
tone(buzzerPin, 40, 300); // tone(Pin, Note, Duration);
223244
delay(100);
224245
noTone(buzzerPin);
225246
}
226-
delay(500);
247+
delay(100);
227248
}
228249
229250
// Set color status feedback
@@ -236,34 +257,50 @@ void loop() {
236257
else{
237258
setColor(0, 255, 0); // green
238259
}
260+
261+
if (currentMillis - previousMillis >= interval) {
262+
// save the last time you called onNerdsTimeChange
263+
previousMillis = currentMillis;
264+
265+
// call the hungry function
266+
eating();
267+
}
268+
269+
if(nerdsFood == 0 && justWokeUp == false && !sosTriggered){
270+
// DIE :(
271+
SOS();
272+
sosTriggered = true;
273+
}
274+
239275
}
240276
277+
241278
void SOS(){
242-
for(int a = 0; a< 3; a++){
279+
for(int a = 0; a < 3; a++){
243280
setColor(255, 0, 0); // Red
244-
tone(buzzerPin, 31, 100); // tone(Pin, Note, Duration);
281+
tone(buzzerPin, 262, 4); // tone(Pin, Note, Duration);
245282
delay(100);
246283
setColor(0, 0, 0); // off
247284
noTone(buzzerPin);
248285
delay(50);
249286
}
250287
251288
delay(1000);
252-
for(int a = 0; a< 3; a++){
289+
for(int a = 0; a < 3; a++){
253290
setColor(255, 0, 0); // Red
254-
tone(buzzerPin, 31, 2000); // tone(Pin, Note, Duration);
291+
tone(buzzerPin, 262, 2000); // tone(Pin, Note, Duration);
255292
delay(1000);
256293
}
257294
258-
for(int a = 0; a< 3; a++){
295+
for(int a = 0; a < 3; a++){
259296
setColor(255, 0, 0); // Red
260-
tone(buzzerPin, 31, 100); // tone(Pin, Note, Duration);
297+
tone(buzzerPin, 262, 100); // tone(Pin, Note, Duration);
261298
delay(100);
262299
setColor(0, 0, 0); // off
263300
noTone(buzzerPin);
264301
delay(50);
265302
}
266-
delay(10000);
303+
delay(1000);
267304
}
268305
269306
// Send RGB values to the LED pins
@@ -273,18 +310,12 @@ void setColor(int red, int green, int blue){
273310
analogWrite(bluePin, blue);
274311
}
275312
276-
void onNerdsTimeChange(){
313+
void eating(){
277314
if(nerdsFood > 0){
278315
nerdsFood--;
279316
delay(200);
280317
}
281318
}
282319
283-
void onNerdsFoodChange(){
284-
if(nerdsFood == 0 && justWokeUp==false){
285-
// DIE :(
286-
SOS();
287-
}
288-
}
289320
290321
```

0 commit comments

Comments
 (0)