Guest User

Untitled

a guest
Jan 3rd, 2013
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.92 KB | None | 0 0
  1. CODIGO SHIELD LCD + BOTONES (lo encontre en la documentacion)
  2. [code]/*
  3.  Example code for the Freetronics LCD & Keypad Shield:
  4.  
  5.    http://www.freetronics.com/products/lcd-keypad-shield
  6.  
  7.  by Marc Alexander, 7 September 2011
  8.  This example code is in the public domain.
  9.  
  10.  ---------------------------------------------------------------------
  11.  
  12.  This program demonstrates button detection, LCD text/number printing,
  13.  and LCD backlight control on the Freetronics LCD & Keypad Shield, connected to an Arduino board.
  14.  
  15.  After powerup, the screen looks like this:
  16.  
  17.       ------------------
  18.       |Freetronics 16x2|
  19.       |Btn:          0 | <- This time value counts up the number of seconds since reset (overflows at 99)
  20.       ------------------
  21.  
  22.  When a button is pressed, a label appears for it:
  23.  
  24.       ------------------
  25.       |Freetronics 16x2|
  26.       |Btn:RIGHT     0 |
  27.       ------------------
  28.       Labels are LEFT, UP, DOWN, RIGHT and SELECT-FLASH.
  29.       SELECT-FLASH makes the LCD backlight flash off and on when held down.
  30.  
  31.  Pins used by LCD & Keypad Shield:
  32.  
  33.    A0: Buttons, analog input from voltage ladder
  34.    D4: LCD bit 4
  35.    D5: LCD bit 5
  36.    D6: LCD bit 6
  37.    D7: LCD bit 7
  38.    D8: LCD RS
  39.    D9: LCD E
  40.    D3: LCD Backlight (high = on, also has pullup high so default is on)
  41.  
  42.  ADC voltages for the 5 buttons on analog input pin A0:
  43.  
  44.    RIGHT:  0.00V :   0 @ 8bit ;   0 @ 10 bit
  45.    UP:     0.71V :  36 @ 8bit ; 145 @ 10 bit
  46.    DOWN:   1.61V :  82 @ 8bit ; 329 @ 10 bit
  47.    LEFT:   2.47V : 126 @ 8bit ; 505 @ 10 bit
  48.    SELECT: 3.62V : 185 @ 8bit ; 741 @ 10 bit
  49. */
  50. /*--------------------------------------------------------------------------------------
  51.  Includes
  52. --------------------------------------------------------------------------------------*/
  53. #include <LiquidCrystal.h>   // include LCD library
  54. /*--------------------------------------------------------------------------------------
  55.  Defines
  56. --------------------------------------------------------------------------------------*/
  57. // Pins in use
  58. #define BUTTON_ADC_PIN           A0  // A0 is the button ADC input
  59. #define LCD_BACKLIGHT_PIN         10  // D3 controls LCD backlight
  60. // ADC readings expected for the 5 buttons on the ADC input
  61. #define RIGHT_10BIT_ADC           0  // right
  62. #define UP_10BIT_ADC            145  // up
  63. #define DOWN_10BIT_ADC          329  // down
  64. #define LEFT_10BIT_ADC          505  // left
  65. #define SELECT_10BIT_ADC        741  // right
  66. #define BUTTONHYSTERESIS         10  // hysteresis for valid button sensing window
  67. //return values for ReadButtons()
  68. #define BUTTON_NONE               0  //
  69. #define BUTTON_RIGHT              1  //
  70. #define BUTTON_UP                 2  //
  71. #define BUTTON_DOWN               3  //
  72. #define BUTTON_LEFT               4  //
  73. #define BUTTON_SELECT             5  //
  74. //some example macros with friendly labels for LCD backlight/pin control, tested and can be swapped into the example code as you like
  75. #define LCD_BACKLIGHT_OFF()     digitalWrite( LCD_BACKLIGHT_PIN, LOW )
  76. #define LCD_BACKLIGHT_ON()      digitalWrite( LCD_BACKLIGHT_PIN, HIGH )
  77. #define LCD_BACKLIGHT(state)    { if( state ){digitalWrite( LCD_BACKLIGHT_PIN, HIGH );}else{digitalWrite( LCD_BACKLIGHT_PIN, LOW );} }
  78. /*--------------------------------------------------------------------------------------
  79.  Variables
  80. --------------------------------------------------------------------------------------*/
  81. byte buttonJustPressed  = false;         //this will be true after a ReadButtons() call if triggered
  82. byte buttonJustReleased = false;         //this will be true after a ReadButtons() call if triggered
  83. byte buttonWas          = BUTTON_NONE;   //used by ReadButtons() for detection of button events
  84. /*--------------------------------------------------------------------------------------
  85.  Init the LCD library with the LCD pins to be used
  86. --------------------------------------------------------------------------------------*/
  87. 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 )
  88. /*--------------------------------------------------------------------------------------
  89.  setup()
  90.  Called by the Arduino framework once, before the main loop begins
  91. --------------------------------------------------------------------------------------*/
  92. void setup()
  93. {
  94.   //button adc input
  95.   pinMode( BUTTON_ADC_PIN, INPUT );         //ensure A0 is an input
  96.   digitalWrite( BUTTON_ADC_PIN, LOW );      //ensure pullup is off on A0
  97.   //lcd backlight control
  98.   digitalWrite( LCD_BACKLIGHT_PIN, HIGH );  //backlight control pin D3 is high (on)
  99.   pinMode( LCD_BACKLIGHT_PIN, OUTPUT );     //D3 is an output
  100.   //set up the LCD number of columns and rows:
  101.   lcd.begin( 16, 2 );
  102.   //Print some initial text to the LCD.
  103.   lcd.setCursor( 0, 0 );   //top left
  104.   //          1234567890123456
  105.   lcd.print( "Freetronics 16x2" );
  106.   //
  107.   lcd.setCursor( 0, 1 );   //bottom left
  108.   //          1234567890123456
  109.   lcd.print( "Btn:" );
  110. }
  111. /*--------------------------------------------------------------------------------------
  112.  loop()
  113.  Arduino main loop
  114. --------------------------------------------------------------------------------------*/
  115. void loop()
  116. {
  117.   byte button;
  118.   byte timestamp;
  119.  
  120.   //get the latest button pressed, also the buttonJustPressed, buttonJustReleased flags
  121.   button = ReadButtons();
  122.   //blank the demo text line if a new button is pressed or released, ready for a new label to be written
  123.   if( buttonJustPressed || buttonJustReleased )
  124.   {
  125.     lcd.setCursor( 4, 1 );
  126.     lcd.print( "            " );
  127.   }
  128.   //show text label for the button pressed
  129.   switch( button )
  130.   {
  131.      case BUTTON_NONE:
  132.      {
  133.         break;
  134.      }
  135.      case BUTTON_RIGHT:
  136.      {
  137.         lcd.setCursor( 4, 1 );
  138.         lcd.print( "RIGHT" );
  139.         break;
  140.      }
  141.      case BUTTON_UP:
  142.      {
  143.         lcd.setCursor( 4, 1 );
  144.         lcd.print( "UP" );
  145.         break;
  146.      }
  147.      case BUTTON_DOWN:
  148.      {
  149.         lcd.setCursor( 4, 1 );
  150.         lcd.print( "DOWN" );
  151.         break;
  152.      }
  153.      case BUTTON_LEFT:
  154.      {
  155.        lcd.setCursor( 4, 1 );
  156.        lcd.print( "LEFT" );
  157.        break;
  158.     }
  159.     case BUTTON_SELECT:
  160.     {
  161.        lcd.setCursor( 4, 1 );
  162.        lcd.print( "SELECT-FLASH" );  
  163.      
  164.        //SELECT is a special case, it pulses the LCD backlight off and on for demo
  165.        digitalWrite( LCD_BACKLIGHT_PIN, LOW );
  166.        delay( 150 );
  167.        digitalWrite( LCD_BACKLIGHT_PIN, HIGH );   //leave the backlight on at exit
  168.        delay( 150 );
  169.      
  170.        /* an example of LCD backlight control via macros with nice labels
  171.        LCD_BACKLIGHT_OFF();
  172.        delay( 150 );
  173.        LCD_BACKLIGHT_ON();   //leave the backlight on at exit
  174.        delay( 150 );
  175.        */
  176.      
  177.        /*
  178.        // an example of LCD backlight control via a macro with nice label, called with a value
  179.        LCD_BACKLIGHT(false);
  180.        delay( 150 );
  181.        LCD_BACKLIGHT(true);   //leave the backlight on at exit
  182.        delay( 150 );
  183.        */
  184.      
  185.        break;
  186.      }
  187.      default:
  188.     {
  189.        break;
  190.     }
  191.   }
  192.   // print the number of seconds since reset (two digits only)
  193.   timestamp = ( (millis() / 1000) % 100 );   //"% 100" is the remainder of a divide-by-100, which keeps the value as 0-99 even as the result goes over 100
  194.   lcd.setCursor( 14, 1 );
  195.   if( timestamp <= 9 )
  196.      lcd.print( " " );   //quick trick to right-justify this 2 digit value when it's a single digit
  197.   lcd.print( timestamp, DEC );
  198. /*
  199.   //debug/test display of the adc reading for the button input voltage pin.
  200.   lcd.setCursor(12, 0);
  201.   lcd.print( "    " );          //quick hack to blank over default left-justification from lcd.print()
  202.   lcd.setCursor(12, 0);         //note the value will be flickering/faint on the LCD
  203.   lcd.print( analogRead( BUTTON_ADC_PIN ) );
  204. */
  205.   //clear the buttonJustPressed or buttonJustReleased flags, they've already done their job now.
  206.   if( buttonJustPressed )
  207.      buttonJustPressed = false;
  208.   if( buttonJustReleased )
  209.      buttonJustReleased = false;
  210. }
  211. /*--------------------------------------------------------------------------------------
  212.  ReadButtons()
  213.  Detect the button pressed and return the value
  214.  Uses global values buttonWas, buttonJustPressed, buttonJustReleased.
  215. --------------------------------------------------------------------------------------*/
  216. byte ReadButtons()
  217. {
  218.   unsigned int buttonVoltage;
  219.   byte button = BUTTON_NONE;   // return no button pressed if the below checks don't write to btn
  220.  
  221.   //read the button ADC pin voltage
  222.   buttonVoltage = analogRead( BUTTON_ADC_PIN );
  223.   //sense if the voltage falls within valid voltage windows
  224.   if( buttonVoltage < ( RIGHT_10BIT_ADC + BUTTONHYSTERESIS ) )
  225.   {
  226.      button = BUTTON_RIGHT;
  227.   }
  228.   else if(   buttonVoltage >= ( UP_10BIT_ADC - BUTTONHYSTERESIS )
  229.           && buttonVoltage <= ( UP_10BIT_ADC + BUTTONHYSTERESIS ) )
  230.   {
  231.      button = BUTTON_UP;
  232.   }
  233.   else if(   buttonVoltage >= ( DOWN_10BIT_ADC - BUTTONHYSTERESIS )
  234.           && buttonVoltage <= ( DOWN_10BIT_ADC + BUTTONHYSTERESIS ) )
  235.   {
  236.      button = BUTTON_DOWN;
  237.   }
  238.   else if(   buttonVoltage >= ( LEFT_10BIT_ADC - BUTTONHYSTERESIS )
  239.           && buttonVoltage <= ( LEFT_10BIT_ADC + BUTTONHYSTERESIS ) )
  240.   {
  241.      button = BUTTON_LEFT;
  242.   }
  243.   else if(   buttonVoltage >= ( SELECT_10BIT_ADC - BUTTONHYSTERESIS )
  244.           && buttonVoltage <= ( SELECT_10BIT_ADC + BUTTONHYSTERESIS ) )
  245.   {
  246.      button = BUTTON_SELECT;
  247.   }
  248.   //handle button flags for just pressed and just released events
  249.   if( ( buttonWas == BUTTON_NONE ) && ( button != BUTTON_NONE ) )
  250.   {
  251.      //the button was just pressed, set buttonJustPressed, this can optionally be used to trigger a once-off action for a button press event
  252.      //it's the duty of the receiver to clear these flags if it wants to detect a new button change event
  253.      buttonJustPressed  = true;
  254.      buttonJustReleased = false;
  255.   }
  256.   if( ( buttonWas != BUTTON_NONE ) && ( button == BUTTON_NONE ) )
  257.   {
  258.      buttonJustPressed  = false;
  259.      buttonJustReleased = true;
  260.   }
  261.  
  262.   //save the latest button value, for change event detection next time round
  263.   buttonWas = button;
  264.  
  265.   return( button );
  266. }
  267. [/code]
Advertisement
Add Comment
Please, Sign In to add comment