Weblog

Posts

:info: Welcome to the blog section! This is where content that isn't long enough to be an article or a separate page goes to live! This will be a little bit of a calmer space. Spelling mistakes, and strange phrasing will not edited much here.

Among the many problems I've encountered and had to solve with the Arduino microcontroller, there are two I would like to write about. These will likely be obvious to anyone more accustomed to microcontrollers, however I feel like my experience with them has been really valuable for me, and may just well serve as examples of why I find this so intriguing.

In my previous post, I’ve said how I’d wanted to create a kind of music player. To store the notes, at first I naively created a note class, with a frequency, and a hold time. Both fields were integers. Little did I notice, that while I was struggling to somehow transcribe sheet music, or MIDI files by hand, that the memory of the Arduino was being significantly loaded. I reached about 49% of the Flash Storage with just one song that didn’t even sound like the original. Then I started thinking about it.

The day after, I devised a two-byte solution which blows the 8 bytes per note out of the water. The format is not yet final, and I’m still not educated in music, but for the time being, this is the breakdown:

Sometimes you’ll see notes written as such: A#4. This essentially encodes that. The first two bits are flags. IsAccord, and then isSharp. Simple enough, they do not require much explanation. Then three bits follow for the letter, so like C, A, G, E, F… there are seven of them, stored in three bits, which means that one of the possible combinations is never used and is promptly lost. Following this, another three bits, for the pitch. This is the number, ranging from 0 to 7 here. Limited, but hopefully covers quite a decent range. It certainly covers the frequencies in the pitches.h floating around in Arduino related communities. The second byte, in entirety, is just the hold time.

Speaking of pitches.h, the idea is that the Arduino will encounter a note, bit-mask the letter, pitch, and sharpness, and use a lookup table to find the appropriate pitch.

The other problem I’ve encountered was with buttons serving as digital inputs. More pressingly, three buttons in a resistor ladder. It happened quite sometimes that I pressed it, and nothing was happening, to my absolute confusion. At this time, I had already abandoned delay()-based polling, because I had to run multiple timers independent of one another. It still puzzled me that even with relatively frequent polling, the buttons just did not work for some time.

Then I learnt about interrupts. I’ve heard about them before, but I’ve never really dealt with them, or wrote them. Arduino made the implementation quite easy. I quickly pulled out the analog reader wire, and replaced it with the digital required for the interrupts so that I could detect changes with the buttons. I’m sure someone already caught the mistake, digital pins cannot read analog voltages. The solution is dead simple, yet its discovery made me feel very proud of myself. I could just have both wires. The interrupt would raise a flag, which if encountered in the loop would prompt the analog reading. This completely sidesteps the timing misery, and allows me to have three buttons wired into only two pins. (one digital, one analog). Computing, and electronics really are a rollercoaster that takes you between the feelings of being unstoppable and being incapable of anything.

I think model trains are awesome. Since I study programming, it came naturally to me that I would want to build a model train that is controlled by software that I write. Unfortunately that seemed a little too improbable with my current conditions, so it got shelved, and it molded. I wasn't quite sure what to call it, I labeled this concept physical programming. Probably a bonkers title, but that is how it lived in my head for a while. Eventually I found myself on subreddits of interesting fun contraptions, (/r/doohickeycorporation/) and model trains.

Another time I was also growing a bit frustrated with some forms of advertising, so I contemplated using a Pi-hole, but realized that it wouldn't help much in my specific case. However the concept was planted, that it'd be cool to craft something myself with some microcontroller. I found out about the Arduino line of microcontrollers, and the accessibility, combined with all the stuff I saw online convinced me, that I should probably get one to learn something this Summer.

I got my hands on a Arduino Starter Kit R4 today. I picked this, because it has components which are surely to be compatible with one another, and a neat little guide was provided too. Sure I could have gotten cheaper clones, the blueprints are open-source, however official gave me the security that I won't have to deal with finding out what components to get and where, which was valuable, considering that I had known practically nothing about these components until a few hours ago.

Admittedly, this write-up is a little pre-mature, considering I'm not too far into it yet, practically only having done the first example project.

I never really engaged with electronics on this level. I did have physics classes but we never actually wired anything. After completing my first circuit to turn a light on with a button, I slowly modified it. Now it has a blue LED, and a red LED, both with buttons, so you can turn them on independently. The inspiration was a police light. Of course there is nothing stopping you from pressing down both of them at the same time, and the circuit is very basic. I'm just surprised with how much fun I had with something so simple. It must seem second nature to some, but novelty can craft very pleasant experiences. Of course this was just electronics, and I have not touched programming it. I'm looking forward to the challenges offered by limited resources, and the environment where a C like language can thrive instead of feeling tedious like on full computers.

My goal, which probably isn't that advanced as I might believe it to be, is to have a little music player. The Piezo buzzer would synthetize audio, and buttons would allow stopping, playing, and cycling between hard-coded tracks. The LCD would show the name of the song, and perhaps even time.

I'm looking forward to making it real!

A GIF animation of the blue and red lights being pressed one after another
[MOTD]