Guest User

Arduino & Freetronics LCD - Magic:The Gathering Life Counter

a guest
Aug 5th, 2012
382
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.48 KB | None | 0 0
  1. /* Test the freetronics 16x2 LCD shield
  2. http://www.freetronics.com/pages/16x2-lcd-shield-quickstart-guide#.UBKMm7T9OK9
  3.  
  4. MTG Life Counter
  5.  
  6. */
  7. /*--------------------------------------------------------------------------------------
  8.   Includes
  9. --------------------------------------------------------------------------------------*/
  10. #include <LiquidCrystal.h>   // include LCD library
  11. /*--------------------------------------------------------------------------------------
  12.   Defines
  13. --------------------------------------------------------------------------------------*/
  14. // Pins in use
  15. #define BUTTON_ADC_PIN           A0  // A0 is the button ADC input
  16. #define LCD_BACKLIGHT_PIN         3  // D3 controls LCD backlight
  17. // ADC readings expected for the 5 buttons on the ADC input
  18. #define RIGHT_10BIT_ADC           0  // right
  19. #define UP_10BIT_ADC            145  // up
  20. #define DOWN_10BIT_ADC          329  // down
  21. #define LEFT_10BIT_ADC          505  // left
  22. #define SELECT_10BIT_ADC        741  // right
  23. #define BUTTONHYSTERESIS         10  // hysteresis for valid button sensing window
  24. //return values for ReadButtons()
  25. #define BUTTON_NONE               0  //
  26. #define BUTTON_RIGHT              1  //
  27. #define BUTTON_UP                 2  //
  28. #define BUTTON_DOWN               3  //
  29. #define BUTTON_LEFT               4  //
  30. #define BUTTON_SELECT             5  //
  31. /*--------------------------------------------------------------------------------------
  32.   Variables
  33. --------------------------------------------------------------------------------------*/
  34. byte buttonJustPressed  = false;         //this will be true after a ReadButtons() call if triggered
  35. byte buttonJustReleased = false;         //this will be true after a ReadButtons() call if triggered
  36. byte buttonWas          = BUTTON_NONE;   //used by ReadButtons() for detection of button events
  37.  
  38. int life1 = 20;
  39. int life2 = 20;
  40. int who = 0;
  41. int resetcounter = 0;
  42. /*--------------------------------------------------------------------------------------
  43.   Init the LCD library with the LCD pins to be used
  44. --------------------------------------------------------------------------------------*/
  45. LiquidCrystal lcd( 8, 9, 4, 5, 6, 7 );   //Pins for the freetronics 16x2 LCD shield. LCD: ( RS, E, LCD-D4, LCD-D5, LCD-D6, LCD-D7 )
  46. /*--------------------------------------------------------------------------------------
  47.   setup()
  48.   Called by the Arduino framework once, before the main loop begins
  49. --------------------------------------------------------------------------------------*/
  50. void setup()
  51. {
  52.    //button adc input
  53.    pinMode( BUTTON_ADC_PIN, INPUT );         //ensure A0 is an input
  54.    digitalWrite( BUTTON_ADC_PIN, LOW );      //ensure pullup is off on A0
  55.    //lcd backlight control
  56.    digitalWrite( LCD_BACKLIGHT_PIN, HIGH );  //backlight control pin D3 is high (on)
  57.    pinMode( LCD_BACKLIGHT_PIN, OUTPUT );     //D3 is an output
  58.    //set up the LCD number of columns and rows:
  59.    lcd.begin( 16, 2 );
  60.    //Print some initial text to the LCD.
  61.    lcd.setCursor( 0, 0 );   //top left
  62.    //          1234567890123456
  63.    lcd.print( "MTG Life Counter" );
  64.    updateLifeCounts();
  65. }
  66. /*--------------------------------------------------------------------------------------
  67.   loop()
  68.   Arduino main loop
  69. --------------------------------------------------------------------------------------*/
  70. void loop()
  71. {
  72.    byte button;
  73.    
  74.    button = ReadButtons();
  75.    //show text label for the button pressed
  76.    switch(button) {
  77.      case BUTTON_NONE:
  78.        resetcounter = 0;
  79.        break;
  80.  
  81.      case BUTTON_LEFT: if(buttonJustPressed) {
  82.          who = 0;
  83.        } break;
  84.  
  85.      case BUTTON_RIGHT: if(buttonJustPressed) {
  86.          who = 1;
  87.        } break;
  88.  
  89.      case BUTTON_UP: if(buttonJustPressed) {
  90.          if(who == 0) life1++; else life2++;
  91.        } break;
  92.  
  93.      case BUTTON_DOWN: if(buttonJustPressed) {
  94.          if(who == 0) life1--; else life2--;
  95.        } break;
  96.  
  97.      case BUTTON_SELECT:
  98.        //SELECT is a special case, it pulses the LCD backlight off and on for demo
  99.        digitalWrite( LCD_BACKLIGHT_PIN, LOW );
  100.        delay( 150 );
  101.        digitalWrite( LCD_BACKLIGHT_PIN, HIGH );   //leave the backlight on at exit
  102.        delay( 150 );
  103.        
  104.        resetcounter++;
  105.        if(resetcounter > 10) {
  106.          resetcounter = 0;
  107.          life1 = 20;
  108.          life2 = 20;
  109.          updateLifeCounts();
  110.        }
  111.        break;
  112.        
  113.       default: break;
  114.    }
  115.  
  116.    //Output the update life totals.
  117.    if(buttonJustPressed || buttonJustReleased ) updateLifeCounts();
  118.  
  119.    //clear the buttonJustPressed or buttonJustReleased flags, they've already done their job now.
  120.    if(buttonJustPressed)  buttonJustPressed  = false;
  121.    if(buttonJustReleased) buttonJustReleased = false;
  122.    delay(20);
  123. }
  124. /*--------------------------------------------------------------------------------------
  125.   ReadButtons()
  126.   Detect the button pressed and return the value
  127.   Uses global values buttonWas, buttonJustPressed, buttonJustReleased.
  128. --------------------------------------------------------------------------------------*/
  129. byte ReadButtons() {
  130.    unsigned int buttonVoltage;
  131.    byte button = BUTTON_NONE;   // return no button pressed if the below checks don't write to btn
  132.    
  133.    //read the button ADC pin voltage
  134.    buttonVoltage = analogRead( BUTTON_ADC_PIN );
  135.    //sense if the voltage falls within valid voltage windows
  136.    if( buttonVoltage < ( RIGHT_10BIT_ADC + BUTTONHYSTERESIS ) )
  137.    {
  138.       button = BUTTON_RIGHT;
  139.    }
  140.    else if(   buttonVoltage >= ( UP_10BIT_ADC - BUTTONHYSTERESIS )
  141.            && buttonVoltage <= ( UP_10BIT_ADC + BUTTONHYSTERESIS ) )
  142.    {
  143.       button = BUTTON_UP;
  144.    }
  145.    else if(   buttonVoltage >= ( DOWN_10BIT_ADC - BUTTONHYSTERESIS )
  146.            && buttonVoltage <= ( DOWN_10BIT_ADC + BUTTONHYSTERESIS ) )
  147.    {
  148.       button = BUTTON_DOWN;
  149.    }
  150.    else if(   buttonVoltage >= ( LEFT_10BIT_ADC - BUTTONHYSTERESIS )
  151.            && buttonVoltage <= ( LEFT_10BIT_ADC + BUTTONHYSTERESIS ) )
  152.    {
  153.       button = BUTTON_LEFT;
  154.    }
  155.    else if(   buttonVoltage >= ( SELECT_10BIT_ADC - BUTTONHYSTERESIS )
  156.            && buttonVoltage <= ( SELECT_10BIT_ADC + BUTTONHYSTERESIS ) )
  157.    {
  158.       button = BUTTON_SELECT;
  159.    }
  160.    //handle button flags for just pressed and just released events
  161.    if( ( buttonWas == BUTTON_NONE ) && ( button != BUTTON_NONE ) )
  162.    {
  163.       //the button was just pressed, set buttonJustPressed, this can optionally be used to trigger a once-off action for a button press event
  164.       //it's the duty of the receiver to clear these flags if it wants to detect a new button change event
  165.       buttonJustPressed  = true;
  166.       buttonJustReleased = false;
  167.    }
  168.    if( ( buttonWas != BUTTON_NONE ) && ( button == BUTTON_NONE ) )
  169.    {
  170.       buttonJustPressed  = false;
  171.       buttonJustReleased = true;
  172.    }
  173.    
  174.    //save the latest button value, for change event detection next time round
  175.    buttonWas = button;
  176.    
  177.    return( button );
  178. }
  179.  
  180. void updateLifeCounts() {
  181.   lcd.setCursor( 0, 1 );
  182.   if(life1 < 10 && life1 > 0) lcd.print(" ");
  183.   if(life1 <= 0) lcd.print("XX"); else if(life1 > 99) lcd.print("99"); else lcd.print(life1);
  184.   if(who == 0) lcd.print("*"); else lcd.print(" ");
  185.  
  186.    //        1234567890123456
  187.   lcd.print("         ");
  188.  
  189.   lcd.setCursor( 13, 1 );
  190.   if(who == 1) lcd.print("*"); else lcd.print(" ");
  191.   if(life2 < 10 && life2 > 0) lcd.print(" ");
  192.   if(life2 <= 0) lcd.print("XX"); else if(life2 > 99) lcd.print("99"); else lcd.print(life2);
  193. }
Add Comment
Please, Sign In to add comment