Holiday Special 19

Enter Your Holiday Season Project for a Chance to Win Over $20,000 Worth of Prizes and Gifts to Give!

Back to The Project14 homepage

Project14 Home
Monthly Themes
Monthly Theme Poll

 

COOKIE JAR

 

1. Introduction

     Hi! This will be my entry for this years Holiday Special Project14. I was barely able to do any project until the end for quite some time due to a lot of my time being trapped for university, but I finally managed to get some free time to work on one of the projects and hopefully there is plenty more to come this following year. First of all I would like to wish everyone happy holidays and a happy new year and also thank element14 for this awesome gift which I got one day at my doorstep.

The package contained a lot of cool stuff as well as a hand written letter which I loved! For this project I decided to go with some parts of this gift, more particular with the Arduino MKR1010 WiFi and the relay shield, which I had to cut down a bit which will be shown later in the blog. Here is what I'll be making.

 

2. Idea

     I decided to go with a pretty fun project for this Project14. For the holidays as most people I love relaxing a bit, staying at home more and of course, eating sweets. Everyone loves eating sweets, from gummy bears and cookies to chocolate bars, there is something sweet that everyone loves. Combining that with not moving a lot during the holidays isn't really the ideal combination unfortuantely, so I decided to make a project based around this problem. My idea for the project is to make a jar which is full of candy that unlocks only when you reach a certain number of steps during that day. The idea is to use a pedometer on my phone to send the number of steps online and then get that information using the Arduino.

 

3. Plan

     As you can see from the teaser picture above I got a giant glass jar with some sort of a mechanism and a 4 digit display inside. My plan is to construct a small mechanism which will "lock" and "unlock" the jar depending on the number of steps taken that day. By locked and unlocked, it just means if the user will be able to remove the jar lid or not. The 4 digit 7 segment display will be an added touch to follow the progress for that day. Here is a diagram of how it all should work together.

As I've said previously, the main board for this project will be the Arduino MKR WiFi 1010, which will be controlling the servo and other electronics while gathering data over WiFi using Thingspeak. On the user side, I will be making a small simple app using MIT App Inventor 2, to log the number of steps I take, as well as upload that info to Thingspeak. As for why I went with Thingspeak, I think it will work good enough for this project, specially because I have experience using it with Android apps in the past, but it will my first time using Thingspeak with Arduino.

 

4. Materials & Tools I Used For This Project

     Before I start with the work on this project, I will just list some of the essential things I needed for working on this project when it comes to tools and materials, as you will see none of the material nor tools are hard to come by so it's a project that anyone can tackle without any problem.

 

Materials

  • Wood
    • 4mm thick plywood used in schools
    • 12mm by 12mm wooden planks
  • Bolts and nuts
    • 2mm bolts and nuts
    • 3mm bolts and nuts
    • 2 longer 6mm bolts and 4 6mm nuts
  • Electronics
    • Arduino MKR WiFi 1010
    • Arduino MKR Relay Shield
    • Push button
    • 7 segment 4 digit display
    • 18650 lithium cell
    • 5V relay
    • 1k resistors
    • 10k resistors
    • 100k resistor
    • 1uF capacitor
    • 2 100nF capacitors
    • IC7805 linear regulator
    • NPN and PNP transistor
    • Blue LED
    • 800mA fuse
    • Thermostat switch
    • Small slide switch
    • Boost converter
    • Screw terminals
    • Perfboard

 

Tools

  • Soldering iron and other equipment needed for soldering
  • Screwdrivers
  • Hex key set
  • Needle nose pliers
  • Hand wood saw
  • Sandpaper
  • Drill with a 3mm, 5mm and 6mm drill bit

 

5. Build

     Time to start building this project. I've covered all of the more important materials and tools I'll be using for this project. I'll begin the build with the mechanism first. Here are some dimensions of the jar that we have to be aware of when designing this mechanism.


The upper part of the jar has a diameter of a 100mm, while the inside of the jar has an inner diameter of around ~150mm. So our mechanism when closed has to have a diameter smaller than 100mm, but when it opens to lock the jar up, the diameter has to be more than 100mm but less than 150mm for it to work properly. For the design of the whole project I went with a "sandwich" design where I made everything in separate levels. Since the diameter of the top part is 100mm I decided on going with 90mm for everything I'm doing, keeping it safe like that, while also not having to extend the mechanism too far to lock the jar. Here is how the mechanism idea and design process went.

 

Mechanism

     Now that we have the main dimensions written down let's start with choosing the actuator for this project. I went with a standard sized servo. I had one laying around, plus I have a lot of screw on mounts for them. They are really easy to control with an Arduino. My other option was a small stepper motor, it would require a small motor driver and it's smaller in size than a servo which is a plus, but I didn't have any mounts for the stepper, so I decided to stay with the servo. Here are the ideas which I had for the mechanism, until settling in the end for idea number 3 as it seemed like the most robust of them all.

 

Ideas

 

Idea 1 - Geared mechanism

This was my first idea for the mechanism, it's really simple and easy to make if you have the required parts. A main big gear would be attached to the mount of the servo motor, and 3 other gears would be geared into the main gear, let's call those side gears. to those side gears, we have attached locking arms, which would protrude out of the base plate and therefore the whole thing would have a width of over 100mm effectively locking the jar. While this approach is great in so many ways, from how simple it is to make, to how compact it can be, also just by adjusting the length of the locking arms, we can adjust how far they protrude, I didn't have 3 matching gears to make this. I think this would be the best and most compact approach if I were using something like a 3D printer to make parts, or have the parts laser cut.

 

Idea 2 - Springed mechanism

This mechanism has the same motion of the locking arms as the first mechanism. This idea came from developing the Idea1 so I can actually make it with stuff that I have. It also has 3 locking arms with axles being close to the edge of the base plate, but instead of using gears to transfer the torque, my idea was to literally use just the mount of the servo which would push on the actuator pin and move around the locking arm like that. I added a limiter and spring mount on it, to make the mechanism lock itself when it's either opened or closed.

In both fully locked and unlocked position the spring will hold it in place, so it doesn't close on it's own. This idea was doable with hand tools and some plywood, I also loved the spring addition to the mechanism. The only kind of sketchy part was the actuation part where the motor mount pushes against a pin but it could have worked for sure.

 

Idea 3 - Piston mechanism

     This idea different a lot from the other 2 when it comes to how it worked. While the previous 2 had swinging arms, the idea with this one was to have small pistons which would go out and in. After a few sketches third turned out to be the simplest and most robust idea of them all to build with what I had. The first ideas was to have the pistons held in with small springs and for the motor mount to have small bearing on the other edge which would push out the pistons as it rotates. Here is how I imagined it.

While this could have worked spacing with attaching the bearing to the motor mount would mean i would need vertical spacers for pistons and so on, so I went with a simpler and more robust approach than this which didn't use any springs, instead, it just used small piston connecting rods to connect the piston to the motor mount. This turned out to be pretty easy to make while it is a bit on the bigger side than let's say Idea 1, it will do the job just great.

The baseplate will be made using regular plywood which can be bought in bookstores, the 4mm thick one that's used in schools, the pistons as well as piston guides are made of 12 by 12mm planks, the motor mount uses a off the shelf motor mount with a small plywood disk I made and in the piston connecting rods are made out of thin metal which I cut, bent over and drilled holes to attach it to the mechanism. With the general idea out of the way, it's time to start making this mechanism.

 

Build

     For this part of the build the main material will be wood, more specifically the 4mm thick plywood used in schools. I love using it, it's easy to obtain in any bookstore and it's really easy to work with. Besides that I'll be using 12mm by 12mm wooden planks, I got them from a local hardware store. I will be using them to make the pistons as well as the piston guides.

 

Motor mount

     I will be using  a motor mount that already came with the motor, but, it needs an additional attachment. The one I am using has 6 legs, but they are so thin, that I wouldn't like to attach bigger screws to them, so I cut a 35mm diameter circle out of plywood which I then attached to the motor mount.

 

{gallery} Motor Mount

IMAGE 1: Motor mount and wooden disk

IMAGE 2: The fit is excellent, but the mount needs modifying

IMAGE 3: I cut off 3 of the legs, so I can further in the build mount bigger screws easier

IMAGE 4: I drilled 3 small holes for attaching the 2 together

IMAGE 5: Finished mount, I only later drilled the screws holes which attached to the piston connecting rods

IMAGE 6: Here is how it looks mounted to a servo

 

With that out of the way, it's time to start cutting out base plates, and start making the lower section with pistons. For connecting everything I just used a bit of glue as it was the best way to connect everything precisely. One thing I had to solve was that the pistons and piston guides are the same size, in other way if I just glued everything together, depending on small variations in the size of the wooden planks, I can get a mechanism which doesn't function properly, with piston getting stuck and so on. To solve this I used cardboard spacers when gluing as well as when putting the upper base plate on, and in that way insuring that everything runs smoothly. Here are the cut parts that I cut out off wood.

Base

The lines that can be seen drawn on the right base plate are the guides for putting the pistons, as I've shown in my design I went with a 3 pistons design. The diameter of the base plate circles is 45mm, pistons are 40mm in length and the pistons guides are 25mm.

{gallery} Lower Layer

IMAGE 1: Attaching the first piston guide, I held down the piston in place with a small clamp, and put a piece of cardboard next to it, while I glued it in place

IMAGE 2: After letting the first side dry a bit, I would attach the other side the same way, and then put some clamps until the glue sets

IMAGE 3: All glued in place

IMAGE 4: I glued small pieces of cardboard on top of the piston guides so I don't have any clearance issues later on

IMAGE 5: And here I just added the upper base plate, making sure all the grooves line up with where the pistons will be

 

Pistons

     The pistons are made out of the wooden planks and they are 12x12x40mm. All they need is a hole for the bolt to go through, to which the piston connecting rod will attach.

To make the holes I just used 2 different sized drill bits. First the thinner one that went all the way through, and then I took another one which was wide enough for the head of the bolt to fit, and drilled about half way into every piston. I ended up with this:

The grooves cut into the upper base plate are so the bolts can go in them. I decided on attaching the bolts at this end rather than the other one like on a car, because this way the mechanism will function more smoothly. The piston connecting rods are longer which means as the mechanism is let's say, pushing the pistons out, there will be some force on the piston guides as the piston connecting rod is not pushing straight on, but because it's longer than attaching it at the near end, that means that the force that we need, that actually pushes the piston out, is far greater than the force pushing the piston into the piston guides. Here is a picture of how the pistons fit into the mechanism as well as a demo of how the mechanism is working. I didn't have the connecting rods made yet, so I just used small pieces of cardboard instead.

 

 

Piston Connecting Rod

     In the video above as you can see I used improvised connecting rods made out of cardboard, for the final build I decided to go with thin metal. The metal I used is from a leftover piece of gutter, the metal is just what I need because it's not too soft to make it bend that easily, but it's also not that hard, so I managed to cut it with a pair of tin snips. After that all that needed to be done is to hammer them a bit into shape, I also gave them a curve because of how everything fit, as you will see later on, and drilled holes for attaching to the pistons as well as the motor mount.

 

Servo holder and final pieces of the mechanism

     The only thing left to do now is to mount the servo and connect everything together, for connecting the servo to this I decided on going with another base plate above the second one which will hold the servo as well as all of the electronics. I used 40mm spacers scavenged from an old drone.

{gallery} Mechanism Final

IMAGE 1: Aluminium spacers for separating the 2 layers

IMAGE 2: Servo is 20mm in width, so I decided to cut out a slow ~20mm wide to the servo can slot in

IMAGE 3: Small wooden blocks I cut out for attaching directly to the servo with a couple of bolts, the only thing left is to glue in the servo like this using this small wooden blocks, and the mechanism is complete

IMAGE 4: And here is how it all looks when it's put together, all that's left now is to try it out

 

Test

     I did a few tests to find out which are the correct angles for this set up for my servo, connected it to the Arduino and here is how it all looks. I'm really happy with how it's working and how it turned out in the end. I tested it with the jar and it all fits when the pistons are in, but it can't go through when the pistons are out, just as we wanted!

 

Electronics

     Now that we are done with the mechanical aspect of this project, it's time to concentrate on the electronics of this project. I tried keeping it really simple, but also adding some safety features just in case. To power the whole thing I went with an 18650 Lithium Ion battery that I got from a power bank, they are rechargeable, can store a lot of energy, and can also deliver a lot of current. This is how all of the electronics is connected:

The Arduino Vin Pin required 6-12V while I went with 5V for the servo and the display. Between the thermostat switch and the boost converter there is a small switch which is for the main power on/off, while a push button is the input on the latch circuit. I wanted to use a button for turning on the project instead of  a switch because if the thermostat breaks the circuit when it reaches it's temperature it will get the circuit going again on it's own as soon as it cools of. With the button which is normally opened, if the thermostat switch activated the only way to activate the circuit again is when the user presses the button again.

Be careful when using lithium cells in projects, as they can be pretty dangerous. They don't like being overcharged or overdischarged and they can also get pretty hot a lose a lot of their performance. If not treated correctly they can burst into flames and it's a pretty serious fire, so be careful if you plan on using them in your projects.

Latch circuit

     As I've explained above, I want to make a small latching circuit which will be triggered with a small push button, which will trigger a small relay, which will power the Arduino. This is the tutorial I followed for the latch circuit. https://www.homemade-circuits.com/simple-and-useful-transistor-latch/ . I tried making this circuit and it worked great, just one thing to note, as it is also mentioned in the tutorial, in this configuration the circuit will work for voltages above 5V, I tried connecting the 3.7V cell directly to the circuit, but it didn't function properly. For powering the servo and small display I used a IC7805 linear regulator to drop the voltage down to 5V from 10V that I am getting from the boost converter. After I was satisfied with how everything worked I put all of the components on a board so I can solder everything.

 

Thermostat switch

     Besides the current fuse, this is one feature that I really wanted to add to this project, a thermostat switch. A thermostat switch is a switch which is normally closed which opens at a specific temperature, it can be found in a lot of house electronics as form of protection, I found one of them in a printer for example. To roughly measure the temperature of the cell I just tied it with some zip ties. While I know the temperature transfer will be anything but ideal, this is just a worse case scenario if the cell starts heating too much, the switch will detect that for sure, the one I got reacts at 55C. Here is how they look.

Servo

     Nothing much to add for the servo, I used a standard sized servo from a RC car, and as for the Arduino side, I used the standard servo library. Here is the code I used for testing the final build of the mechanism, really simple.

#include <Servo.h>

Servo myservo;

void setup() {
  myservo.attach(5);
}

void loop() {  
   myservo.write(45); 
   delay(1000);
   myservo.write(130); 
   delay(1000);   
}



 

7 segment display

     To show the current number of steps I chose a 4 digit 7 segment display. I could have used a LCD display, but I thought this was gonna look cool with its bright red glow and just the number shining. One problem I had to solve with this display is that it was 4 digit, and going 5 digits with the number of steps during a day is recommended, so I wanted to use a decimal point. Again a problem, the one I have doesn't have the digit dot, but rather the 2 dots, so I just covered one of them with black tape. I need that dot for showing numbers bigger than 9999. I used the TM1637Display library which worked great for me in the past and got the job done again, here is a small test code for the display that I wrote, with a function that I will be transferring to the final version of the code, also here is a small bracket I made for the display.

#include <TM1637Display.h>

// Define the connections pins:
#define CLK 4
#define DIO 5

TM1637Display display = TM1637Display(CLK, DIO);

void displayNumber(int number){
    //Depending on the size of the number it displays the full number or a number with a dot
    //if the number is equal or over 10000
    if(number <= 9999){
        display.showNumberDecEx(number, 0b00000000, false, 4, 0); 
      }
      else{
          display.showNumberDecEx(number/10, 0b11100000, false, 4, 0);
        }
  }

void setup() {
  // put your setup code here, to run once:
  display.setBrightness(7);
}

void loop() {
  // put your main code here, to run repeatedly:
  for(int i=9000;i<50000;i++){
      displayNumber(i);
      delay(20);
    }
}





 

Final Assembly

     All that's left now is to assemble everything and test it out. To assemble it, I will be adding all of the electronics on top of the upper base plate and servo motor, as well as from underneath the jar lid and the connect the 2 halves together. To make connecting everything easy I used double sided sticky tape for a lot of the stuff. Next to the servo I mounted the cell with both fuses on top of it, as well as the switch, on the servo I mounted the boost convertor and on the other side the small latching circuit. Here is how that turned out.

Now to mount stuff to the lid. To the lid i will be mounting the push button as well as the Arduino. One problem I encountered is that the relay shield I planned on using was a bit too long to fit on the list, so I had to cut off a part of it to make it fit, besides these, I put in 2 big and long bolts which connect to the upper base plate of the mechanism later on. Because the bolts didn't have a thread all the way through, I had to put plastic tubing around the bolts which acted as spacers for mounting everything together.

With 2 more holes drilled into the top base plate and some wiring I ended up with the final hardware look for this project.

 

6. Android app

     My first idea was to try and get data directly off of an existing step counter app like Strava or Google Fitness. While doing some researach on this topic Google Fitness seemed like the most plausiable way of doing this, but as I tried experimenting with it's REST API I managed to extract the number of steps to show up on screen, but automating that for it to upload that data to Thingspeak seemed impossible in a reasonable amount of time for me. So instead I decided to make a small step coutner app using MIT App Inventor 2. I simply love MIT App Inventor since it's drag and drop programming for Android with which I had some experience writing codes from zero, and let's just say, that it's so much easier to do it with this service, specially when something is in a prototype phase like this, but I've also seen really polished apps coming out of it as well.

Here is how the app works, the service already has a built in pedometer component so we just have to call it everytime it detects a step to incremenet the number of steps. Using timer clock 2 we upload the data online every 16 seconds, because the maximum rate for uploading to Thingspeak is every 15 seconds, while with the timer clock1 we save all of the data on the phone. By comparing if the date is the same as the last date that was recorded for the step we can reset the number of steps to zero when a new day comes, and that would be all for the app.

 

7. Arduino Software

     With the Android app working ok, all that's left to do is to try out using Arduino with Thingspeak and then just putting the puzzle of different codes together into one. Separating the problems into individual small tests like I did for the servo and the display really helps me a lot when it comes to later on putting the whole thing together. Everything just goes so much smoother.

 

Reading data from Thingspeak

     For connecting to Thingspeak I went with the Thingspeak library, before starting this I didn't know there was a library so I was pretty excited when I found out about it. If you are using an Arduino MKR WiFi 1010 one thing you should know is that instead of the regular WiFi library you should go with the WiFiNINA library. There's a tutorial as well as examples on the official Arduino site. The standard WiFi library won't work. As for reading the data off Thingspeak it's really easy with this library as you will see in the upcoming code. Also we will be reading the status code of the read, status code 200 means everything is alright. To test all of this out, just change out the ssid and the password for WiFi, as well as Channel ID and Read API Key for your Thingspeak channel.

#include <SPI.h>
#include <WiFiNINA.h>
#include "ThingSpeak.h"
#include <TM1637Display.h>

#define CLK 4
#define DIO 5

char ssid[] = "ssid";        // your network SSID (name)
char pass[] = "password";    // your network password (use for WPA, or use as key for WEP)
int keyIndex = 0;            // your network key Index number (needed only for WEP)

int statusCode = 0;
int status = WL_IDLE_STATUS;
char server[] = "www.google.com";

TM1637Display display = TM1637Display(CLK, DIO);

WiFiClient client;

//---------Channel Details---------//
unsigned long counterChannelNumber = xxxxxx;            // Channel ID
const char * myCounterReadAPIKey = "API KEY HERE"; // Read API Key
const int FieldNumber1 = 1;  // The field you wish to read
//-------------------------------//

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  // check for the WiFi module:
  if (WiFi.status() == WL_NO_MODULE) {
    Serial.println("Communication with WiFi module failed!");
    // don't continue
    while (true);
  }

  String fv = WiFi.firmwareVersion();
  if (fv < WIFI_FIRMWARE_LATEST_VERSION) {
    Serial.println("Please upgrade the firmware");
  }

  // attempt to connect to Wifi network:
  while (status != WL_CONNECTED) {
    Serial.print("Attempting to connect to SSID: ");
    Serial.println(ssid);
    // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
    status = WiFi.begin(ssid, pass);

    // wait 10 seconds for connection:
    delay(10000);
  }
  Serial.println("Connected to wifi");
  ThingSpeak.begin(client);
  printWifiStatus();
  display.setBrightness(7);
  displayNumber(0);
  delay(100);
}

void loop() {
  long steps = readTSData();
  displayNumber(steps);
  delay(5000);
}

long readTSData(){
    // put your main code here, to run repeatedly:
    //---------------- Channel 1 ----------------//
    long data = ThingSpeak.readLongField(counterChannelNumber, FieldNumber1, myCounterReadAPIKey);
    statusCode = ThingSpeak.getLastReadStatus();
    if (statusCode == 200)
    {
      Serial.print("Number of steps: ");
      Serial.println(data);
      return data;
    }
    else
    {
      Serial.println("Unable to read channel / No internet connection");
      return 0;
    }
  }

void printWifiStatus() {
  // print the SSID of the network you're attached to:
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // print your board's IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);

  // print the received signal strength:
  long rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI):");
  Serial.print(rssi);
  Serial.println(" dBm");
}

void displayNumber(int number){
    //Depending on the size of the number it displays the full number or a number with a dot
    //if the number is equal or over 10000
    if(number <= 9999){
        display.showNumberDecEx(number, 0b00000000, false, 4, 0); 
      }
      else{
          display.showNumberDecEx(number/10, 0b11100000, false, 4, 0);
        }
















  }

Final Arduino Code

     Now that we have all of the pieces of the code we need, it's time to put all of it together into one big code and the project will be completed. With some troubleshooting for setting up all of the cases how to work I ended up with this code which for now works flawlessly. It can always be upgraded, the only annoying thing about it, as you will see in one of the videos, is the 10 second wait for the WiFi on start-up, but besides that I love how it works.

//Libraries
#include <SPI.h>
#include <Servo.h>
#include <WiFiNINA.h>
#include <TM1637Display.h>
#include "ThingSpeak.h"

//Pins for the servo
#define SERVORELAY 2
#define SERVOPIN 3

//Pins for our 4 digit display
#define CLK 4
#define DIO 5

//Step goal
//At this number of steps, the jar will open
#define STEP_GOAL 5000

//Variable for storing the number of steps
int numberOfSteps = 0;

//variable for storing the state of the jar
bool isJarClosed = true;

char ssid[] = "ssid";        // your network SSID (name)
char pass[] = "password";    // your network password (use for WPA, or use as key for WEP)
int keyIndex = 0;            // your network key Index number (needed only for WEP)

int statusCode = 0;
int status = WL_IDLE_STATUS;
char server[] = "www.google.com";

Servo myservo;
TM1637Display display = TM1637Display(CLK, DIO);
WiFiClient client;

//---------Channel Details---------//
unsigned long counterChannelNumber = xxxxxx;            // Channel ID
const char * myCounterReadAPIKey = "YOUR API KEY HERE"; // Read API Key
const int FieldNumber1 = 1;  // The field you wish to read
//-------------------------------//

void setup() {
  Serial.begin(9600);
  pinMode(SERVORELAY, OUTPUT);
  
  //Attaching the servo and setting it to it's start position
  myservo.attach(SERVOPIN);
  digitalWrite(SERVORELAY,HIGH);
  myservo.write(45);
  delay(500);
  digitalWrite(SERVORELAY,LOW);

  String fv = WiFi.firmwareVersion();
  if (fv < WIFI_FIRMWARE_LATEST_VERSION) {
    Serial.println("Please upgrade the firmware");
  }

  // attempt to connect to Wifi network:
  while (status != WL_CONNECTED) {
    Serial.print("Attempting to connect to SSID: ");
    Serial.println(ssid);
    // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
    status = WiFi.begin(ssid, pass);

    // wait 10 seconds for connection:
    delay(10000);
  }
  Serial.println("Connected to wifi");
  printWifiStatus();

  //Setting up ThingSpeak
  ThingSpeak.begin(client);

  //Initial state 4 digit display
  display.setBrightness(7);
  displayNumber(0);
  delay(100);
  
}

void loop() {
    numberOfSteps = readTSData();
    if(numberOfSteps != -1){
    displayNumber(numberOfSteps);
    Jar(numberOfSteps);
    }
    delay(5000);
}

void Jar(int num){
    //If the jar is locked and we reached our step count, we can unlock the jar
    if(isJarClosed == true && num >= STEP_GOAL){
        Serial.println("Unlocking JAR!");
        isJarClosed = false;
        digitalWrite(SERVORELAY,HIGH);
        myservo.write(130);
        delay(500);
        digitalWrite(SERVORELAY,LOW);
        return;
      }

    //If the jar is locked, and let's say, a new day comes and our steps return to zero,
    //we have to lock the jar
    if(isJarClosed == false && num <= STEP_GOAL){
        Serial.println("Locking JAR!");
        isJarClosed = true;
        digitalWrite(SERVORELAY,HIGH);
        myservo.write(45);
        delay(500);
        digitalWrite(SERVORELAY,LOW);
        return;
      }
  }

long readTSData(){
    //This function is for reading data from ThingSpeak
    long data = ThingSpeak.readLongField(counterChannelNumber, FieldNumber1, myCounterReadAPIKey);
    statusCode = ThingSpeak.getLastReadStatus();
    if (statusCode == 200)
    {
      Serial.print("Number of steps: ");
      Serial.println(data);
      return data;
    }
    else
    {
      Serial.println("Unable to read channel / No internet connection");
      return -1;
    }
  }

void printWifiStatus() {
  // print the SSID of the network you're attached to:
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // print your board's IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);

  // print the received signal strength:
  long rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI):");
  Serial.print(rssi);
  Serial.println(" dBm");
}

void displayNumber(int number){
    //Depending on the size of the number it displays the full number or a number with a dot
    //if the number is equal or over 10000
    if(number <= 9999){
        display.showNumberDecEx(number, 0b00000000, false, 4, 0); 
      }
      else{
          display.showNumberDecEx(number/10, 0b11100000, false, 4, 0);
        }






















  }

8. Testing the Completed Project

     With all of that done, now comes the fun part, testing out the project. To test all of this out, while I did test it with the app and everything works as expected, for showing how it works I just manually changed the value on Thingspeak to see the effect of the jar opening or closing. Here is a first video of the mechanism working:

 

 

Now that I know that everything is working smoothly, it's time to fill up the jar with all sorts of candy and give it a real test. Here are 2 videos of that, the first video start from mechanism already on, while in the second I turn it on with the button.

 

 

 

9. Upgrade Ideas

     While I love how this works there are some other things that I would love to add to this project, some of them being:

  • Wireless charging pad or magnetic connector for charging, so you don't have to worry about the battery life of this thing
  • Sound signalization when the goal is reached
  • Manual controls from the inside for setting up the step goal for that day
  • Another 4 digit display for showing only the step goal
  • Try out one of the other mechanism ideas to see if it could fit in a smaller space than this one
  • Cell voltage measurement

10. Summary

     This was a really fun project for me. I finally managed to get some free time to work on a project and I'm glad it's this one. I love how it turned out, I was thinking of covering the mechanism so it can't be seen from outside, but I really just love how it looks like this, specially with the small display turned on. As I've said, I tried getting data off of Google Fit, but turned out to be an absolute nightmare and I haven't really explored that much into other fitness apps, so I went with the approach of making my own step counter app. Happy holidays to everyone again, hope you enjoyed this fun project, thank you for reading the blog!

 

Milos