Monday, September 17, 2012

Sept 17-Digital I/O


Introduction:
For this week’s homework project, my partner and I decided to recreate the Pirate Isle game that I had to program in Racket last semester for CSC 120. It is a simple game that requires two inputs and seven outputs.  At first I was intimidated as I tried to think through what all was going to be involved, remembering the painful hours I spent last semester trying to hammer out exactly how to make the game work, but once we got started, it was a lot easier to break it all down and accomplish our task.

There were definitely more than a few moments where I felt like this,

But after fiddling with all of the hardware and debugging the code, everything came together and works exactly as intended.

Procedure:

The Game

This is what the game map looked like in Racket.

The user began on Pirates’ Island and tried to navigate to Treasure Island by pressing either the “A” or “B” on the keyboard.  The green circle would move to indicate which island you were currently on.
Rather than just using an image, we decided to use LEDs to represent Islands and Pushbuttons instead of a keyboard.  The game consists of 2 buttons and 7 islands. There is a set path that the LEDs will follow based on which button is pushed. The user will have to discover the path to make it to treasure isle.  Rather than a moving green circle, the currently occupied island will be indicated by the LED being turned on. Once the game is completed, the LEDs will trace the path that the user followed and flash the Treasure Island LED

Programming and Circuitry

Circuitry was definitely our problem child with this project. It seemed like as soon as we got part of the circuit working, another part would stop. Pushbuttons seemed to be the root of all of our problems.
Finally, after moving around wires and trying several different layouts, we managed to get the LED to turn on when the button was pressed and turn off when the button wasn’t pressed. 
Next, we decided to try to get the button to turn on with the press of the button and stay on until the next press of the button, allowing the pushbutton to act like a light switch. At first it seemed that we had figured it all out, but the light would turn on or off erratically, or dim instead of turning off. After reading some documentation, we realized this was caused by “debouncing.” This is from an extra signal being sent to the Arduino because the contacts in the pushbutton bounce. Basically, when the button is pushed, it makes contact twice instead of once. While this happens in less than a second, the microcontroller is able to read the input even faster. So to fix this issue, we added a delay in our code so that the microcontroller would only react to the button push after half of a second. This eliminated the debouncing and allowed our light to work properly.
The next thing we wanted to do was connect a second LED so that we could start writing the program for multiple LEDs. We decided to write the program to have different states. State 0 was when both LEDs were turned on, State 1 was when the red LED was on and green LED was off, State 2 was when the red LED was turned off and the green LED was turned on, and the final state was the same as the initial-where both LEDs were turned off. We designed the program to change between states on the press of the button.

Once that was working properly, we redesigned the code for the specific needs of our project. Keeping in mind the idea of states, we designed if statements for each of the buttons to determine which LED should be lit up using our predetermined path. For example:


else if(button == 2){
    if(state == 7)
      {state = 8;}     


This tells the program that if the board is in state 7 (read: LED 7 is lit up and the rest are turned off) and button two is pressed, change to state 8 (where LED 8 is lit up and none of the others are). By having an if statement for each of the possible states (since the LEDs were plugged in to pins 7-13, states 7-13), we were easily able to tell the program which light should be on. To make sure that the other lights got turned off, we used another simple function.


void turnAllOff(){
 for (int i = 7 ; i < 14; i++)
  {digitalWrite (i, LOW);}


This walks through and writes each LED pin, starting at 7, and rewrites them at low (or, in simple terms, turns them off).

We decided the next step was to plug up all of the wires and both buttons to make sure that they all worked and to test our program. We encountered a few basic errors and made minor mistakes such as plugging an LED to a wrong pin, etc. but the majority of the process went smoothly.

Once all of that was successfully working, we decided to attack design-how to make the game look pretty. First, I cut a piece of foam board to the size I wanted the game to be, and printed an image of the island map. After hot gluing the map onto the board, I punched small holes in the middle of each island for the LEDs and cut two holes near the bottom for the pushbuttons to make a face plate. Then, I took two more pieces of foam board and made legs for the plate so that it would be suspended above our breadboard. I then took another piece of board and glued it to the bottom, making a 2-sided box so that the game would be neat, clean, and easy to transport, but so that it was still easy to see the wiring inside.
Next, we had to solder wires to each of the LEDs and to each of the pushbuttons so that they could be pushed into the faceplate without being too far from the breadboard. Soldering seemed much easier this time around and all of that went smoothly.
Now that all of the coding was taken care of, the hardware was prepared, and all of the exterior design was done, it was just a matter of plugging everything up. We pushed the LEDs and pushbuttons through the holes we had made in the map face plate and plugged in all of the wires.
Voila! The finished product.


Conclusion:
In the end, I learned a lot about circuitry as well as programming in C. It was really neat to go from being completely lost and confused on Thursday to halfway understanding the concepts over the weekend and then gaining mastery of everything and seeing it come together on Monday. I really enjoyed being able to do this project and can’t wait to do another! 


No comments:

Post a Comment