Advertisement
LXL15

Volleyball Scoreboard Sketch Arduino

Dec 15th, 2013
380
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 22.69 KB | None | 0 0
  1. /*--------------------------------------------------------------------------------------
  2.   Includes
  3. --------------------------------------------------------------------------------------*/
  4. #include <Wire.h>
  5. #include <LiquidCrystal.h>   // include LCD library
  6. #include <SdFat.h>
  7.  
  8. /*--------------------------------------------------------------------------------------
  9.   Defines
  10. --------------------------------------------------------------------------------------*/
  11. // Pins in use
  12. #define BUTTON_ADC_PIN           A0  // A0 is the button ADC input
  13. #define LCD_BACKLIGHT_PIN        A1  // A1 controls LCD backlight
  14. // ADC readings expected for the 5 buttons on the ADC input
  15. #define RIGHT_10BIT_ADC           0  // right
  16. #define UP_10BIT_ADC            208  // up
  17. #define DOWN_10BIT_ADC          410  // down
  18. #define LEFT_10BIT_ADC          626  // left
  19. #define SELECT_10BIT_ADC        819  // select
  20. #define BUTTONHYSTERESIS         5  // hysteresis for valid button sensing window
  21. //return values for ReadButtons()
  22. #define BUTTON_NONE               0  //
  23. #define BUTTON_RIGHT              1  //
  24. #define BUTTON_UP                 2  //
  25. #define BUTTON_DOWN               3  //
  26. #define BUTTON_LEFT               4  //
  27. #define BUTTON_SELECT             5  //
  28. //some example macros with friendly labels for LCD backlight/pin control, tested and can be swapped into the example code as you like
  29. #define LCD_BACKLIGHT_OFF()     digitalWrite( LCD_BACKLIGHT_PIN, LOW )
  30. #define LCD_BACKLIGHT_ON()      digitalWrite( LCD_BACKLIGHT_PIN, HIGH )
  31. #define LCD_BACKLIGHT(state)    { if( state ){digitalWrite( LCD_BACKLIGHT_PIN, HIGH );}else{digitalWrite( LCD_BACKLIGHT_PIN, LOW );} }
  32.  
  33. /*--------------------------------------------------------------------------------------
  34.   Variables
  35. --------------------------------------------------------------------------------------*/
  36. byte buttonJustPressed  = false;         //this will be true after a ReadButtons() call if triggered
  37. byte buttonJustReleased = false;         //this will be true after a ReadButtons() call if triggered
  38. byte button             = BUTTON_NONE;
  39. byte buttonWas          = BUTTON_NONE;   //used by ReadButtons() for detection of button events
  40. byte buttonWas2         = BUTTON_NONE;
  41. int top_score           = 0;              //Value for top team's score
  42. int bot_score           = 0;              //Value for bottom team's score
  43. int top_set             = 0;              //Value for top team's sets
  44. int bot_set             = 0;              //Value for bottom team's sets
  45. int line_num            = 1;
  46. int team_num            = 1;
  47. int file_len            = 1;
  48. int file_size;        
  49. int counter[30];
  50. char top_set_char[2];
  51. char bot_set_char[2];
  52. char top_score_char[3];
  53. char bot_score_char[3];
  54. char teams[1];
  55. Sd2Card  card;
  56. SdVolume volume;
  57. SdFile   root;
  58. SdFile   file;
  59.  
  60.  
  61. /*--------------------------------------------------------------------------------------
  62.   Init the LCD library with the LCD pins to be used and DMD scan
  63. --------------------------------------------------------------------------------------*/
  64. LiquidCrystal lcd( 4, 5, 0, 1, 2, 3 );   //Pins for the freetronics 16x2 LCD shield. LCD: ( RS, E, LCD-D4, LCD-D5, LCD-D6, LCD-D7 )
  65.  
  66. #include "SPI.h"        
  67. #include "DMD.h"        
  68. #include "TimerOne.h"
  69. #include "SystemFont5x7.h"
  70. /* you can remove the fonts if unused */
  71. #define DISPLAYS_ACROSS 3
  72. #define DISPLAYS_DOWN 1
  73. DMD dmd( DISPLAYS_ACROSS , DISPLAYS_DOWN );
  74.  
  75. void ScanDMD()
  76. {
  77.   dmd.scanDisplayBySPI();
  78. }
  79.  
  80. /*--------------------------------------------------------------------------------------
  81.   setup()
  82. --------------------------------------------------------------------------------------*/
  83. void setup()
  84. {
  85.   int file_track = 0;
  86.  
  87.   pinMode(10, OUTPUT);
  88.   digitalWrite(10, LOW);
  89.  
  90.   dmd.scanDisplayBySPI();
  91.    
  92.   if (!card.init(SPI_FULL_SPEED, 10)) {
  93.     Serial.println(F("initialization failed!"));
  94.     return;
  95.   }
  96.  
  97.   // open the file. note that only one file can be open at a time,
  98.   // so you have to close this one before opening another.
  99.      
  100.      if (!volume.init(&card)) {
  101. //       Serial.println(F("volume.init failed"));
  102.        return;
  103.     }
  104.  
  105.     // open the root directory
  106.     if (!root.openRoot(&volume))
  107.     {
  108. //        Serial.println(F("openRoot failed"));
  109.         return;
  110.     }
  111.  
  112.     // open a file
  113.     if (file.open(&root, "teamlist.TXT", O_READ))
  114.     {
  115.         file_size = file.fileSize();
  116.        
  117.         file.rewind();
  118.        
  119.         char character;
  120.        
  121.         for(int x = 0; x <= file_size; x++ )
  122.             {
  123.               character = file.read();
  124.               if( character == '\n' )
  125.               {
  126.                 line_num = line_num + 1;
  127.               }
  128.             }
  129.        
  130.         int index = 0;
  131.  
  132.         for( int x=0; x<=line_num; x++)
  133.         {
  134.           counter[x] = 0;
  135.         }
  136.        
  137.         file.rewind();
  138.        
  139.         int readvar;
  140.        
  141.         while(file_track <= file_size)
  142.         {
  143.             while(readvar != 10 && readvar != -1)
  144.             {
  145.               counter[index] = counter[index] + 1;
  146.               readvar = file.read();
  147.               file_track = file_track + 1;
  148.             }
  149.             index = index + 1;
  150.             counter[index] = counter[index-1];
  151.  
  152.             readvar = 0;
  153.  
  154.         }
  155.        
  156.         file.rewind();
  157.  
  158.   }  
  159.     else {
  160. //        Serial.println(F("file.open failed"));
  161.         return;
  162.     }
  163.  
  164.   int readvar;
  165.   //Read team names into one char array, including line return characters (two at the end of each name except the last name).
  166.   char teams[file_size];
  167.   readvar = file.read( teams, file_size);
  168.  
  169.   digitalWrite(10, HIGH);
  170.  
  171.   dmd.scanDisplayBySPI();
  172.  
  173.   //DMD setup
  174.    Timer1.initialize( 5000 );          
  175.    Timer1.attachInterrupt( ScanDMD );  
  176.    dmd.clearScreen( true );
  177.    dmd.selectFont( SystemFont5x7 );
  178.    
  179.    //LCD setup
  180.    //button adc input
  181.    pinMode( BUTTON_ADC_PIN, INPUT );         //ensure A0 is an input
  182.    digitalWrite( BUTTON_ADC_PIN, LOW );      //ensure pullup is off on A0
  183.    //lcd backlight control
  184.    digitalWrite( LCD_BACKLIGHT_PIN, HIGH );  //backlight control pin A1 is high (on)
  185.    pinMode( LCD_BACKLIGHT_PIN, OUTPUT );     //A1 is an output
  186.    //set up the LCD number of columns and rows:
  187.    lcd.begin( 16, 2 );
  188.    //Print some initial text to the LCD.
  189.    lcd.setCursor( 0, 0 );   //top left
  190.    //          1234567890123456
  191.    lcd.print( "Home" );
  192.    // Set cursor to write top score and then write it
  193.    lcd.setCursor(12,0);
  194.    lcd.print("0");
  195.    lcd.setCursor(14,0);
  196.    lcd.print("0");
  197.  
  198.    lcd.setCursor( 0, 1 );   //bottom left
  199.    //          1234567890123456
  200.    lcd.print( "Away" );
  201.    // Set cursor to write bottom score and then write it
  202.    lcd.setCursor(12,1);
  203.    lcd.print("0");
  204.    lcd.setCursor(14,1);
  205.    lcd.print("0");    
  206.  
  207.   //DMD write names and zero score
  208.   int zero = 0;
  209.    dmd.drawString( 0,0, "Home", 4, GRAPHICS_NORMAL );
  210.    //convert int to char type
  211.    itoa(zero,top_set_char,10);
  212.    dmd.drawString( 73,0, top_set_char, 1, GRAPHICS_NORMAL );
  213.    itoa(zero,top_score_char,10);
  214.    dmd.drawString( 85,0, top_score_char, 2, GRAPHICS_NORMAL );
  215.    
  216.    dmd.drawString( 0,9, "Away", 4, GRAPHICS_NORMAL );
  217.    //convert int to char type
  218.    itoa(zero,bot_set_char,10);
  219.    dmd.drawString( 73,9, bot_set_char, 1, GRAPHICS_NORMAL );
  220.    itoa(zero,bot_score_char,10);
  221.    dmd.drawString( 85,9, bot_score_char, 2, GRAPHICS_NORMAL );
  222.    //
  223.   delay(10);
  224.  
  225. }
  226. /*--------------------------------------------------------------------------------------
  227.   loop()
  228. --------------------------------------------------------------------------------------*/
  229. void loop()
  230. {
  231.    //get the latest button pressed, also the buttonJustPressed, buttonJustReleased flags
  232.    button = ReadButtons();
  233.    
  234.    digitalWrite(10, HIGH);
  235.    
  236.    switch( button )
  237.    {
  238.       case BUTTON_NONE:
  239.       {
  240.          break;
  241.       }
  242.       case BUTTON_RIGHT:
  243.       {
  244.          
  245.        delay(400);
  246.        button = ReadButtons();
  247.        
  248.        if (button == BUTTON_NONE && buttonWas2 == BUTTON_NONE)
  249.          {
  250.            //Increase bottom score by one and redraw it
  251.            bot_score = bot_score + 1;
  252.            //check bot_score is above zero, otherwise set to zero
  253.            if(bot_score < 0)
  254.              {
  255.                bot_score = 0;
  256.              }
  257.            // Set cursor to clear score, then set to write bottom score and then write it
  258.            lcd.setCursor(14,1);
  259.            lcd.print("  ");
  260.            lcd.setCursor(14,1);
  261.            lcd.print(bot_score);
  262.            itoa(bot_score,bot_score_char,10);
  263.            dmd.drawString( 85,9, bot_score_char, 2, GRAPHICS_NORMAL );
  264.            delay(250);
  265.            break;
  266.          }
  267.          
  268.          else if (button == BUTTON_RIGHT)
  269.          {
  270.            //Decrease bottom set by one and redraw it
  271.            bot_set = bot_set + 1;
  272.            //check bot_score is above zero, otherwise set to zero
  273.            if(bot_set < 0)
  274.              {
  275.                bot_set = 0;
  276.              }
  277.            // Set cursor to clear score, then set to write bottom score and then write it
  278.            lcd.setCursor(12,1);
  279.            lcd.print("  ");
  280.            lcd.setCursor(12,1);
  281.            lcd.print(bot_set);
  282.            itoa(bot_set,bot_set_char,10);
  283.            dmd.drawString( 73,9, bot_set_char, 1, GRAPHICS_NORMAL );
  284.            delay(250);
  285.            break;
  286.          }
  287.          
  288.          break;
  289.       }
  290.       case BUTTON_UP:
  291.       {
  292.          
  293.        delay(400);
  294.        button = ReadButtons();
  295.        
  296.        if (button == BUTTON_NONE && buttonWas2 == BUTTON_NONE)
  297.          {
  298.            //Decrease bottom score by one and redraw it
  299.            top_score = top_score + 1;
  300.            //check bot_score is above zero, otherwise set to zero
  301.            if(top_score < 0)
  302.              {
  303.                top_score = 0;
  304.              }
  305.            // Set cursor to clear score, then set to write bottom score and then write it
  306.            lcd.setCursor(14,0);
  307.            lcd.print("  ");
  308.            lcd.setCursor(14,0);
  309.            lcd.print(top_score);
  310.            itoa(top_score,top_score_char,10);
  311.            dmd.drawString( 85,0, top_score_char, 2, GRAPHICS_NORMAL );
  312.            delay(250);
  313.            break;
  314.          }
  315.          
  316.          else if (button == BUTTON_UP)
  317.          {
  318.            //Decrease bottom set by one and redraw it
  319.            top_set = top_set + 1;
  320.            //check bot_score is above zero, otherwise set to zero
  321.            if(top_set < 0)
  322.              {
  323.                top_set = 0;
  324.              }
  325.            // Set cursor to clear score, then set to write bottom score and then write it
  326.            lcd.setCursor(12,0);
  327.            lcd.print("  ");
  328.            lcd.setCursor(12,0);
  329.            lcd.print(top_set);
  330.            itoa(top_set,top_set_char,10);
  331.            dmd.drawString( 73,0, top_set_char, 1, GRAPHICS_NORMAL );
  332.            delay(250);
  333.            break;
  334.          }
  335.          
  336.          break;
  337.       }
  338.       case BUTTON_DOWN:
  339.       {
  340.          
  341.        delay(400);
  342.        button = ReadButtons();
  343.        
  344.        if (button == BUTTON_NONE && buttonWas2 == BUTTON_NONE)
  345.          {
  346.            //Decrease bottom score by one and redraw it
  347.            top_score = top_score - 1;
  348.            //check bot_score is above zero, otherwise set to zero
  349.            if(top_score < 0)
  350.              {
  351.                top_score = 0;
  352.              }
  353.            // Set cursor to clear score, then set to write bottom score and then write it
  354.            lcd.setCursor(14,0);
  355.            lcd.print("  ");
  356.            lcd.setCursor(14,0);
  357.            lcd.print(top_score);
  358.            itoa(top_score,top_score_char,10);
  359.            dmd.drawString( 85,0, top_score_char, 2, GRAPHICS_NORMAL );
  360.            delay(250);
  361.            break;
  362.          }
  363.          
  364.          else if (button == BUTTON_DOWN)
  365.          {
  366.            //Decrease bottom set by one and redraw it
  367.            top_set = top_set - 1;
  368.            //check bot_score is above zero, otherwise set to zero
  369.            if(top_set < 0)
  370.              {
  371.                top_set = 0;
  372.              }
  373.            // Set cursor to clear score, then set to write bottom score and then write it
  374.            lcd.setCursor(12,0);
  375.            lcd.print("  ");
  376.            lcd.setCursor(12,0);
  377.            lcd.print(top_set);
  378.            itoa(top_set,top_set_char,10);
  379.            dmd.drawString( 73,0, top_set_char, 1, GRAPHICS_NORMAL );
  380.            delay(250);
  381.            break;
  382.          }
  383.          
  384.          break;
  385.       }
  386.       case BUTTON_LEFT:
  387.       {
  388.          
  389.        delay(400);
  390.        button = ReadButtons();
  391.        
  392.        if (button == BUTTON_NONE && buttonWas2 == BUTTON_NONE)
  393.          {
  394.            //Decrease bottom score by one and redraw it
  395.            bot_score = bot_score - 1;
  396.            //check bot_score is above zero, otherwise set to zero
  397.            if(bot_score < 0)
  398.              {
  399.                bot_score = 0;
  400.              }
  401.            // Set cursor to clear score, then set to write bottom score and then write it
  402.            lcd.setCursor(14,1);
  403.            lcd.print("  ");
  404.            lcd.setCursor(14,1);
  405.            lcd.print(bot_score);
  406.            itoa(bot_score,bot_score_char,10);
  407.            dmd.drawString( 85,9, bot_score_char, 2, GRAPHICS_NORMAL );
  408.            delay(250);
  409.            break;
  410.          }
  411.          
  412.          else if (button == BUTTON_LEFT)
  413.          {
  414.            //Decrease bottom set by one and redraw it
  415.            bot_set = bot_set - 1;
  416.            //check bot_score is above zero, otherwise set to zero
  417.            if(bot_set < 0)
  418.              {
  419.                bot_set = 0;
  420.              }
  421.            // Set cursor to clear score, then set to write bottom score and then write it
  422.            lcd.setCursor(12,1);
  423.            lcd.print("  ");
  424.            lcd.setCursor(12,1);
  425.            lcd.print(bot_set);
  426.            itoa(bot_set,bot_set_char,10);
  427.            dmd.drawString( 73,9, bot_set_char, 1, GRAPHICS_NORMAL );
  428.            delay(250);
  429.            break;
  430.          }
  431.          break;
  432.      }
  433.      case BUTTON_SELECT:
  434.      {
  435.        delay(1000);
  436.        button = ReadButtons();
  437.        
  438.        if (button == BUTTON_NONE && buttonWas2 == BUTTON_NONE)
  439.          {
  440.           //Reset scores to 0 but not set values
  441.           top_score = 0;
  442.           bot_score = 0;
  443.            
  444.           // Set cursor to clear score, then set to write 0
  445.           lcd.setCursor(14,1);
  446.           lcd.print("  ");
  447.           lcd.setCursor(14,1);
  448.           lcd.print(0);
  449.           dmd.drawString( 85,9, "0 ", 2, GRAPHICS_NORMAL );
  450.          
  451.           // Set cursor to clear score, then set to write 0
  452.           lcd.setCursor(14,0);
  453.           lcd.print("  ");
  454.           lcd.setCursor(14,0);
  455.           lcd.print(0);
  456.           dmd.drawString( 85,0, "0 ", 2, GRAPHICS_NORMAL );
  457.          
  458.           delay(250);
  459.           break;
  460.          }
  461.          
  462.        else if (button == BUTTON_SELECT)
  463.          {
  464.           lcd.setCursor(0,0);
  465.           lcd.print("                ");
  466.           lcd.setCursor(0,1);
  467.           lcd.print("Team Names?     ");
  468.          
  469.           button = ReadButtons();
  470.           int topbot = 1;
  471.          
  472.           while(button == BUTTON_SELECT)
  473.           {
  474.             button = ReadButtons();
  475.           }
  476.           int pos = 0;
  477.           while(button != BUTTON_SELECT)          
  478.           {        
  479.            
  480.             if (button == BUTTON_NONE)
  481.             {
  482.               button = ReadButtons();
  483.             }
  484.                        
  485.             else if(button == BUTTON_DOWN || button == BUTTON_UP)
  486.             {
  487.               topbot = topbot * -1;
  488.               if (topbot == -1)
  489.               {
  490.                 pos = 1;
  491.               }
  492.               else if (topbot == 1)
  493.               {
  494.                 pos = 0;
  495.               }
  496.               lcd.setCursor(11,0);
  497.               lcd.print(" ");
  498.               lcd.setCursor(11,1);
  499.               lcd.print(" ");
  500.               lcd.setCursor(11,pos);
  501.               lcd.print("<");
  502.  
  503.             }
  504.            
  505.             else if(button == BUTTON_RIGHT)
  506.             {
  507.               team_num = team_num + 1;
  508.               if (team_num > line_num)
  509.               {
  510.                 team_num = 1;
  511.               }
  512.              
  513.               int readvar;
  514.               //Read team names into one char array, including line return characters (two at the end of each name except the last name).
  515.               char team_name[12] = "           ";
  516.               if( team_num == 1 )
  517.               {
  518.                 file.seekSet(0);
  519.                 int readsize = counter[0] - 2 ;
  520.                 readvar = file.read( team_name, readsize);
  521.               }
  522.               else if( team_num != 1 && team_num < line_num)
  523.               {
  524.                 file.seekSet(counter[team_num-2]);
  525.                 int readsize = counter[team_num-1]-counter[team_num-2]-2;
  526.                 readvar = file.read( team_name, readsize);
  527.               }
  528.               else if( team_num == line_num )
  529.               {
  530.                 file.seekSet(counter[team_num-2]);
  531.                 int readsize = counter[team_num-1]-counter[team_num-2]-1;
  532.                 readvar = file.read( team_name, readsize);
  533.               }
  534.  
  535.                lcd.setCursor(0, pos);
  536.                lcd.print("           ");
  537.                lcd.setCursor(0, pos);
  538.                lcd.print(team_name);
  539.                
  540.                //Write team name to DMD displays
  541.                int teamline = 9*pos;
  542.                dmd.drawString( 0, teamline, team_name, 11, GRAPHICS_NORMAL );          
  543.            }
  544.            else if(button == BUTTON_LEFT)
  545.             {
  546.               team_num = team_num - 1;
  547.               if (team_num == 0)
  548.               {
  549.                 team_num = line_num;
  550.               }
  551.                            
  552.               int readvar;
  553.               //Read team names into one char array.
  554.               char team_name[12] = "           ";
  555.               if( team_num == 1 )
  556.               {
  557.                 file.seekSet(0);
  558.                 int readsize = counter[0] - 2 ;
  559.                 readvar = file.read( team_name, readsize);
  560.               }
  561.               else if( team_num != 1 && team_num < line_num)
  562.               {
  563.                 file.seekSet(counter[team_num-2]);
  564.                 int readsize = counter[team_num-1]-counter[team_num-2]-2;
  565.                 readvar = file.read( team_name, readsize);
  566.               }
  567.               else if( team_num == line_num )
  568.               {
  569.                 file.seekSet(counter[team_num-2]);
  570.                 int readsize = counter[team_num-1]-counter[team_num-2]-1;
  571.                 readvar = file.read( team_name, readsize);
  572.               }
  573.                
  574.                //Write team name to LCD screen  
  575.                lcd.setCursor(0, pos);
  576.                lcd.print("           ");
  577.                lcd.setCursor(0, pos);
  578.                lcd.print(team_name);          
  579.                
  580.                //Write team name to DMD displays
  581.                int teamline = 9*pos;
  582.                dmd.drawString( 0, teamline, team_name, 11, GRAPHICS_NORMAL );
  583.            }
  584.              delay(200);
  585.  
  586.              button = ReadButtons();
  587.           }
  588.            //Reset scores on LCD screeen
  589.            lcd.setCursor(11,0);
  590.            lcd.print("     ");
  591.            lcd.setCursor(11,1);
  592.            lcd.print("     ");
  593.            lcd.setCursor(12,0);
  594.            lcd.print("0");
  595.            lcd.setCursor(14,0);
  596.            lcd.print("0");
  597.            lcd.setCursor(12,1);
  598.            lcd.print("0");
  599.            lcd.setCursor(14,1);
  600.            lcd.print("0");
  601.            
  602.            
  603.            //Reset score on DMD displays
  604.            top_score = 0;
  605.            bot_score = 0;
  606.            top_set = 0;
  607.            bot_set = 0;
  608.            itoa(bot_score,bot_score_char,10);
  609.            dmd.drawString( 85,9, bot_score_char, 2, GRAPHICS_NORMAL );
  610.            itoa(top_score,top_score_char,10);
  611.            dmd.drawString( 85,0, top_score_char, 2, GRAPHICS_NORMAL );
  612.            itoa(bot_set,bot_set_char,10);
  613.            dmd.drawString( 73,9, bot_set_char, 2, GRAPHICS_NORMAL );
  614.            itoa(top_set,top_set_char,10);
  615.            dmd.drawString( 73,0, top_set_char, 2, GRAPHICS_NORMAL );          
  616.         break;
  617.       }
  618.       break;
  619.      }
  620.       default:
  621.      {
  622.         break;
  623.      }
  624.    }
  625.  
  626.    //clear the buttonJustPressed or buttonJustReleased flags, they've already done their job now.
  627.    if( buttonJustPressed )
  628.       buttonJustPressed = false;
  629.    if( buttonJustReleased )
  630.       buttonJustReleased = false;
  631. }
  632. /*--------------------------------------------------------------------------------------
  633.   ReadButtons()
  634.   Detect the button pressed and return the value
  635.   Uses global values buttonWas, buttonJustPressed, buttonJustReleased.
  636. --------------------------------------------------------------------------------------*/
  637. byte ReadButtons()
  638. {
  639.      //save the latest button value, for change event detection next time round
  640.    buttonWas2 = buttonWas;
  641.    buttonWas = button;
  642.  
  643.   unsigned int buttonVoltage;
  644.    byte button = BUTTON_NONE;   // return no button pressed if the below checks don't write to btn
  645.    
  646.    //read the button ADC pin voltage
  647.    buttonVoltage = analogRead( BUTTON_ADC_PIN );
  648.  //  Serial.println(buttonVoltage);
  649.    //sense if the voltage falls within valid voltage windows
  650.    if( buttonVoltage < ( RIGHT_10BIT_ADC + BUTTONHYSTERESIS ) )
  651.    {
  652.       button = BUTTON_RIGHT;
  653.    }
  654.    else if(   buttonVoltage >= ( UP_10BIT_ADC - BUTTONHYSTERESIS )
  655.            && buttonVoltage <= ( UP_10BIT_ADC + BUTTONHYSTERESIS ) )
  656.    {
  657.       button = BUTTON_UP;
  658.    }
  659.    else if(   buttonVoltage >= ( DOWN_10BIT_ADC - BUTTONHYSTERESIS )
  660.            && buttonVoltage <= ( DOWN_10BIT_ADC + BUTTONHYSTERESIS ) )
  661.    {
  662.       button = BUTTON_DOWN;
  663.    }
  664.    else if(   buttonVoltage >= ( LEFT_10BIT_ADC - BUTTONHYSTERESIS )
  665.            && buttonVoltage <= ( LEFT_10BIT_ADC + BUTTONHYSTERESIS ) )
  666.    {
  667.       button = BUTTON_LEFT;
  668.    }
  669.    else if(   buttonVoltage >= ( SELECT_10BIT_ADC - BUTTONHYSTERESIS )
  670.            && buttonVoltage <= ( SELECT_10BIT_ADC + BUTTONHYSTERESIS ) )
  671.    {
  672.       button = BUTTON_SELECT;
  673.    }
  674.    //handle button flags for just pressed and just released events
  675.    if( ( buttonWas == BUTTON_NONE ) && ( button == BUTTON_NONE ) )
  676.    {
  677.       //the button was just pressed, set buttonJustPressed, this can optionally be used to trigger a once-off action for a button press event
  678.       //it's the duty of the receiver to clear these flags if it wants to detect a new button change event
  679.       buttonJustPressed  = true;
  680.       buttonJustReleased = false;
  681.    }
  682.    if( ( buttonWas != BUTTON_NONE ) && ( button == BUTTON_NONE ) )
  683.    {
  684.       buttonJustPressed  = false;
  685.       buttonJustReleased = true;
  686.    }
  687.    
  688.    return( button );
  689. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement