Corning Community College
ENGR1050 C for Engineers
Revisiting our LED binary counter (with expanded capacity), this time with buttons to control the count (up/down by one), and optimizing our LED lighting logic with a loop to cut down on the individual position if statements needed.
Do note, the productive way to go about this project involves taking the following steps:
If you start too late, and do not ask questions, and do not have enough time and don't know what is going on, you are not doing the project correctly.
Taking our knowledge of LEDs and buttons:
To assist with consistency across all implementations, data files for use with this project are available on lab46 via the grabit tool. Be sure to obtain it and ensure your implementation properly works with the provided data.
lab46:~/src/SEMESTER/DESIG$ grabit DESIG PROJECT
You will want to go here to edit and fill in the various sections of the document:
After you've made a directory for ptb1 on lab46, you can use the grabit command to acquire the source code that we'll be modifying in this project, like so:
grabit c4eng ptb1
This will populate the ptb1 directory with all source files that you'll need for this project.
It's important to note, that while you'll be grabbing the source files via lab46, the required dependencies will not be available there and you will have to retrieve these files from your pi.
You can push these source files from your repository using the following set of commands:
hg add * hg commit -m "YOUR COMMIT MESSAGE HERE" hg push
Assuming you've encountered no errors, you should now be able to retrieve these files on your pi:
hg pull hg update
The LED bar is a rather simplified version of putting 10 LEDs on your breadboard. The LED bar contains 10 different LEDs that require their own GPIO pins and resistors. Each gpio pin needs to be set to output mode. You can connect your resistors from one leg of the LED directly to the “-” column on the side of the bread-board to save some space. If your LED bar is not working, you can try flipping it around because the direction of the flow matters, just like regular LEDs.
each button should be wired the same way, just to different pins. each button needs a 3.3v ran through a 10Kohm resistor and a ground wire, with a final wire with a 10kohm resistor to a gpio pin. Within wiringpi, the pin must be set to input mode. The gpio wire should also be on the opposite side of the 3.3v wire and the ground wire. You made need to switch which pin your wires are connected to if your button is not working properly. You can also check if the button is working by looking at the gpio readall table before and after the button is being pressed, and you should see a change in voltage for that gpio pin (reminder to change voltage first gpio mode “pin” out, then gpio write “pin” 1).
Synopsis: To achieve our desired outcome with only one if statement, we'll need to implement bit shifting and a for loop. It would also be helpful, first, to create an array if our pins are not connected sequentially.
To declare an array in C is similar to declaring any variable: we declare the data type (in arrays, the data type of all the elements), name the variable, and assign a value (or a matrix of values for arrays).
int myArray[10] = {5,2,22,6,...};
In our sample, 10 is the number of elements we have (not shown).
The general flow of the process (one way of going about it, anyway) can be described as follows:
SET COUNT TO ZERO REPEAT INFINITELY: SHOULD THE INCREMENT BUTTON BE PRESSED: INCREMENT COUNT BY ONE SHOULD THE DECREMENT BUTTON BE PRESSED: DECREMENT COUNT BY ONE BIT POSITION IS ONE LED OFFSET IS ZERO REPEAT AS LONG AS LED OFFSET IS LESS THAN TEN: SHOULD THE CURRENT BIT POSITION HAVE A ONE: ACTIVATE THE LED AT CURRENT OFFSET OTHERWISE: DEACTIVATE THE LED AT CURRENT OFFSET LEFT SHIFT BIT POSITION BY ONE LED OFFSET IS INCREMENTED BY ONE COMMENT: INNER REPEAT CONCLUDES DELAY AT LEAST FIFTY MILLISECONDS COMMENT: INFINITE REPEAT BLOCK CONCLUDES
To be successful in this project, the following criteria (or their equivalent) must be met:
Let's say you have completed work on the project, and are ready to submit, you would do the following (assuming you have a program called uom0.c):
lab46:~/src/SEMESTER/DESIG/PROJECT$ make submit
You should get some sort of confirmation indicating successful submission if all went according to plan. If not, check for typos and or locational mismatches.
I'll be evaluating the project based on the following criteria:
52:ptb1:final tally of results (52/52) *:ptb1:used grabit to obtain project by the Sunday prior to duedate [5/5] *:ptb1:clean compile, no compiler messages [5/5] *:ptb1:each button hooked to unique GPIOs [5/5] *:ptb1:button state is read, on press code adjusts count [6/6] *:ptb1:binary value of the count correctly displayed on LED bar [6/6] *:ptb1:LED display logic optimized with a loop containing one if [20/20] *:ptb1:code tracked in lab46 semester repo [5/5]