How Basic Coding Skills Helped Me Entertain My Cats
This is my case study for you noncoders out there, why you should learn the basics of programming, and why you should not be afraid of it.
The worldwide pandemic situation forced us to stay more at home, which made my cats develop a new habit of constantly demanding attention. We catified our home the best we could, but that wasn’t enough for our cats and they still got bored from time to time.
A few years ago we bought a 2nd hand gadget toy mini Sphero. We thought that it was a cool tech piece, that you can control from your smartphone and play games with it. It wasn’t intended for cats and we were not aware that it could be coded.
Back to present time, I was yelled at by my cats, as they were getting bored by the minute. I was seeking a way to entertain them with as little effort as possible from my side. I remembered we had this Sphero ball, and I thought “Surely they have some solutions for cats”.
They didn’t. Their official app (and the games in it) was designed for humans (especially young humans). But I discovered they have a Sphero Edu app — an educational app that teaches coding and STEAM with Sphero robots. I took a glance at this app at the Play Store and decided that if kids can code their Sphero, so can I.
My first attempt to code my Sphero robot was with “blocks”. Sphero calls them “blocks” but it’s actually Scratch — a programming language for kids. With Scratch, kids learn the basic principles of coding in a more visual way and without the need to learning the syntax of a specific programming language. Even though I’m a more advanced programmer than a kid who just learned their way in the coding world, I found it inconvenient to code, having to use words and type on my phone. So I chose the easier way of drag and drop.
At first, I wanted to make my robot roll in a perfect square path. It took me about 7 attempts to understand the variables and how my input affects the robot's movement.
As I added more to my program, it became much harder for me to handle the UI of scratch (I guess I’m too old for it). Something that I could write in a second (while/for-loop) took me minutes to figure out in Scratch. With more elements, drag and drop became less efficient and I made mistakes almost on every edit. Luckily, they have an undo button.
After you start to code with blocks, you can see the program written also in JavaScript, but you cannot edit it. My code looked very simple, and since I had a hard time with Scratch I decided to start over, but this time, I will write my code.
I switched to my PC to write the code, but since my PC doesn’t have Bluetooth, I still had to run it on my phone.
Now I was determined to create a program that will roll the robot in a random direction, speed, and duration. Hopefully, such rolling patterns, or lack thereof, will be engaging for cats. I added some minor LED color settings and a collision algorithm, to avoid a situation in which the Sphero will persist to roll into a wall.
To be able to code something like this on my own, my programming skillset required the knowledge of loops (while/for), conditions (if/if-else), and functions. These are the fundamentals of every programming language. If you learned it once, you can do it in every other programming language. If you look in the code snippet in the example, you will find words such as “async” and “await”. I learned asynchronous programming before but I never used it with JavaScript. This part was new to me. I used Sphero documentation to understand where I should place it.
After each iteration (new edit to the code), I ran the robot to test my code. Sometimes I placed sound command (as a way to replace prints) if I wanted to see if it gets inside the condition loop, or if some function was called.
In the final version of the program, I mostly used methods that are provided by Sphero, such us setMainLed, roll, getRandomInit, setSpeed, and more. I used while loop to call the roll method in an infinite loop until a collision occurs. onCollision is a build-in event(just like a mouse click) which allows me to create a specific behavior after the robot hits a wall. I wanted it to stop for 5 seconds and then roll in the opposite direction. Unfortunately, in most cases, my Sphero mini doesn't recognize these collisions, but I kept this part of the code for the rare occasions it does.
I heard times before that these days, everyone should know how to code, even if they’re not developers. This skill may come in handy across various fields, especially when we have code surrounding us in almost every aspect of life. I could never imagine a day on which I will program a toy for my cats. I could never imagine that it will be so easy and fun. A real no brainer.
Did my cats enjoy their autonomous toy? Well… They were intrigued by it but rarely chased it.
Special thanks to Masha Dodzina for editing and fixing my English.