A Pandemic Snack Counter With an Arduino UNO and the Display Shield
by slviajero in Circuits > Arduino
96 Views, 0 Favorites, 0 Comments
A Pandemic Snack Counter With an Arduino UNO and the Display Shield
This is my entry to the Silly Solutions contest, so it is a bit silly but I actually built and used it this way.
This is a device to count how many snacks I have taken from the fridge in out kitchen. There is a story behind this. During the pandemic I was in home office like the rest of us. For me this started in March 2020. I had been in Italy before and was likely infected so I stayed at home as soon as I was back from the trip. Over the first 3 months I gained 5 kg of weight. When I complained about that to my wife she simply asked me if I had counted how often I went to the fridge for a snack during a typical workday. I answered, I would estimate 4 to 5 times. She just shook her head.
So I build this device to count it reliably and found that it was 20 times in a typical workday.
This is a silly solution because I just could have counted on a list. But I was bored and wanted to build something. After all, there was not much more to do during the time. It is also silly because I used BASIC as a programming language on my Arduino UNO. To sum it up, a useless device programmed in an obsolete language. But it was fun nevertheless.
Supplies
I will used and Arduino UNO and a standard LCD 16x2 display shield with buttons to build the solutions. There are other options which I will mention further down in the text.
The key component is a standard PIR sensor, which you can see in the picture on the upper left side. These sensors get active if something comes near them.
Mount the Arduino, a few plywood pieces can be used. Essentially we need to build a little stand so that the Arduino and the sensor can be mounted on it. A small breadboard is helpful for extension of the device. Maybe you want to add a traffic light and use a few LEDs with the right resistors for it.
As a firmware for the Arduino, you will need the BASIC interpreter from https://github.com/slviajero/tinybasic/tree/main/Basic2/IoTBasic
Despite the name this is not a Tinybasic as often used on some MCUs. It is a full featured BASIC interpreter, implemented from scratch for microcontrollers.
Prepare the Components
Display shield usually come without connector sockets in the front. They can be plugged on the UNO and then cover all the pins. I usually solder connectors on the shield right away. You can see this in the first picture. If you cannot do this then you can also squeeze cables into the sockets of the Arduino before you put the shield on it.
For this device I made a little stand from spare plywood pieces, screwed the Arduino of it. On the upper side of the stand I mounted a small breadboard and the PIR sensor itself. They usually come with screw holes. I used a small piece of wood to mount it, antenna style on top.
This is all the hardware work needed for now.
Prepare the Firmware
To run BASIC on your Arduino you need to download the sketch
https://github.com/slviajero/tinybasic/tree/main/Basic2/IoTBasic
from the repo and install it into your Arduino sketch folder.
The interpreter has two configuration files. One describes the language model you are using.
This file is called language.h. Its first lines should look like this for an Arduino UNO
This should be the default. No edits are needed. BASICSIMPLE is an integer BASIC interpreter with approximately the same language features as a 80s home computer like the Commodore 64. BASICFULL is much more powerful but to large for the UNO and not needed here.
The hardware the interpreter runs on is described in hardware.h. You need to edit it and make the appropriate changes. This first lines should look like this after the edit
In the first section ARDUINOPICOSERIAL, DISPLAYCANSCROLL, LCDSHIELD, and ARDUINOEEPROM are defined, all others are undefined. This means we want to run with a small serial implementation, a scrollable display based on the LCD shield and we want to use the EEPROM of the UNO.
In the second section, the setting ARDUINOPGMEEPROM, has to be set on define. This makes the EEPROM the primary program storage. Inserting and editing programs will go directly to the EEPROM. On UNOs with their small EEPROM sizes this is quite useful.
Do not change any of the settings below that point.
After making these changes, you can upload the sketch on your UNO with one of the many Arduino IDEs. I still use 1.8.15 but tested on the newer versions occasionally.
You should see something like this
after upload in the message field of the IDE.
Opening the serial monitor with 9600 baud and NEWLINE as serial settings you will see the BASIC command prompt
On this prompt > you can type in commands interactively and control many features of the Arduino.
A full tutorial and manual can be found here: https://github.com/slviajero/tinybasic/blob/main/MANUAL.md
For now, type in
on the command line. The text Hello should appear on the display as shown in the photo.
BASIC is an interpreter language and all commands can be entered at the command line. They are executed immediately. If you start a command with a line number, it is stored. The NEW command deletes and existing program and the LIST command shows the program. If you are unfamiliar with BASIC,
The display should show "Hello Display"
The program is stored in the EEPROM. You have 1000 bytes for your program. Just like in the very old days. Entering program lines can be a little slow if the EEPROM is very full. But on an Arduino UNO with its small and fast EEPROM this is not a problem. If you use other MCUs, please read further down "Other Devices".
Connect the PIR Sensor and Write the Program
PIR sensors usually have three electronic connectors. Two are the voltage and one is the output. Output is low unless it has noticed some activity in front of it. Then the output goes on high for a certain period of time and then falls back to low. Billions of devices around the world use these sensors. These are typically automatic door lights and other slow motion dependent processes. Two potentiometers control the behaviour. One is for the sensitivity and the other for the time the device stays on high. Best set both fully to the left for a start.
Connect the power lines to the Arduino 5V and GND pins and connect the output to pin 2 of the Arduino. This is the third pin on the right of the shield.
Once the sensor is connected, we can test its function. Enter the following program on the command line.
Then type RUN. The output should look like this
The second value is the state of the sensor read from pin 2. When you move in front of the sensor it changes to 1 and then falls back to 0. Unlike in the old BASIC dialects, FOR NEXT can be used to create an infinite loop. This avoids the use of GOTO statements. I will count through the entire 16 bit number range if no limits in the loop are given.
The program can always be interrupted by sending the character # from the terminal. Or you simply press the reset button which also brings you to interactive mode.
The next step of the program will implement the counter. For this, add the following lines to the program
Then run it again. The output should look like this:
The variable C counts the number of activations of the sensor now. It is programmed to trigger at the raising impulse. Of course this could be done much more elegantly with interrupts but for this the simple method is good enough.
Now we can add the display code. For this add the following lines:
Now the display shows the number of times the sensor was approached. @X and @Y are special variables in this BASIC. Setting them positions the cursor on the display. They can also be read.
The last improvement would be to add some thresholds and a status message. Add the following lines for this:
Now the display also shows the snack status.The snack status is stored in the string variable ST$. Strings by default are 32 characters. They can be dimensioned to any arbitrary length. Everything up to 4 snacks is GREEN, from 4 to 8 we are at status YELLOW and above 8 the status is RED.
No more snacks allowed!
I tracked my eating behaviour with this device over a certain period of time. It was shocking. One day I had made 20 trips to the fridge for a little snack. No wonder I became fat quite fast during the pandemic.
One things is still missing. The device should start the program without typing in RUN from the command line once it is connected to power.
This is done by entering
on the command line. With this setting the system is in autorun mode. The program in the EEPROM will start automatically. The setting is permanent. It can be changed back on the command line by interrupting the program with # and entering
Congratulations! You build the first working snack counter.
The complete program so far would be:
It is 15 lines and takes approximately 300 bytes of the Arduino's EEPROM.
Show the Traffic Light
As we are already using traffic light colours to indicate the snack status, why not add a traffic lights with LEDs to it.
For this I added three LEDs in red, yellow and green. They are connected to pins A1 to A3 which translates to digital pins 15, 16, and 17 with 300 Ohm resistors.
The following lines would be needed in the program
Line 50 sets the pins to OUTPUT and lines 300 to 330 switch on the appropriate LED.
This way you see if you still are allowed to open the fridge or if you are already past the maximum allowed number of snacks.
Using Other MCUs
All this is written for an UNO and the standard 16x2 display shield. These are low cost parts which every Arduino enthusiasts has. My original build was with an ESP8266 and a Nokia display.
The BASIC interpreter supports several display and most MCU platforms. Please read my Wiki and the hardware manual if you plan another device.
https://github.com/slviajero/tinybasic/wiki/Hardware-Platforms
https://github.com/slviajero/tinybasic/wiki/Peripherals:-Display-LCDs,-TFTs,-and-VGA
You will need different settings in hardware.h depending on the display and the storage you use. ESP systems have a in flash disk that can store larger programs.
A Silly Solution
This may be a silly solution because it solves a problem with technology that could be solved with a bit of common sense and a piece of paper.
It is also silly because it uses an obsolete programming language.
The fun part of it was that one could do something with only 15 lines of code. No compilation, no complicated libraries with hundreds of features. The BASIC interpreter helps to make programming simple things as easy as back then when we had a home computer on our desk.
Sometimes I long for this simplicity.