Advertisement
Guest User

Untitled

a guest
May 21st, 2018
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.75 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 = 1;
  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( "Push Button UP" );
  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_UP: if(buttonJustPressed) {
  82. if(who == 9) life1--;
  83. } break;
  84.  
  85. case BUTTON_DOWN: if(buttonJustPressed) {
  86. if(who == 9) life1--;
  87. } break;
  88.  
  89. case BUTTON_SELECT:
  90. //SELECT is a special case, it pulses the LCD backlight off and on for demo
  91. digitalWrite( LCD_BACKLIGHT_PIN, LOW );
  92. delay( 150 );
  93. digitalWrite( LCD_BACKLIGHT_PIN, HIGH ); //leave the backlight on at exit
  94. delay( 150 );
  95.  
  96. resetcounter--;
  97. if(resetcounter < 0) {
  98. resetcounter = 9;
  99. life1 = 9;
  100.  
  101. updateLifeCounts();
  102. }
  103. break;
  104.  
  105. default: break;
  106. }
  107.  
  108. //Output the update life totals.
  109. if(buttonJustPressed || buttonJustReleased ) updateLifeCounts();
  110.  
  111. //clear the buttonJustPressed or buttonJustReleased flags, they've already done their job now.
  112. if(buttonJustPressed) buttonJustPressed = false;
  113. if(buttonJustReleased) buttonJustReleased = false;
  114. delay(20);
  115. }
  116. /*--------------------------------------------------------------------------------------
  117. ReadButtons()
  118. Detect the button pressed and return the value
  119. Uses global values buttonWas, buttonJustPressed, buttonJustReleased.
  120. --------------------------------------------------------------------------------------*/
  121. byte ReadButtons() {
  122. unsigned int buttonVoltage;
  123. byte button = BUTTON_NONE; // return no button pressed if the below checks don't write to btn
  124.  
  125. //read the button ADC pin voltage
  126. buttonVoltage = analogRead( BUTTON_ADC_PIN );
  127. //sense if the voltage falls within valid voltage windows
  128.  
  129. if( buttonVoltage >= ( UP_10BIT_ADC - BUTTONHYSTERESIS )
  130. && buttonVoltage <= ( UP_10BIT_ADC + BUTTONHYSTERESIS ) )
  131. {
  132. button = BUTTON_UP;
  133. }
  134. else if( buttonVoltage >= ( DOWN_10BIT_ADC - BUTTONHYSTERESIS )
  135. && buttonVoltage <= ( DOWN_10BIT_ADC + BUTTONHYSTERESIS ) )
  136. {
  137. button = BUTTON_DOWN;
  138. }
  139.  
  140. else if( buttonVoltage >= ( SELECT_10BIT_ADC - BUTTONHYSTERESIS )
  141. && buttonVoltage <= ( SELECT_10BIT_ADC + BUTTONHYSTERESIS ) )
  142. {
  143. button = BUTTON_SELECT;
  144. }
  145. //handle button flags for just pressed and just released events
  146. if( ( buttonWas == BUTTON_NONE ) && ( button != BUTTON_NONE ) )
  147. {
  148. //the button was just pressed, set buttonJustPressed, this can optionally be used to trigger a once-off action for a button press event
  149. //it's the duty of the receiver to clear these flags if it wants to detect a new button change event
  150. buttonJustPressed = true;
  151. buttonJustReleased = false;
  152. }
  153. if( ( buttonWas != BUTTON_NONE ) && ( button == BUTTON_NONE ) )
  154. {
  155. buttonJustPressed = false;
  156. buttonJustReleased = true;
  157. }
  158.  
  159. //save the latest button value, for change event detection next time round
  160. buttonWas = button;
  161.  
  162. return( button );
  163. }
  164.  
  165. void updateLifeCounts() {
  166. lcd.setCursor( 0, 1 );
  167. if(life1 <0 && life1 > 9) lcd.print(" ");
  168. if(life1 >= 9) lcd.print("XX"); else if(life1 < 0) lcd.print(9); else lcd.print(life1);
  169. if(who == 9) lcd.print(" "); else lcd.print(" ");
  170.  
  171. // 1234567890123456
  172. lcd.print(" ");
  173.  
  174.  
  175. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement