In our first part on pointers, we covered the basics and common pitfalls of pointers in C. If we had to break it down into one sentence, the main principle of pointers is that they are simply data types storing a memory address, and as long as we make sure that we have enough memory allocated at that address, everything is going to be fine.
In this second part, we are going to continue with some more advanced pointer topics, including pointer arithmetic, pointers with another pointer as underlying data type, and the relationship between arrays and pointers. But first, there is one particular pointer we haven’t talked about yet.
The one proverbial exception to the rule that pointers are just memory addresses is the most (in)famous pointer of all: the
NULL pointer. Commonly defined as preprocessor macro
(void *) 0, we can assign
NULL like any other pointer.
Continue reading “When 4 + 1 Equals 8: An Advanced Take On Pointers In C”
Pointers — you either love them, or you haven’t fully understood them yet. But before you storm off to the comment section now, pointers are indeed a polarizing subject and are both C’s biggest strength, and its major source of problems. With great power comes great responsibility. The internet and libraries are full of tutorials and books telling about pointers, and you can randomly pick pretty much any one of them and you’ll be good to go. However, while the basic principles of pointers are rather simple in theory, it can be challenging to fully wrap your head around their purpose and exploit their true potential.
So if you’ve always been a little fuzzy on pointers, read on for some real-world scenarios of where and how pointers are used. The first part starts with regular pointers, their basics and common pitfalls, and some general and microcontroller specific examples.
Continue reading “The Basics and Pitfalls of Pointers in C”
MIDI instruments and controllers are fun devices if you want to combine your interest in music and electronics in a single project. Breaking music down into standardized, digital signals can technically turn anything with a button or a sensor into a musical instrument or effect pedal. On the other hand, the receiving end of the MIDI signal is mostly overlooked.
[FuseBerry], a music connoisseur with a background in electronics and computer science, always wanted to build a custom MIDI device, but instead of an instrument, he ended up with a MIDI controlled light show in the shape of an exploded truncated icosahedron ([FuseBerry]’s effort to look up that name shouldn’t go unnoticed). He designed and 3D-printed all the individual geometric shapes, and painstakingly equipped them with LEDs from a WS2818B strip. An Arduino Uno controls those LEDS, and receives the MIDI signals through a regular 5-pin DIN MIDI connector that is attached to the Arduino’s UART interface.
The LEDs are mapped to pre-defined MIDI notes, so whenever one of them is played, and their NoteOn message is received, the LEDs light up accordingly. [FuseBerry] uses his go-to DAW to create the light patterns, but any software / device that can send MIDI messages should do the trick. In the project’s current state, the light pattern needs to be created manually, but with some adjustments to the Arduino code, that could be more automated, something along the lines of this MIDI controlled Christmas light show.
Continue reading “There’s More To MIDI Than Music – How About A Light Show?”
Undoubtedly, the ESP8266’s biggest selling point is its WiFi capability for a ridiculously low price. Paranoid folks probably await the day its closed-source firmware bits will turn against humanity in a giant botnet, but until then, hobbyists and commercial vendors alike will proceed putting them in their IoT projects and devices. One of those devices is the Yeelight desk lamp that lets you set its color temperature and brightness via mobile app.
[fvollmer] acquired such a lamp, and while he appreciated its design and general concept, he wasn’t happy that it communicates with external servers. So he did the only reasonable thing and wrote his own firmware that resembles the original functionality, but leaves out the WiFi part. After all, the ESP8266 has still a lot to offer in its core essence: a full-blown 32-bit microcontroller with support for the most common, hobbyist-friendly SDKs.
The lamp’s color temperature and brightness are set with a rotary encoder / push button combo switch, and the LEDs themselves are controlled via PWM. All things considered, it’s a rather straightforward endeavour, for which [fvollmer] chose the standalone C SDK. And in the end, it’s not like he’s unreasonably cautious to keep some control over his household items.
Whether we like it or not, eventually the day will come where we have to admit that we outgrew our childhood toys — unless, of course, we tech them up in the name of science. And in some cases we might get away with simply scaling things up to be more fitting for an adult size. [kenmacken] demonstrates how to do both, by building himself a full-size 1:1 RC car. No, we didn’t forget a digit here, he remodeled an actual Honda Civic into a radio controlled car, and documented every step along the way, hoping to inspire and guide others to follow in his footsteps.
To control the Civic with a standard RC transmitter, [kenmacken] equipped it with a high torque servo, some linear actuators, and an electronic power steering module to handle all the mechanical aspects for acceleration, breaking, gear selection, and steering. At the center of it all is a regular, off-the-shelf Arduino Uno. His write-up features plenty of videos demonstrating each single component, and of course, him controlling the car — which you will also find after the break.
[kenmacken]’s ultimate goal is to eventually remove the radio control to build a fully autonomous self-driving car, and you can see some initial experimenting with GPS waypoint driving at the end of his tutorial. We have seen the same concept in a regular RC car before, and we have also seen it taken further using neural networks. Considering his background in computer vision, it will be interesting to find out which path [kenmacken] will go here in the future.
Continue reading “Go Big Or Go Home – This Arduino RC Car Can Take You There”
When it comes to music production and audio engineering, Linux isn’t the most common choice. This isn’t for lack of decent tools or other typical open source usability issues: Ardour as a highly capable, feature-rich digital audio workstation, the JACK Audio Connection Kit for powerful audio routing, and distributions like Ubuntu Studio packing all the essentials nicely together, offer a great starting point as home recording setup. To add variation to your guitar or bass arrangement on top of that, guitarix is a virtual amp that has a wide selection of standard guitar effects. So when [Arnout] felt that his actual guitar amp’s features were too limiting, he decided to build himself a portable, Linux-based amp.
[Arnout] built the amp around an Orange Pi Zero with an expansion board providing USB ports and an audio-out connector, and powers it with a regular USB power bank to ensure easy portability. A cheap USB audio interface compensates the lacking audio-in option, and his wireless headphones avoid too much cable chaos while playing. The amp could theoretically be controlled via a MIDI pedalboard, but [Arnout] chose to use guitarix’s JSON API via its built-in Python web interface instead. With the Orange Pi set up as WiFi hotspot, he can then use his mobile phone to change the effect settings.
One major shortcoming of software-based audio processing is signal latency, and depending on your ear, even a few milliseconds can be disturbingly noticeable. To keep the latency at a minimum, [Arnout] chose to set up his Orange Pi to use the Linux real-time kernel. Others have chosen a more low-level approach in the past, and it is safe to assume that this won’t be the last time someone connects a single-board computer to an instrument. We surely hope so at least.
Programming C without variables is like, well, programming C without variables. They are so essential to the language that it doesn’t even require an analogy here. We can declare and use them as wildly as we please, but it often makes sense to have a little bit more structure, and combine data that belongs together in a common collection. Arrays are a good start to bundle data of the same type, especially when there is no specific meaning of the array’s index other than the value’s position, but as soon as you want a more meaningful association of each value, arrays will become limiting. And they’re useless if you want to combine different data types together. Luckily, C provides us with proper alternatives out of the box.
This write-up will introduce structures and
unions in C, how to declare and use them, and how
unions can be (ab)used as an alternative approach for pointer and bitwise operations.
Continue reading “Unionize Your Variables – An Introduction to Advanced Data Types in C”