■ FIGURE 2. ATmega328 port pin mapping.
is brighter and shinier than LEDs? (Okay, the sun, but who
has time to go outdoors when there is so much neat stuff
to learn?). Let’s design some chaser lights with eight LEDs
that allow us to use an eight-pin DIP switch to select 16
sweep patterns, eight speeds, and polarity (more on that
later). You can see what this looks like in Figure 1, and I’m
sorry for the rat’s nest of wires. The ATmega328 pins in
Figure 2 and the schematic in Figure 3 will guide you in
creating your own rat’s nest.
Getting Bit Fields From a DIP Switch
We will let the top four switches encode 16
patterns, the next lower three can encode eight speeds,
and the lowest switch can encode the polarity. In order to
look at each of these parameters individually (while
ignoring the others), we use bit masks which are sets of
bits set to 1 that we AND ‘&’ with the byte so that we
exclude the other bits. This is best explained with an
// DIP switch masks
We have eight patterns and we can encode them with
four switches, as follows:
■ FIGURE 3. ATmega328 pins for DIP switch and LED.