Guest User

Arduino & Freetronics LCD - Magic:The Gathering Life Counter

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