Flag variables are great, and totally not evil, when you just have two states: ON or OFF. What about when you have multiple states? Is there an option better than creating multiple flag variables?
The C-language has a declaration type just for this purpose. It is called an enumeration, or enum.
Setting up a state machine with enum is a surprisingly simple. Arduino and embedded programmers should use them!
All you need to do is create descriptive tag names, and let the compiler assign them an integer value. Unlike a #define which is just a macro replacement, the compiler treats an enum as your personal variable type.
This behavior comes in handy when you’re creating states for a state machine. I show how to create a simple state machine with enum, to blink an LED with millis(), in this post.
Recently I picked up a device called Logic from Saleae. It’s a 4-channel USB-based logic analyzer. While learning how the simple, but effective, UI works I ran some timing benchmarks on my Arduino Uno. The subject? digitalWrite(). I wanted to know how fastdigitalWrite() could turn on two (or more) pins.
Almost all Arduino users start out with the simple “blink” sketch. Turn pin 13 ON, delay, turn it OFF, and delay again. The heart of this version of “Hello World!” is the digitalWrite() function. Many Arduino users never even think about all of the stuff this single function call hides.
Found this one while going through some old files. One of my favorite volunteering activities is introducing kids to engineering. After judging a FIRST competition, I learned about Central Texas Discover Engineering. They had a program that gave me a chance to spend an entire day talking to middle school kids about engineering.
I started the class with a simple question: “What do engineers do? Surprisingly, most of the kids answered pretty well. Then I went through these slides to give them an idea of other types of engineers. Lastly, we did a fun “engineering” activity.
Getting programming questions answered on the internet can be problematic. Programmers love to have opinions, stick to those ideas, and express them to you even when their opinion has nothing to do with your question(s).
Not only am I going to explain how to use flag variables in your code, I am going to encourage their use—which most programmers avoid.
However, this advice comes with two caveats.
This information only applies to limited resource environments like an Arduino, LaunchPad or PIC.
Use flag variables very carefully when you do use them.
The following flag variable usage examples are Arduino-centric but apply to any microcontroller platform, including the Energia project for TI Launchpads.
On every page of my blog, you might notice a chat window. If I’m not busy, we can chat in real-time. If not, the messages come to me by email. Here’s one I got from Matt the other day:
Let’s talk a bit about how (and why) you would use a P-Channel MOSFET. Matt, and he’s not the only one, is probably asking this question based on the “myth” that P-Channel MOSFETs require “negative voltage” supplies.
The other day my friend called me up. He told me how much he missed building circuits and wanted to start again with the Arduino.
So he asked me “which Arduino starter kit is the best to buy?” At which point, I drew a long breath. Easy question, not always an easy answer.
Picking out an electronics kit depends on a number of factors. You should consider:
What you already have
What you want to do
#1 and #2 are probably pretty easy to figure out. For many beginners, it’s “not much” and “nothing.” When you don’t know #3, what you want to do, then it gets trickier. Coming back to my friend, what did I do? Well, I went out and bought each one of the kits in this post. I put myself in his shoes and maybe these are your shoes as well.
Back in the 1990s, a 1.44mb floppy disk was a reasonable storage size for most documents. For bigger documents or backing up an internal hard drive, other options were necessary. You might remember the Zip Drive, but that wasn’t the first large portable media.
The Bernoulli Box was the precursor to the Zip Drive. It used custom media that could store 10s to 100mbs on portable disks. Well, portable compared to carrying around an entire hard drive. Operating using the Bernoulli principal, the drive’s head never comes in contact with the “floppy” material inside the protective case.
What part is the most important part in building a project? All of them! Okay, bad joke. Selecting the right parts or components for a design is an area where both new hobbyist and veteran engineers struggle. The wide variety of devices make it almost impossible to know if you are selecting the right one.
Looking at a curated List, using component search engines and browsing DIY shops are how I tend to find parts for my projects.
Using a learning algorithm known as NEAT, this Super Mario World play through is an example of a machine learning how to beat the level on its own. Using an evolutionary process, a neural network was built–or learned–to complete the level. The name of the program used to control Mario is called… Mar-I/O.
E: Evolution of
The initial play through is fascinating as well as the breakdown of what is going on. Well worth the 5 minutes.
[Update… Ryan in the comments provided this MarI/O version modified to factor in score.]