Technological MacGyvering

Midterm, Nov. 2, 2021

When done, show me your working circuit and email me your code.

Always make sure you have a solid version of a step before tackling the next step. I would rather see a working version of just Steps 1 and 2 than a broken version of Step 3 or Step 4.

Build a Digital Counter with LED Display and Reset Button

Step 1 — A Counter

Just write a program that counts from 0 to 255. Delay by 500 milliseconds at each count. Check that your counter is working by setting up a Serial monitor and logging the counter.

When your counter gets to 255, instead of going to 256, arrange that it goes back to 0 and starts counting again.

There is no need to do any wiring for this step! It’s just a bit of C code.

Step 2 — Wiring the LED Display

Set up 8 LEDs protected with 8 current-limiting 220Ω resistors. Connect them to Arduino pins 2 through 9. Check using digitalWrite that that they can all be turned on and all be turned off before proceeding to the next step.

Digital Counter Schematic

Step 3 — Code to Control the LEDs

Here is how to calculate the ith power of 2 in C:

int ith_power_of_2 = 1 << i;

It’s a bit of magic that uses the “left shift” operator <<.

Here is how to determine whether the LED representing the ith binary digit should be lit:

int LED_is_lit = counter & ith_power_of_2;

It’s another bit of magic that uses the “bitwise and” operator &.

Use these two bits of magic in your code that decides which LEDs should be lit. Your code will have a loop involving i that will go from 0 through 7 to pull out the 8 binary digits.

Step 4 — Only start Step 4 if you get Step 3 done!

Add a reset switch connected to Arduino Pin 10 so that you can reset to 0 whenever you like. You will need a push-button switch, a 10kΩ pull-down resistor, and a few more jumpers.

EXTRA CREDIT

If your code completely works, and you have emailed it to me, and you are looking for yet more to do….

Your code has a delay(500) in it. Can you re-write the code to have the same behavior but not use a delay? The benefit is that you won’t have to hold the switch down for a whole half-second to be guaranteed of getting a reset.