diff --git a/content/hardware/09.kits/maker/iot-bundle/tutorials/puzzlebox/assets/screenshot_2022-11-23_141306_tsMoI77zJw.png b/content/hardware/09.kits/maker/iot-bundle/tutorials/puzzlebox/assets/screenshot_2022-11-23_141306_tsMoI77zJw.png
deleted file mode 100644
index 8b72f9a21e..0000000000
Binary files a/content/hardware/09.kits/maker/iot-bundle/tutorials/puzzlebox/assets/screenshot_2022-11-23_141306_tsMoI77zJw.png and /dev/null differ
diff --git a/content/hardware/09.kits/maker/iot-bundle/tutorials/puzzlebox/content.md b/content/hardware/09.kits/maker/iot-bundle/tutorials/puzzlebox/content.md
index 3ee4235444..1aa8891cb5 100644
--- a/content/hardware/09.kits/maker/iot-bundle/tutorials/puzzlebox/content.md
+++ b/content/hardware/09.kits/maker/iot-bundle/tutorials/puzzlebox/content.md
@@ -22,11 +22,11 @@ source: "https://create.arduino.cc/projecthub/Arduino_Genuino/puzzlebox-with-ard
**Create a Puzzle Box with the help of the Arduino Cloud!**
-Keeping your valuable items away from prying eyes can be hard sometimes, unless you put it in a big safe or something similar... but who has room for that? Instead, create your own puzzle box using the components from the IoT Bundle and some cardboard! We can't guarantee the safety of your belongings, but at least it will be a fun deterrent for potential thieves. Of course, we advise you to stash your candy in there... not actual valuables.
+Keeping your valuable items away from prying eyes can be hard sometimes unless you put them in a big safe or something similar... but who has room for that? Instead, create your own puzzle box using the components from the IoT Bundle and some cardboard! We can't guarantee the safety of your belongings, but at least it will be a fun deterrent for potential thieves. Of course, we advise you to stash your candy in there... not actual valuables.
### In a Nutshell
-In order to open the box, which is held closed with a servo motor, you will have to turn the potentiometers until you get the right combination. The combination can be set through the dashboard on the Arduino Cloud. An LED will help you guess, giving you colour feedbacks: the closer you are, the warmer the color. When the right combination is guessed, the box will open and a victorious melody will start playing, revealing whatever you have locked inside. In order to create our puzzle box we will need the following components:
+In order to open the box, which is held closed with a servo motor, you will have to turn the potentiometers until you get the right combination. The combination can be set through the dashboard on the Arduino Cloud. An LED will help you guess, giving you color feedback: the closer you are, the warmer the color. When the right combination is guessed, the box will open and a victorious melody will start playing, revealing whatever you have locked inside. To create our puzzle box we will need the following components:
* Buzzer
* RGB LED
@@ -76,7 +76,13 @@ We will start by setting up the Arduino Cloud by following the steps below:
We will start by adding four variables:
-
+- `display` - `String` - `READ & WRITE`
+
+- `sliderOne` - `INT` - `READ & WRITE`
+
+- `sliderThree` - `TIME` - `READ & WRITE`
+
+- `sliderTwo` - `INT` - `READ & WRITE`
### Dashboard
@@ -376,7 +382,7 @@ void onDisplayChange() {
**RGB LED**
-We will use the RGB LED as a feedback to help people guessing the combination, the closer they get to the right value the warmer the colour of the LED, spanning from blue, aqua, yellow and red.
+We will use the RGB LED as feedback to help people guess the combination, the closer they get to the right value the warmer the color of the LED, spanning from blue, aqua, yellow and red.

@@ -499,7 +505,7 @@ void onDisplayChange() {
### Connect It to the Arduino Cloud
-Now we are starting to put things together: we have connected the potentiometers to unlock the box, created two dashboards for setting the combination and seeing your current position, as well as installed a RGB which will tell you how close you are to the right combination.
+Now we are starting to put things together: we have connected the potentiometers to unlock the box, created two dashboards for setting the combination and seeing your current position, as well as installed an RGB which will tell you how close you are to the right combination.
- Note that we will use the function `giveColorFeedback()` to set the color of the RGB LED when the absolute value of each potentiometer is closer than a certain threshold to the correct combination.
@@ -509,7 +515,7 @@ void giveColorFeedback(int PotOne, int PotTwo, int PotThree){...}
- Note that the initial value is set to 1, it will change only if you modify the values of the sliders on the Cloud dashboard. If you reset the board the combination will be back to the default value.
-A boolean variable **bool start = true;** is used to detect when the combination has already been guessed, so to avoid reopening the the box at every loop.
+A boolean variable **bool start = true;** is used to detect when the combination has already been guessed, to avoid reopening the the box at every loop.
Upload this example sketch and turn the potentiometers to see it in action.
@@ -661,7 +667,7 @@ We will use the buzzer to play a melody when the box is opened. Connect the buzz

-Now, navigate into the Arduino Web Editor through **Thing > Sketch tab > open full editor**. This will open up our automatically generated sketch in the full Arduino Web Editor. Next we need to add an extra tab containing all the necessary notes to play the song. Click the arrow on the right side to add a new tab called `"Melody.h"` and add the code below.
+Now, navigate into the Arduino Web Editor through **Thing > Sketch tab > open full editor**. This will open up our automatically generated sketch in the full Arduino Web Editor. Next we need to add an extra tab containing all the necessary notes to play the song. Click the arrow on the right side to add a new tab called `"melody.h"` and add the code below.
```
#define NOTE_B0 31
@@ -778,13 +784,13 @@ void playMelody() {
}
```
-Now, we need to go back to our main sketch to play the melody. In order to use the notes we need to add " **#include "Melody.h"** " at the top of our code and inside **setup()** we need to add our **buzzerPin** as **Output.** To test our melody we can call **playMelody()** inside **loop()** if the combination has been set right.
+Now, we need to go back to our main sketch to play the melody. In order to use the notes we need to add " **#include "melody.h"** " at the top of our code and inside **setup()** we need to add our **buzzerPin** as **Output.** To test our melody we can call **playMelody()** inside **loop()** if the combination has been set right.
```
#include "LiquidCrystal.h"
#include "SPI.h"
#include "thingProperties.h"
-#include "Melody.h"
+#include "melody.h"
/* RGB LED pins */
int redPin = 6;
int greenPin = 8;
@@ -941,19 +947,19 @@ void onDisplayChange() {
> **Note:** for the servo motor you will need a 9V battery which is not included in the IoT Bundle! Alternatively you can use another external power supply such as a phone charger with open ended cables.
-The servo motor is the lock of our box, we will need it to turn 90 degrees when the combination is correct, so that the box will open. Connecting the servo only requires three wires though you will need a 9V battery to power the servo motor. Connect **9V**, **GND** and **pin9** as shown below.
+The servo motor is the lock of our box, we will need it to turn 90 degrees when the combination is correct so that the box will open. Connecting the servo only requires three wires though you will need a 9V battery to power the servo motor. Connect **9V**, **GND** and **pin9** as shown below.


-To use the servo motor we need to `#include "Servo.h"` at the top of our code as well as set starting position to 0 and include the `Servo myservo` object. Now we can create two functions, one for opening the box and one for closing it.
+To use the servo motor we need to `#include "Servo.h"` at the top of our code as well as set the starting position to 0 and include the `Servo myservo` object. Now we can create two functions, one for opening the box and one for closing it.
```arduino
#include "LiquidCrystal.h"
#include "SPI.h"
#include "thingProperties.h"
-#include "Melody.h"
+#include "melody.h"
#include "Servo.h"
int pos = 0;
Servo myservo;
@@ -1110,7 +1116,7 @@ void onDisplayChange() {
}
```
-Note that in order to turn the servo back and close the box all you'll have to do is to turn all potentiometer to 0.
+Note that in order to turn the servo back and close the box all you'll have to do is to turn all potentiometers to 0.
### Build Your Puzzle Box
@@ -1127,4 +1133,184 @@ This tutorial is part of a series of experiments that familiarize you with the A
## Full Code
-
\ No newline at end of file
+```arduino
+#include "LiquidCrystal.h"
+#include "SPI.h"
+#include "thingProperties.h"
+#include "melody.h"
+#include "Servo.h"
+
+int pos = 0;
+Servo myservo;
+
+/* RGB LED pins */
+int redPin = 6;
+int greenPin = 8;
+int bluePin = 7;
+
+/* LCD screen pins */
+const int rs = 12,
+ en = 11,
+ d4 = 2,
+ d5 = 3,
+ d6 = 4,
+ d7 = 5;
+
+bool start = true;
+
+LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
+
+void setup() {
+ pinMode(redPin, OUTPUT);
+ pinMode(greenPin, OUTPUT);
+ pinMode(bluePin, OUTPUT);
+ analogWrite(A3, 0); /* set the brightness of the LCD screen to the maximum value */
+ Serial.begin(9600);
+ delay(1500);
+ initProperties();
+ ArduinoCloud.begin(ArduinoIoTPreferredConnection{{if .Broker}}, false, "{{.Broker}}"{{end}});
+ setDebugMessageLevel(2);
+ ArduinoCloud.printDebugInfo();
+ lcd.begin(16, 2); /* begin LCD screen with 16 columns and 2 rows */
+
+myservo.attach(9);
+myservo.write(pos);
+}
+
+void loop() {
+ ArduinoCloud.update();
+ int PotOne = map(analogRead(A0), 0, 1023, 0, 9);
+ int PotTwo = map(analogRead(A1), 0, 1023, 0, 9);
+ int PotThree = map(analogRead(A2), 0, 1023, 0, 9);
+ lcd.setCursor(0, 0);
+ lcd.print(PotOne);
+ lcd.setCursor(2, 0);
+ lcd.print(PotTwo);
+ lcd.setCursor(4, 0);
+ lcd.print(PotThree);
+
+ String numberOne = String(PotOne);
+ String numberTwo = String(PotTwo);
+ String numberThree = String(PotThree);
+
+ display =String(numberOne + numberTwo + numberThree);
+
+ if (start) {
+ giveColorFeedback(PotOne, PotTwo, PotThree);
+ if (PotOne == sliderOne && PotTwo == sliderTwo && PotThree == sliderThree)
+ {
+ blinkGreenLed();
+ start = false;
+ open_the_box();
+ playMelody();
+ }
+ }
+ if (!start) {
+ if (PotOne == 0 && PotTwo == 0 && PotThree == 0) {
+ start = true;
+ close_the_box();
+ }
+ }
+}
+
+/* Give feedback based on how close the potentiometer are to the combination value
+The more it's close the warmer is the color of the LED */
+void giveColorFeedback(int PotOne, int PotTwo, int PotThree) {
+ if (abs(PotOne - sliderOne) <= 1 && abs(PotTwo - sliderTwo) <= 1 && abs(PotThree - sliderThree) <= 1 ) {
+ /* Red */
+ setColor(255, 0, 0);
+ }
+ else if (abs(PotOne - sliderOne) <= 3 && abs(PotTwo - sliderTwo) <= 3 && abs(PotThree - sliderThree) <= 3 ) {
+ /* yellow */
+ setColor(255, 255, 0);
+ }
+ else if (abs(PotOne - sliderOne) <= 4 && abs(PotTwo - sliderTwo) <= 4 && abs(PotThree - sliderThree) <= 4 ) {
+ /* aqua */
+ setColor(0, 255, 255);
+ }
+ else {
+ /* blue */
+ setColor(0, 0, 255);
+ }
+}
+
+void blinkGreenLed() {
+ for (int a = 0; a < 2; a++) {
+ for (int b = 0; b <= 255; b += 5) {
+ setColor(0, b, 0);
+ delay(5);
+ }
+ for (int b = 255; b >= 0; b -= 5) {
+ setColor(0, b, 0);
+ delay(5);
+ }
+ }
+ for (int b = 0; b <= 255; b += 5) {
+ setColor(0, b, 0);
+ delay(5);
+ }
+}
+
+void open_the_box() {
+ for (pos = 0; pos <= 90; pos += 1) { /* goes from 0 degrees to 90 degrees */
+ myservo.write(pos); /* tell servo to go to position in variable 'pos' */
+ delay(15); /* waits 15ms for the servo to reach the position */
+ }
+}
+
+void close_the_box() {
+ for (pos = 90; pos >= 0; pos -= 1) { /* goes from 90 degrees to 0 degrees */
+ myservo.write(pos); /* tell servo to go to position in variable 'pos' */
+ delay(15); /* waits 15ms for the servo to reach the position */
+ }
+}
+
+void initLCD() {
+ Serial.println(">>> begin LCD");
+ lcd.begin(16, 2);
+ lcd.print(" Initialising");
+
+ delay(100);
+}
+
+void onSliderOneChange() {
+ /* Add your code here to act upon SliderValue1 change */
+ Serial.print("New combination: ");
+ Serial.print(sliderOne);
+ Serial.print(" ");
+ Serial.print(sliderTwo);
+ Serial.print(" ");
+ Serial.println(sliderThree);
+}
+
+void onSliderTwoChange() {
+ /* Add your code here to act upon SliderValue2 change */
+ Serial.print("New combination: ");
+ Serial.print(sliderOne);
+ Serial.print(" ");
+ Serial.print(sliderTwo);
+ Serial.print(" ");
+ Serial.println(sliderThree);
+}
+
+void onSliderThreeChange() {
+ /* Add your code here to act upon SliderValue3 change */
+ Serial.print("New combination: ");
+ Serial.print(sliderOne);
+ Serial.print(" ");
+ Serial.print(sliderTwo);
+ Serial.print(" ");
+ Serial.println(sliderThree);
+}
+
+/* Send RGB values to the LED pins */
+void setColor(int red, int green, int blue) {
+ analogWrite(redPin, red);
+ analogWrite(greenPin, green);
+ analogWrite(bluePin, blue);
+}
+
+void onDisplayChange() {
+/* Add your code here to act upon Display change */
+}
+```
\ No newline at end of file
diff --git a/content/hardware/09.kits/maker/iot-bundle/tutorials/the-nerd/assets/circuit.png b/content/hardware/09.kits/maker/iot-bundle/tutorials/the-nerd/assets/circuit.png
new file mode 100644
index 0000000000..ef880aa6eb
Binary files /dev/null and b/content/hardware/09.kits/maker/iot-bundle/tutorials/the-nerd/assets/circuit.png differ
diff --git a/content/hardware/09.kits/maker/iot-bundle/tutorials/the-nerd/assets/screenshot_2022-11-18_175524_4XRWc1dHYc.png b/content/hardware/09.kits/maker/iot-bundle/tutorials/the-nerd/assets/screenshot_2022-11-18_175524_4XRWc1dHYc.png
deleted file mode 100644
index 492baba52c..0000000000
Binary files a/content/hardware/09.kits/maker/iot-bundle/tutorials/the-nerd/assets/screenshot_2022-11-18_175524_4XRWc1dHYc.png and /dev/null differ
diff --git a/content/hardware/09.kits/maker/iot-bundle/tutorials/the-nerd/assets/the_nerd_rp2040_dqr7egmpao_ESpsLy8iaq.png b/content/hardware/09.kits/maker/iot-bundle/tutorials/the-nerd/assets/the_nerd_rp2040_dqr7egmpao_ESpsLy8iaq.png
deleted file mode 100644
index cc6786368b..0000000000
Binary files a/content/hardware/09.kits/maker/iot-bundle/tutorials/the-nerd/assets/the_nerd_rp2040_dqr7egmpao_ESpsLy8iaq.png and /dev/null differ
diff --git a/content/hardware/09.kits/maker/iot-bundle/tutorials/the-nerd/content.md b/content/hardware/09.kits/maker/iot-bundle/tutorials/the-nerd/content.md
index 14effa73d4..525a090f9c 100644
--- a/content/hardware/09.kits/maker/iot-bundle/tutorials/the-nerd/content.md
+++ b/content/hardware/09.kits/maker/iot-bundle/tutorials/the-nerd/content.md
@@ -19,11 +19,11 @@ source: "https://create.arduino.cc/projecthub/Arduino_Genuino/the-nerd-with-ardu
**Create a desktop pet with the help of the Arduino Cloud!**
-The Nerd is a desktop electronic pet that survives by eating and some sunlight. In order for it to thrive, you must feed it periodically and expose it to sunlight. If it is running out of food, it will communicate an SOS in Morse code using its built-in piezo speaker.
+The Nerd is a desktop electronic pet that survives by eating and some sunlight. For it to thrive, you must feed it periodically and expose it to sunlight. If it is running out of food, it will communicate an SOS in Morse code using its built-in piezo speaker.
### In a Nutshell
-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.
+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.
### Components
@@ -31,7 +31,7 @@ The Nerd will need food which you can give it by pressing its button. Otherwise
* Phototransistor
* Buzzer
* Push button
-* 220 Ohm resistor
+* 1M Ohm resistor
* 10K Ohm resistor
### Learning Goals
@@ -52,9 +52,11 @@ This tutorial is part of a series of experiments that familiarise you with the A
### Circuit
-In this project, we will be using the following circuit. In it we have a 220 ohm resistor connected between ground and the A2 pin used for the phototransistor. And a 10k ohm resistor connected between between ground and the push button.
+In this project, we will be using the following circuit. In it, we have a 1M Ohm resistor connected between the ground and the A2 pin used for the phototransistor. And a 10k Ohm resistor connected between between ground and the push button.
-
+
+
+***Make sure to place the phototransistor in reverse polarity, meaning the long leg is connected to A2 and the short leg is connected to 3.3 V.***
### Setting up the Arduino Cloud
@@ -68,7 +70,7 @@ See the image below to understand how to set it up.

-Creating a new thing and dashboard is really easy. First go to the Arduino Cloud site [here](https://create.arduino.cc/iot). Setting up the Cloud consists of the following parts:
+Creating a new thing and dashboard is easy. First, go to the Arduino Cloud site [here](https://create.arduino.cc/iot). Setting up the Cloud consists of the following parts:
* Creating a **Thing**
* Attaching a **Device**
@@ -81,7 +83,9 @@ Creating a new thing and dashboard is really easy. First go to the Arduino Cloud
We will start by adding three variables:
-
+- `nerdsFood` - `INT` - `READ ONLY`
+
+- `nerdsLight` - `INT` - `READ ONLY`
### Dashboard
@@ -94,9 +98,9 @@ The next step to deploying our project is adding a control panel using the Ardui
### Setup Hardware & Sketch
-**Keeping track of the Nerds food**
+**Keeping track of the Nerd's food**
-To keep track of the Nerds food we will be using an **int** variable. When the Nerd is in enough sunlight and the button is pressed it will be fed. Making a sound so you know that it received the food. The RGB led will change color depending on the Nerds hunger state.
+To keep track of the Nerd's food we will be using an **int** variable. When the Nerd is in enough sunlight and the button is pressed it will be fed. Making a sound so you know that it received the food. The RGB LED will change color depending on the Nerd's hunger state.
```
/* Set color status feedback */
@@ -111,37 +115,56 @@ else{
}
```
-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 forgot to update the tracker on the dashboard as well so you can accurately track the food that the Nerd has.
+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).
+
+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.
+
+```arduino
+ if (currentMillis - previousMillis >= interval) {
+ // save the last time you called onNerdsTimeChange
+ previousMillis = currentMillis;
+ // call the eating function
+ eating();
+ }
```
-void onNerdsFoodChange(){
- if(nerdsFood == 0 && justWokeUp==false){
- /* DIE :( */
- SOS();
- }
-}
+
+```arduino
+void eating() {
+ if(nerdsFood > 0){
+ nerdsFood--;
+ delay(200);
+ }
```
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.
+```arduino
+ if(nerdsFood == 0 && justWokeUp == false && !sosTriggered){
+ // DIE :(
+ SOS();
+ sosTriggered = true;
+ }
+```
+
**Checking the light level**
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.
```
-int SensorPin = A2;
-nerdsLight = analogRead(SensorPin);
+int sensorPin = A2;
+nerdsLight = analogRead(sensorPin);
```
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:
```
-if(nerdsFood < 12 && nerdsLight>150)
+if(nerdsFood < 12 && nerdsLight > 300)
```
**Time tracker with the Arduino Cloud**
-The Nerd will get hungry every 10 minutes and eat the food it has been given. To keep track of when the Nerd gets hungry we will use a time variable from the Arduino Cloud. We will use the auto generated functions we get from the Arduino Cloud to make the changes to the Nerds food when it eats. This function will be executed after a amount of time has passed. The time is determined in the nerdsTime variable configuration. In this example we set the time to be 10 minutes, this has to be stated in seconds.
+The Nerd will get hungry every 10 minutes and eat the food it has been given. To keep track of when the Nerd gets hungry we will use a time variable from the Arduino Cloud. We will use the auto-generated functions we get from the Arduino Cloud to make the changes to the Nerd's food when it eats. This function will be executed after an amount of time has passed. The time is determined in the nerdsTime variable configuration. In this example we set the time to be 10 minutes, this has to be stated in seconds.
### Want to Know More?
@@ -154,4 +177,147 @@ This tutorial is part of a series of experiments that familiarize you with the A
## Full Code
-
\ No newline at end of file
+***Note: For the code to work you also need `thingProperties.h` which is automatically generated when creating a Cloud sketch.***
+
+```arduino
+#include "thingProperties.h"
+
+// RGB LED pins
+int redPin = 6;
+int greenPin = 8;
+int bluePin = 7;
+
+int buzzerPin = 9;
+int sensorPin = A2;
+int buttonPin = 2; // the number of the pushbutton pin
+
+bool justWokeUp = true;
+bool sosTriggered = false;
+int buttonState = 0;
+
+unsigned long previousMillis = 0;
+const long interval = 600000; // 10 minutes interval in milliseconds
+
+void setup() {
+ Serial.begin(9600);
+ delay(1500);
+ initProperties();
+ ArduinoCloud.begin(ArduinoIoTPreferredConnection);
+ setDebugMessageLevel(2);
+ ArduinoCloud.printDebugInfo();
+
+ pinMode(redPin, OUTPUT);
+ pinMode(greenPin, OUTPUT);
+ pinMode(bluePin, OUTPUT);
+ pinMode(buttonPin, INPUT);
+
+ nerdsFood = 5;
+}
+
+void loop() {
+ ArduinoCloud.update();
+
+ unsigned long currentMillis = millis();
+
+ nerdsLight = analogRead(sensorPin);
+ buttonState = digitalRead(buttonPin);
+
+ // Awaking notification
+ if(nerdsLight < 300 && justWokeUp){
+
+ //Print wake up message
+ Serial.println("woke up");
+
+ setColor(0, 255, 0); // green
+ tone(buzzerPin, 262, 1000); // tone(Pin, Note, Duration);
+ delay(2000);
+ setColor(0, 0, 0); // off
+ noTone(buzzerPin);
+ delay(1000);
+ justWokeUp = false;
+ }
+
+ if (buttonState == HIGH && !sosTriggered) {
+ if(nerdsFood < 12 && nerdsLight > 300){
+ nerdsFood++;
+ tone(buzzerPin, 40, 300); // tone(Pin, Note, Duration);
+ delay(100);
+ tone(buzzerPin, 40, 300); // tone(Pin, Note, Duration);
+ delay(100);
+ noTone(buzzerPin);
+ }
+ delay(100);
+ }
+
+ // Set color status feedback
+ if(nerdsFood < 4){ // if starving show red
+ setColor(255, 0, 0); // Red
+ }
+ else if(nerdsFood >= 4 && nerdsFood < 8){
+ setColor(255, 255, 0); // yellow
+ }
+ else{
+ setColor(0, 255, 0); // green
+ }
+
+ if (currentMillis - previousMillis >= interval) {
+ // save the last time you called onNerdsTimeChange
+ previousMillis = currentMillis;
+
+ // call the hungry function
+ eating();
+ }
+
+ if(nerdsFood == 0 && justWokeUp == false && !sosTriggered){
+ // DIE :(
+ SOS();
+ sosTriggered = true;
+ }
+
+}
+
+
+void SOS(){
+ for(int a = 0; a < 3; a++){
+ setColor(255, 0, 0); // Red
+ tone(buzzerPin, 262, 4); // tone(Pin, Note, Duration);
+ delay(100);
+ setColor(0, 0, 0); // off
+ noTone(buzzerPin);
+ delay(50);
+ }
+
+ delay(1000);
+ for(int a = 0; a < 3; a++){
+ setColor(255, 0, 0); // Red
+ tone(buzzerPin, 262, 2000); // tone(Pin, Note, Duration);
+ delay(1000);
+ }
+
+ for(int a = 0; a < 3; a++){
+ setColor(255, 0, 0); // Red
+ tone(buzzerPin, 262, 100); // tone(Pin, Note, Duration);
+ delay(100);
+ setColor(0, 0, 0); // off
+ noTone(buzzerPin);
+ delay(50);
+ }
+ delay(1000);
+}
+
+// Send RGB values to the LED pins
+void setColor(int red, int green, int blue){
+ analogWrite(redPin, red);
+ analogWrite(greenPin, green);
+ analogWrite(bluePin, blue);
+}
+
+void eating(){
+ if(nerdsFood > 0){
+ nerdsFood--;
+ delay(200);
+ }
+}
+
+
+```