Circuit Playground Express Study Buddy
by noamkimchi in Circuits > Arduino
322 Views, 1 Favorites, 0 Comments
Circuit Playground Express Study Buddy
Every student during their exams period knows that they need to sit on their asses and study as hard as possible.
The temptations are many, and the greatest temptation is the one that grabs the attention of every human being with its addictive charm - the smartphone!
Every student has one, and it's always by our side. Either flipped upside-down, in "do-not disturb" mode or even turned off - it somehow finds a way to distract us.
As students whom are disturbed by our smartphones during every exam period, we've decided to create this Circuit Playground Express - Blynk - Integromat based Study Helper which will try to prevent students from getting their attention off studying and also help students set the optimal studying environment.
The Study Helper will have several functions -
- Light detector - which will notify you via Blynk when your study environment is too dark
- Sound detector - which will notify you via Blynk when your study environment is too noisy
- Study timer - start a timer for study time and will be notified when it's over and you're free
- Break state - when you go on a break the study buddy will go on a break too
- Distance detector - which will notify you via Integromat when you try to escape and drift for more than 100m from your study zone.
- Phone lift detector - which will make every phone use during study time a nightmare.
So - are you ready??
Get all the supplements you need and prepare to get an A+ in your next exam!
***All code documentation is attached and added at the bottom of this step***.
Downloads
Supplies
- PC with the Arduino app installed - Download Arduino app
- Adafruit Circuit Playground Express board
- Cable to get the board connected to your computer
- Smartphone
- Download and install Blynk app on your smartphone
- Download and install Integromat app on your smartphone
Installations and Environment Preparations
Lets make all the preparations required for the environment to be ready for us to work with.
Screenshots attached for each of the steps below
1. Open the Arduino app on your computer and make sure a new sketch file is displayed properly.
2. Open the Blynk app on your smartphone and sign up.
Start a new project -
- Click on "New Project"
- Give it a name as you wish (Study Helper for instance)
- Choose the following device - Arduino MKR1000
- Make sure connection type is - WiFi
- Click on "Create Project"
- Now you suppose to receive an email containing an auth token for the project
3. Connect the Blynk to your Arduino sketch project (Relevant PS of the code attached)
4. Sign up to Integromat
- Click on "Create a new scenario"
- Add Android, Google Sheets and HTTP as services
- Click on "Continue"
Creating the Blynk Board Ready to Work With
Lets start by adding all the required Blynk widgets for our project -
By double clicking on the project's screen you will have a right side menu open with all the available widgets that you can add to your project.
By long press on the widget that was added to the board you can resize it and move it on the board.
Lets start with the Sound Detector widgets-
- Click on the Value Display (from the displays section in the menu) to add it to your board
- Click on the widget which was added to the board in order to configure it
- Name it as you wish ("Sound Detector" for example)
- Select a virtual pin for it to be connected to the arduino code we'll write (for example V0)
- Change the refresh interval to 1 second
- You can change the text color if you wish
- Click on the Gauge (from the displays section in the menu) to add it to your board
- Click on the widget which was added to the board in order to configure it
- Name it as you wish ("Sound Meter" for example)
- Select a virtual pin (for example V1)
- Change the refresh interval to 1 second
- You can change the text color if you wish
For the Light Detector -
- Click on the Value Display (from the displays section in the menu) to add it to your board
- Click on the widget that was added to the board in order to configure it
- Name it as you wish ("Light Detector" for example)
- Select a virtual pin (for example V2)
- Change the refresh interval for 1 second
- You can change the text color if you wish
- Click on the Gauge (from the displays section in the menu) to add it to your board
- Click on the widget which was added to the board in order to configure it
- Name it as you wish ("Light Meter" for example)
- Select a virtual pin (for example V3)
- Change the refresh interval to 1 seconds
- You can change the text color if you wish
For the Timer -
- Click on the Text Input widget (from the interfaces section in the menu) to add it to your board
- Click on the widget that was added to the board in order to configure it
- Name it as you wish ("Time To Study" for example)
- Select a virtual pin (for example V5)
- Write a hint for the student (for example "Set the time in seconds")
- Limit the characters to 5
- You can change the text color if you wish
- Click on Button (from the controllers section in the menu) to add it to your board
- Click on the widget that was added to the board in order to configure it
- Name it as you wish ("Start Learning Timer" for example)
- Select a virtual pin (for example V4)
- Change the button mode to PUSH
- You can change the button color if you wish
Add Notification widget from the notifications section in the menu
For the Phone Lift Detector -
- Click on Button (from the controllers section in the menu) to add it to your board
- Click on the widget that was added to the board in order to configure it
- Name it as you wish ("Lift Detector" for example)
- Select a virtual pin (for example V6)
- You can change the button color if you wish
- Click on the Accelerometer widget (from the smartphone sensors section in the menu)
- Click on the widget that was added to the board in order to configure it
- Name it as you wish (for example "Accelerometer")
- Select a virtual pin (for example V11)
All set! lets continue to the next step :)
Initialize() Function
We at first add an initialization function for the project to start as we wish.
This function is being called just once, in the setup() function.
The initialize() function writes to the Value Displays widgets of the Sound and Light Detectors "Get a quite/lit place to study at!" according to the relevant detector.
Also, it makes sure that all the leds of the CPX board are off.
Break Detector
We will start from implementing the break time detector because our code is working with a flag which determines if we are in a break mode or study mode. If on study mode - other detectors/features will be available.
If on break mode - no detectors/features are available.
The break detector will detect that the student is on a break if the Circuit Playground Express board will be turned upside down. When turned upside down - student is on a break. Otherwise - student is studying.
We will detect that the board is upside down by checking the value of the Z axis .
Add the attached code parts to the arduino sketch.
In the first code part we check in the loop function if the board is on break mode or study mode.
If on a break mode (meaning the CPX is turned upside down and therefore it's Z axis value is smaller than -5) - we will notify the student that he is on a break (and because it's in an infinite loop we want to notify just once, therefore we need a flag to let us know if we have already notified or not (we have two such flags - one for notifying about going on a break noteBreak and one for returning from the break noteStudy. Both were declared before in the code and are attached) and will get all the widgets on the Blynk board to not work by calling the onABreak() function (which is explained later).
If the student returned from his break (meaning he turned the Circuit Playground Express board upside down again to it's normal state so it's Z axis value is not smaller than -5), he will get notified about returning to study (therefore the noteStudy flag turns TRUE and the noteBreak flag turnes FALSE and all the widgets will get back to work by calling the studyEnv() function which will be built later in the other steps.
The other code attached of the BREAK state supposed to be written above the setup and loop functions.
In this code we declare the variables Z (for the Z axis value) to be of type float, and the noteBreak and noteStudy ones to be of type boolean and with FALSE value.
The onABreak() function is being called from the loop function mentioned above when the user turns the CPX board upside down. It writes to the Value Displays widgets of the Blynk board "Enjoy you break :)" and puts no value in the Gauges widgets.
If a notification of going on a break was not sent to the student (noteBreak == FALSE), it will send a notification of "Get a break!" and will change noteBreak = TRUE.
Study Timer
We will now turn to work on the Study Timer function.
The Study Timer is being started by the student from the Blynk board.
The student will have to input the time in seconds that he wants the timer to work on (for example, if the student wants to study for an hour he needs to input 3600 seconds) in the Time To Study text input widget on the Blynk board and to send it. After it was sent, the student needs to press the Start Learning Timer button.
When the timer starts, all the CPX board leds are colored blue. Because it has 10 leds, each time 10% of the timer time passes, one led is being turned off. When the timer finishes all the board leds are off and then a sound is being played from the CPX board along with all the leds colored purple.
Also in the Blynk the Gauge widgets show how much time is left for the timer and the Value Displays show "timer on" message.
So lets start to work!
We start with it because as mentioned before while the Study Timer is working, no other functions are working.
This is implemented in the studyEnv() function that was mentioned before, which is the main function of the project. If the timer is on, than no other functions and capabilities work. Otherwise, all other functions are on.
The timer works by getting the desired study time from the student (via the Blynk Text Input widget), and it always counts how much time is left for the timer.
In the studyEnv() function, if timerStart ==1 (this is a boolean variable which checks if the start timer button was pressed by the student in the Blynk board) and isTimer == false (meaning the timer is not running yet) we will start the timer run -
- curTime - is the counter which counts the time that has already passed. We initialize it to 0 when the timer starts.
- timerStart - as mentioned before it's the indication for the student to press the start timer button. We want to return it to be 0 (meaning the button is no more pressed)
- isTimer - as mentioned before, it let us know if the timer is running and therefore will be changed to TRUE
- For loop is necessary for the leds to be all blue at the beginning of the timer run
Then we always check if isTimer == TRUE, meaning there is still time for the timer to run. If so - we will call timerOn() function.
We first declare the required variables for the timer implementation as can be seen in the PS attached of the STUDY_TIMER. Then we start with the timerOn() function implementation:
We always check if there is still time left for the timer to run by reducing the current time that had already passed from the timer time that was inputted by the student. If there is still time, we increase the current time by 1 and always check if there are leds that need to be turned off.
If there is no time left for the timer (timreToLearn - curTime == 0) then we call the timerFinished() function because the timer is finished.
The timerFinished() function first turns all the CPX leds off, and then after a delay of half a second, it turns all the leds on with purple color. Then a tone (make sure to declare NOTE_C4 in the beginning of the code) is being played for 5 times and after all the leds are being turned off.
The last things we need to do is implement the functions that read the input from the student for the timer to run -
The first function checks if the Start Learning Timer button was pressed.
The second function gets the input timer value in seconds from the student and also calculates the ten percent value of it.
Yayyy we have finished the Timer implementation!
Lets move on to the next step :)
Sound Detector
Lets continue the studyEnv() function with the implementation of the Sound Detector.
The Sound Detector is always checking the sound level in the studying environment. If the sound level is too high to study at - the student will be notified in Blynk to go to a more quite place. Also, in the Blynk board the Gauge related to the Sound Detector will always show the sound level and the Value Display will give a relevant message according to the sound level.
Make sure to add the attached declarations in the beginning of the code.
The soundSensor variable is always checking the sound level in the study environment. This is being done by the sound sensor of the CPX board. If the sound level increases 64, at first the student will be notified and the Value Display widget will message - "This place is too load!".
Otherwise, the Value display will message - "This place is quite enough!".
Light Detector
Continue to implement the studyEnv() function, and add the attached code right after the code of the Sound Detector (in the else part of the studyEnv() function).
The Light Detector is always checking the lighting level in the studying environment. If the lighting level is too low (dark) to study at - the student will be notified in Blynk to go to a more lit place. Also, in the Blynk board the Gauge related to the Light Detector will always show the lighting level and the Value Display will give a relevant message according to the lighting level.
Make sure to add the attached declarations in the beginning of the code.
The lightSensor variable is always checking the lighting level in the study environment. This is being done by the light sensor of the CPX board. If the lighting level decreases 30, at first the student will be notified that the place is too dark and the Value Display widget will message - "This place is too dark!". Otherwise, the Value display will message - "This place is lit enough!".
Distance Detector - Integromat
Enter the new scenario you have created in Integromat and:
- Add the Android service as a trigger event, then select Watch Location Changes under GPS.
- In the pop-up window select "Add" and follow the instructions given by Integromat to connect your phone to the service.
- After connecting your Android device, click "Add another module" and select HTTP, then select Make a request.
- Go back to the email received from Blynk with the authentication code and copy it to your clipboard, then, in the pop-up window enter the following address: http://188.166.206.43//update/V6...>/update/V6?va...>/update/V6?value=1 and make sure to change the "" part to your authentication code. All other variables shouldn't be changed.
- Go to your Google account, create a new Sheet and name it "Phone location". In the first row enter the following headers: Date&Time , Latitude , Longtitude , Altitude in this order.
- Go back to the Integromat scenario and add another module, select Google Sheets, then select Add a row.
- Add a Google connection by clicking "Add" and following the instructions. Afterward, select the Google Sheet you have just created.
- In the pop-up select "Yes" for the "Table contains headers" dropdown box, and for the "Values" select the headers in the same order they were inserted into the sheet. (see 3rd screenshot)
- Now, open your Integromat app that you installed earlier and login to your account. Afterwards, go to Settings -> GPS and mark the "Track GPS location" checkbox, and modify the distance to 100 meters.
Now that you're all set, let's move to the next step to add the functionality and code.
Distance Detector - Arduino
After we've set up our Integromat scenario, we want to connect it to our Arduino code and Adafruit board.
1. First, we'll create the combination of tones that form the words we'd like our alarm to say.
2. Then, we'll create the function playAlarm that will use the Circuit Playground Express speaker library to "say" the words in the correct order.
3. At last, we want to create the functionality that will cause the alarm to trigger once our integromat identifies that our smartphone has left the allocated study zone. For that, all we need to do is to make the location change trigger an HTTP request that is already integrated with our Blynk project.
Surprise! we've already done that when we created the Integromat scenario. All we need to do is connect the virtual pin (say V6) to our code, and activate playAlarm.
Now, while our Integromat scenario is activated, whenever we leave our allocated 100 meters of study zone, the alarm will trigger and we'll be notified to: "Stop right now. Return home and finish test" and also our location will be documented and monitored in the Google Sheet we created.
THERE IS NO ESCAPE.. DON'T EVEN TRY...
Phone Lift Detector
Now we'll add some code for the phone lift functionality (see screenshot)
Phone Lift Detector uses the phones accelerometer sensor to receive the positioning of the phone in the three dimensional space. Using this feature we can identify when the student is trying to pick his/her phone.
Whenever Phone Lift Detector is activated while studying, our Adafruid board will play a horrible alarm sound accompanied by red flashing lights until our smartphone is put aside.
For the sound we'll play a specific note using declared frequency and duration, both included in the playNote() function. For the flashing lights, we'll use the standard setPixelColor function with the (255,0,0) value for red.
You're Finished!
That it! All set for your Study Helper.
Hoping you are ready to get your A+ in the next exam :)
Good Luck!