Project 3 - V.E.G.A.S

From UCLA IEEE OPS
Jump to: navigation, search

Very
Eccentric
Gadget
Activates
Shenanigans

(Acronym courtesy of Justin "is" Young and Daniel "Dantastic" "Grandmaster" Zhang)

In this project we will be using a microcontroller(MCU) to create a slot machine game.

The microcontroller will cycle the RGB's through their colors and when a button is pressed one of the colors will be locked in. When all three buttons hold the same color the game is won, else you lose.

Dates

Assigned: 11/21/14
Due: 12/15/14 (beginning of finals week)

Purpose

The purpose of this project is to introduce you to microcontrolling programming. Microcontrollers are small computers that can be programmed to do almost anything. For this project the circuit will be very straightforward, the main focus of this project is to get you programming the MCU to control the circuit.

V.E.G.A.S. Schematic

Parts for the Project

  • Teensy 3.1 Microcontroller
  • 3 RedBlueGreen LED's (RGB LED)
  • 1 Pushbutton
  • 2 Resistors
    • 1 Current limiting resistor
    • 1 Pull-down resistor

Grading Criteria

  1. Cycles through at least three colors
  2. Button locks in a current color
  3. When all three colors are locked in and the same color, program should recognize that and initialize winning sequence
  4. When all three colors are locked in and not the same, there should be a losing sequence that plays

OPTIONAL

  1. Play around with color combinations
  2. Potentially add speaker to the game
  3. check out analogWrite function (will allow you to fade colors using PWM)
  4. Solder circuit DO NOT SOLDER TEENSY DIRECTLY TO BOARD

Functions You'll Be Using

  • pinMode(pin, mode)
    • pin - the pin you want to set the mode of
    • mode - the mode you want to set that pin to. Can be either INPUT or OUTPUT
  • digitalWrite(pin, value)
    • pin - the pin you want to set the value of
    • value - what you want to set that pin to. Can be either HIGH or LOW
  • delay(time)
    • time - the length you want to delay. Measured in milliseconds
  • attachInterrupt(pin, function, mode)
    • pin - the pin you want to use as an input
    • function - the function you want to call every time the input is triggered
    • mode - how you want your interrupt to be triggered. For this project, we recommend this is FALLING


Pseudocode

 //First we define all our pin numbers
 #define all LED pins
 #define button pin
 
 //Definitions for variables we will use
 int state
 int light1
 int instantLight1
 int light2
 int instantLight2
 int light3
 int instantLight3 
 
 //Define our functions that will be used in the “Main” loop
 
 //This function will be called every time the button is pressed
 void switchState()
 {
   if state == 1
     instantLight1 = light1
   else if state == 2
     instantLight2 = light2
   else if state == 3
     instantLight3 = light3
   state = state + 1
   if state > 4
     state = 1
 }
 
 
 //This function is to turn off all LEDs
 void allOff()
 {
   turn each LED off, one by one
 }
 
 
 //This function is to cycle through all three lights
 void cycleAll()
 {
   set one color on each RGB LED on
   delay a small time interval
   allOff()
   set another color on each LED on
   delay a small time interval
   allOff()
   set another color on each LED on
   delay a small time interval
   allOff()
 }
 
 
 //This function is to cycle through just the last two lights
 void cycle2()
 {
   set the first LED to instantLight1
   set one color on the two remaining LEDs on
   delay a small amount
   turn the two LEDs off
   set another color on the two remaining LEDs on
   delay a small amount
   turn the two LEDs off
   set another color on the two remaining LEDs on
   delay a small amount
   turn the two LEDs off
 }
 
 
 //This function will just cycle the last LED
 void cycle1()
 {
   set the second LED to instantLight2
   set the last LED to a color
   delay a small amount
   turn the last LED off
   set the last LED to a different color
   delay a small amount
   turn the last LED off
   set the last LED to a different color
   delay a small amount
   turn the last LED off
 }
 
 //Setup and Main
  
 //This is where you setup your code
 void setup()
 {
   set pinMode for every pin
   attachInterrupt to your button
 }
 
 
 //This is essentially our "Main" function
 void loop()
 {	
   while state = 1
     cycleAll()
   while state = 2
     cycle2()
   while state == 3
     cycle1()
   Keep all three lights on for another second so user can see what they got
   if all three lights are the same color
     while (1)
       set all three lights to green
       delay a short amount
       turn all three lights off
       delay a short amount
   else
     while (1)
       set all three lights to red
       delay a short amount
       turn all three lights off
       delay a short amount

FAQ

Teensy 3.1 (Front)

How do I know what number corresponds to each pin?

Please refer to this diagram for the pin names and capabilities of the Teensy 3.1 pinout.