Advertisement
LXL15

Scoreboard Sketch

Oct 29th, 2013
299
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 18.65 KB | None | 0 0
  1. /*--------------------------------------------------------------------------------------
  2.   Includes
  3. --------------------------------------------------------------------------------------*/
  4. #include <Wire.h>
  5. #include <LiquidCrystal.h>   // include LCD library
  6. #include <SD.h>
  7. /*--------------------------------------------------------------------------------------
  8.   Defines
  9. --------------------------------------------------------------------------------------*/
  10. // Pins in use
  11. #define BUTTON_ADC_PIN           A0  // A0 is the button ADC input
  12. #define LCD_BACKLIGHT_PIN        A1  // A1 controls LCD backlight
  13. // ADC readings expected for the 5 buttons on the ADC input
  14. #define RIGHT_10BIT_ADC           0  // right
  15. #define UP_10BIT_ADC            208  // up
  16. #define DOWN_10BIT_ADC          410  // down
  17. #define LEFT_10BIT_ADC          626  // left
  18. #define SELECT_10BIT_ADC        819  // select
  19. #define BUTTONHYSTERESIS         5  // hysteresis for valid button sensing window
  20. //return values for ReadButtons()
  21. #define BUTTON_NONE               0  //
  22. #define BUTTON_RIGHT              1  //
  23. #define BUTTON_UP                 2  //
  24. #define BUTTON_DOWN               3  //
  25. #define BUTTON_LEFT               4  //
  26. #define BUTTON_SELECT             5  //
  27. //some example macros with friendly labels for LCD backlight/pin control, tested and can be swapped into the example code as you like
  28. #define LCD_BACKLIGHT_OFF()     digitalWrite( LCD_BACKLIGHT_PIN, LOW )
  29. #define LCD_BACKLIGHT_ON()      digitalWrite( LCD_BACKLIGHT_PIN, HIGH )
  30. #define LCD_BACKLIGHT(state)    { if( state ){digitalWrite( LCD_BACKLIGHT_PIN, HIGH );}else{digitalWrite( LCD_BACKLIGHT_PIN, LOW );} }
  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. int top_score           = 0;              //Value for top team's score
  38. int bot_score           = 0;              //Value for bottom team's score
  39. int hold_count          = 1;              //Value for hold of reset button
  40. int top_set             = 0;              //Value for top team's sets
  41. int bot_set             = 0;              //Value for bottom team's sets
  42. int line_num            = 1;
  43. int team_num            = 0;
  44. int file_len            = 1;
  45. int topbot              = 1;
  46. int pos                 = 0;
  47. int cur_pos             = 0;
  48. int num_byte            = 0;
  49. char top_set_char[2];
  50. char bot_set_char[2];
  51. char top_score_char[3];
  52. char bot_score_char[3];
  53. char character;
  54. char last_char;
  55.  
  56. /*--------------------------------------------------------------------------------------
  57.   Init the LCD library with the LCD pins to be used and DMD scan
  58. --------------------------------------------------------------------------------------*/
  59. 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 )
  60.  
  61. #include "SPI.h"        
  62. #include "DMD.h"        
  63. #include "TimerOne.h"
  64. #include "SystemFont5x7.h"
  65. /* you can remove the fonts if unused */
  66. #define DISPLAYS_ACROSS 3
  67. #define DISPLAYS_DOWN 1
  68. DMD dmd( DISPLAYS_ACROSS , DISPLAYS_DOWN );
  69.  
  70. void ScanDMD()
  71. {
  72.   dmd.scanDisplayBySPI();
  73. }
  74.  
  75. /*--------------------------------------------------------------------------------------
  76.   setup()
  77. --------------------------------------------------------------------------------------*/
  78. void setup()
  79. {
  80. //  Serial.begin(9600);
  81.  
  82.   //DMD setup
  83.    Timer1.initialize( 5000 );          
  84.    Timer1.attachInterrupt( ScanDMD );  
  85.    dmd.clearScreen( true );
  86.    dmd.selectFont( SystemFont5x7 );
  87.    
  88.    //LCD setup
  89.    //button adc input
  90.    pinMode( BUTTON_ADC_PIN, INPUT );         //ensure A0 is an input
  91.    digitalWrite( BUTTON_ADC_PIN, LOW );      //ensure pullup is off on A0
  92.    //lcd backlight control
  93.    digitalWrite( LCD_BACKLIGHT_PIN, HIGH );  //backlight control pin A1 is high (on)
  94.    pinMode( LCD_BACKLIGHT_PIN, OUTPUT );     //A1 is an output
  95.    //set up the LCD number of columns and rows:
  96.    lcd.begin( 16, 2 );
  97.    //Print some initial text to the LCD.
  98.    lcd.setCursor( 0, 0 );   //top left
  99.    //          1234567890123456
  100.    lcd.print( "Home" );
  101.    // Set cursor to write top score and then write it
  102.    lcd.setCursor(12,0);
  103.    lcd.print(top_set);
  104.    lcd.setCursor(14,0);
  105.    lcd.print(top_score);
  106.  
  107.    lcd.setCursor( 0, 1 );   //bottom left
  108.    //          1234567890123456
  109.    lcd.print( "Away" );
  110.    // Set cursor to write bottom score and then write it
  111.    lcd.setCursor(12,1);
  112.    lcd.print(bot_set);
  113.    lcd.setCursor(14,1);
  114.    lcd.print(bot_score);    
  115.  
  116.   //DMD write names and zero score
  117.    dmd.drawString( 0,0, "Home", 4, GRAPHICS_NORMAL );
  118.    //convert int to char type
  119.    itoa(top_set,top_set_char,10);
  120.    dmd.drawString( 73,0, top_set_char, 1, GRAPHICS_NORMAL );
  121.    itoa(top_score,top_score_char,10);
  122.    dmd.drawString( 85,0, top_score_char, 2, GRAPHICS_NORMAL );
  123.    
  124.    dmd.drawString( 0,9, "Away", 4, GRAPHICS_NORMAL );
  125.    //convert int to char type
  126.    itoa(bot_set,bot_set_char,10);
  127.    dmd.drawString( 73,9, bot_set_char, 1, GRAPHICS_NORMAL );
  128.    itoa(bot_score,bot_score_char,10);
  129.    dmd.drawString( 85,9, bot_score_char, 2, GRAPHICS_NORMAL );
  130.    //
  131. }
  132. /*--------------------------------------------------------------------------------------
  133.   loop()
  134. --------------------------------------------------------------------------------------*/
  135. void loop()
  136. {
  137.    byte button;
  138.    byte timestamp;
  139.    
  140.    //get the latest button pressed, also the buttonJustPressed, buttonJustReleased flags
  141.    button = ReadButtons();
  142.  
  143.    switch( button )
  144.    {
  145.       case BUTTON_NONE:
  146.       {
  147.          break;
  148.       }
  149.       case BUTTON_RIGHT:
  150.       {
  151.          
  152.        delay(200);
  153.        byte button;
  154.        byte buttonWAS;
  155.        button = ReadButtons();
  156.        
  157.        if (button == BUTTON_NONE && buttonWAS == BUTTON_NONE)
  158.          {
  159.            //Decrease bottom score by one and redraw it
  160.            bot_score = bot_score + 1;
  161.            //check bot_score is above zero, otherwise set to zero
  162.            if(bot_score < 0)
  163.              {
  164.                bot_score = 0;
  165.              }
  166.            // Set cursor to clear score, then set to write bottom score and then write it
  167.            lcd.setCursor(14,1);
  168.            lcd.print("  ");
  169.            lcd.setCursor(14,1);
  170.            lcd.print(bot_score);
  171.            itoa(bot_score,bot_score_char,10);
  172.            dmd.drawString( 85,9, bot_score_char, 2, GRAPHICS_NORMAL );
  173.            delay(250);
  174.            break;
  175.          }
  176.          
  177.          else if (button == BUTTON_RIGHT)
  178.          {
  179.            //Decrease bottom set by one and redraw it
  180.            bot_set = bot_set + 1;
  181.            //check bot_score is above zero, otherwise set to zero
  182.            if(bot_set < 0)
  183.              {
  184.                bot_set = 0;
  185.              }
  186.            lcd.setCursor( 0, 1 );   //bottom left
  187.            // Set cursor to clear score, then set to write bottom score and then write it
  188.            lcd.setCursor(12,1);
  189.            lcd.print("  ");
  190.            lcd.setCursor(12,1);
  191.            lcd.print(bot_set);
  192.            itoa(bot_set,bot_set_char,10);
  193.            dmd.drawString( 73,9, bot_set_char, 1, GRAPHICS_NORMAL );
  194.            delay(250);
  195.            break;
  196.          }
  197.          
  198.       }
  199.       case BUTTON_UP:
  200.       {
  201.          
  202.        delay(200);
  203.        byte button;
  204.        byte buttonWAS;
  205.        button = ReadButtons();
  206.        
  207.        if (button == BUTTON_NONE && buttonWAS == BUTTON_NONE)
  208.          {
  209.            //Decrease bottom score by one and redraw it
  210.            top_score = top_score + 1;
  211.            //check bot_score is above zero, otherwise set to zero
  212.            if(top_score < 0)
  213.              {
  214.                top_score = 0;
  215.              }
  216.            // Set cursor to clear score, then set to write bottom score and then write it
  217.            lcd.setCursor(14,0);
  218.            lcd.print("  ");
  219.            lcd.setCursor(14,0);
  220.            lcd.print(top_score);
  221.            itoa(top_score,top_score_char,10);
  222.            dmd.drawString( 85,0, top_score_char, 2, GRAPHICS_NORMAL );
  223.            delay(250);
  224.            break;
  225.          }
  226.          
  227.          else if (button == BUTTON_UP)
  228.          {
  229.            //Decrease bottom set by one and redraw it
  230.            top_set = top_set + 1;
  231.            //check bot_score is above zero, otherwise set to zero
  232.            if(top_set < 0)
  233.              {
  234.                top_set = 0;
  235.              }
  236.            lcd.setCursor( 0, 1 );   //bottom left
  237.            // Set cursor to clear score, then set to write bottom score and then write it
  238.            lcd.setCursor(12,0);
  239.            lcd.print("  ");
  240.            lcd.setCursor(12,0);
  241.            lcd.print(top_set);
  242.            itoa(top_set,top_set_char,10);
  243.            dmd.drawString( 73,0, top_set_char, 1, GRAPHICS_NORMAL );
  244.            delay(250);
  245.            break;
  246.          }
  247.          
  248.       }
  249.       case BUTTON_DOWN:
  250.       {
  251.          
  252.        delay(200);
  253.        byte button;
  254.        byte buttonWAS;
  255.        button = ReadButtons();
  256.        
  257.        if (button == BUTTON_NONE && buttonWAS == BUTTON_NONE)
  258.          {
  259.            //Decrease bottom score by one and redraw it
  260.            top_score = top_score - 1;
  261.            //check bot_score is above zero, otherwise set to zero
  262.            if(top_score < 0)
  263.              {
  264.                top_score = 0;
  265.              }
  266.            // Set cursor to clear score, then set to write bottom score and then write it
  267.            lcd.setCursor(14,0);
  268.            lcd.print("  ");
  269.            lcd.setCursor(14,0);
  270.            lcd.print(top_score);
  271.            itoa(top_score,top_score_char,10);
  272.            dmd.drawString( 85,0, top_score_char, 2, GRAPHICS_NORMAL );
  273.            delay(250);
  274.            break;
  275.          }
  276.          
  277.          else if (button == BUTTON_DOWN)
  278.          {
  279.            //Decrease bottom set by one and redraw it
  280.            top_set = top_set - 1;
  281.            //check bot_score is above zero, otherwise set to zero
  282.            if(top_set < 0)
  283.              {
  284.                top_set = 0;
  285.              }
  286.            lcd.setCursor( 0, 1 );   //bottom left
  287.            // Set cursor to clear score, then set to write bottom score and then write it
  288.            lcd.setCursor(12,0);
  289.            lcd.print("  ");
  290.            lcd.setCursor(12,0);
  291.            lcd.print(top_set);
  292.            itoa(top_set,top_set_char,10);
  293.            dmd.drawString( 73,0, top_set_char, 1, GRAPHICS_NORMAL );
  294.            delay(250);
  295.            break;
  296.          }
  297.          
  298.       }
  299.       case BUTTON_LEFT:
  300.       {
  301.          
  302.        delay(200);
  303.        byte button;
  304.        byte buttonWAS;
  305.        button = ReadButtons();
  306.        
  307.        if (button == BUTTON_NONE && buttonWAS == BUTTON_NONE)
  308.          {
  309.            //Decrease bottom score by one and redraw it
  310.            bot_score = bot_score - 1;
  311.            //check bot_score is above zero, otherwise set to zero
  312.            if(bot_score < 0)
  313.              {
  314.                bot_score = 0;
  315.              }
  316.            lcd.setCursor( 0, 1 );   //bottom left
  317.            // Set cursor to clear score, then set to write bottom score and then write it
  318.            lcd.setCursor(14,1);
  319.            lcd.print("  ");
  320.            lcd.setCursor(14,1);
  321.            lcd.print(bot_score);
  322.            itoa(bot_score,bot_score_char,10);
  323.            dmd.drawString( 85,9, bot_score_char, 2, GRAPHICS_NORMAL );
  324.            delay(250);
  325.            break;
  326.          }
  327.          
  328.          else if (button == BUTTON_LEFT)
  329.          {
  330.            //Decrease bottom set by one and redraw it
  331.            bot_set = bot_set - 1;
  332.            //check bot_score is above zero, otherwise set to zero
  333.            if(bot_set < 0)
  334.              {
  335.                bot_set = 0;
  336.              }
  337.            lcd.setCursor( 0, 1 );   //bottom left
  338.            // Set cursor to clear score, then set to write bottom score and then write it
  339.            lcd.setCursor(12,1);
  340.            lcd.print("  ");
  341.            lcd.setCursor(12,1);
  342.            lcd.print(bot_set);
  343.            itoa(bot_set,bot_set_char,10);
  344.            dmd.drawString( 73,9, bot_set_char, 1, GRAPHICS_NORMAL );
  345.            delay(250);
  346.            break;
  347.          }
  348.      }
  349.      case BUTTON_SELECT:
  350.      {
  351.        delay(500);
  352.        byte button;
  353.        byte buttonWAS;
  354.        button = ReadButtons();
  355.        
  356.        if (button == BUTTON_NONE && buttonWAS == BUTTON_NONE)
  357.          {
  358.           //Reset scores to 0 but not set values
  359.           top_score = 0;
  360.           bot_score = 0;
  361.            
  362.           // Set cursor to clear score, then set to write 0
  363.           lcd.setCursor(14,1);
  364.           lcd.print("  ");
  365.           lcd.setCursor(14,1);
  366.           lcd.print(0);
  367.           dmd.drawString( 85,9, "0 ", 2, GRAPHICS_NORMAL );
  368.          
  369.           // Set cursor to clear score, then set to write 0
  370.           lcd.setCursor(14,0);
  371.           lcd.print("  ");
  372.           lcd.setCursor(14,0);
  373.           lcd.print(0);
  374.           dmd.drawString( 85,0, "0 ", 2, GRAPHICS_NORMAL );
  375.          
  376.           delay(250);
  377.           break;
  378.          }
  379.          
  380.        else if (button == BUTTON_SELECT)
  381.          {
  382.           lcd.setCursor(0,0);
  383.           lcd.print("                ");
  384.           lcd.setCursor(0,1);
  385.           lcd.print("Team Name?      ");
  386.          
  387.           button = ReadButtons();
  388.          
  389.           File myFile;
  390.           myFile = SD.open("teamlist.txt", FILE_READ);
  391.          
  392.           file_len = 1;
  393.          
  394.           num_byte = myFile.size();
  395.          
  396.           lcd.setCursor(0,0);
  397.           lcd.print(num_byte);
  398.          
  399.           for (int x = 0; x <=num_byte; x++)
  400.           {
  401.             character = myFile.read();
  402.             if (last_char == '\n')
  403.             {
  404.              file_len = file_len + 1;
  405.             }
  406.             last_char = character;
  407.           }
  408.          
  409.           lcd.setCursor(3,0);
  410.           lcd.print(file_len);
  411.          
  412.           myFile.close();
  413.          
  414.           while(button == BUTTON_SELECT)
  415.           {
  416.             button = ReadButtons();
  417.           }
  418.          
  419.           while(button != BUTTON_SELECT)          
  420.           {        
  421.            
  422.             if (button == BUTTON_NONE)
  423.             {
  424.               button = ReadButtons();
  425.             }
  426.                        
  427.             else if(button == BUTTON_DOWN || button == BUTTON_UP)
  428.             {
  429.  
  430.               topbot = topbot * -1;
  431.               if (topbot == -1)
  432.               {
  433.                 pos = 1;
  434.               }
  435.               else if (topbot == 1)
  436.               {
  437.                 pos = 0;
  438.               }
  439.               lcd.setCursor(11,0);
  440.               lcd.print(" ");
  441.               lcd.setCursor(11,1);
  442.               lcd.print(" ");
  443.               lcd.setCursor(11,pos);
  444.               lcd.print("<");
  445.             }
  446.            
  447.             else if(button == BUTTON_RIGHT)
  448.             {
  449.               team_num = team_num + 1;
  450.               if (team_num > file_len)
  451.               {
  452.                 team_num = 1;
  453.               }
  454.              
  455.               Serial.begin(9600);
  456.               Serial.print("team_num: ");
  457.               Serial.println(team_num);
  458.               Serial.end();
  459.              
  460.               myFile = SD.open("teamlist.txt");
  461.              
  462.               while (myFile.available())
  463.               {
  464.                 character = myFile.read();
  465.                 if (last_char == '\n')
  466.                 {
  467.                   line_num = line_num + 1;
  468.                 }
  469.                 else if (line_num == team_num)
  470.                 {
  471.                   lcd.setCursor(cur_pos,pos);
  472.                   lcd.print(character);
  473.                   cur_pos = cur_pos + 1;
  474.                 }
  475.                 last_char = character;
  476.               }
  477.               myFile.close();
  478.              
  479.              }
  480.              delay(250);
  481.  //            Serial.begin(9600);
  482.              button = ReadButtons();
  483.  //            Serial.end();
  484.             }
  485.         break;
  486.       }
  487.       break;
  488.      }
  489.       default:
  490.      {
  491.         break;
  492.      }
  493.    }
  494.  
  495.    //clear the buttonJustPressed or buttonJustReleased flags, they've already done their job now.
  496.    if( buttonJustPressed )
  497.       buttonJustPressed = false;
  498.    if( buttonJustReleased )
  499.       buttonJustReleased = false;
  500. }
  501. /*--------------------------------------------------------------------------------------
  502.   ReadButtons()
  503.   Detect the button pressed and return the value
  504.   Uses global values buttonWas, buttonJustPressed, buttonJustReleased.
  505. --------------------------------------------------------------------------------------*/
  506. byte ReadButtons()
  507. {
  508.    unsigned int buttonVoltage;
  509.    byte button = BUTTON_NONE;   // return no button pressed if the below checks don't write to btn
  510.    
  511.    //read the button ADC pin voltage
  512.    buttonVoltage = analogRead( BUTTON_ADC_PIN );
  513.  //  Serial.println(buttonVoltage);
  514.    //sense if the voltage falls within valid voltage windows
  515.    if( buttonVoltage < ( RIGHT_10BIT_ADC + BUTTONHYSTERESIS ) )
  516.    {
  517.       button = BUTTON_RIGHT;
  518.    }
  519.    else if(   buttonVoltage >= ( UP_10BIT_ADC - BUTTONHYSTERESIS )
  520.            && buttonVoltage <= ( UP_10BIT_ADC + BUTTONHYSTERESIS ) )
  521.    {
  522.       button = BUTTON_UP;
  523.    }
  524.    else if(   buttonVoltage >= ( DOWN_10BIT_ADC - BUTTONHYSTERESIS )
  525.            && buttonVoltage <= ( DOWN_10BIT_ADC + BUTTONHYSTERESIS ) )
  526.    {
  527.       button = BUTTON_DOWN;
  528.    }
  529.    else if(   buttonVoltage >= ( LEFT_10BIT_ADC - BUTTONHYSTERESIS )
  530.            && buttonVoltage <= ( LEFT_10BIT_ADC + BUTTONHYSTERESIS ) )
  531.    {
  532.       button = BUTTON_LEFT;
  533.    }
  534.    else if(   buttonVoltage >= ( SELECT_10BIT_ADC - BUTTONHYSTERESIS )
  535.            && buttonVoltage <= ( SELECT_10BIT_ADC + BUTTONHYSTERESIS ) )
  536.    {
  537.       button = BUTTON_SELECT;
  538.    }
  539.    //handle button flags for just pressed and just released events
  540.    if( ( buttonWas == BUTTON_NONE ) && ( button != BUTTON_NONE ) )
  541.    {
  542.       //the button was just pressed, set buttonJustPressed, this can optionally be used to trigger a once-off action for a button press event
  543.       //it's the duty of the receiver to clear these flags if it wants to detect a new button change event
  544.       buttonJustPressed  = true;
  545.       buttonJustReleased = false;
  546.    }
  547.    if( ( buttonWas != BUTTON_NONE ) && ( button == BUTTON_NONE ) )
  548.    {
  549.       buttonJustPressed  = false;
  550.       buttonJustReleased = true;
  551.    }
  552.    
  553.    //save the latest button value, for change event detection next time round
  554.    buttonWas = button;
  555.    
  556.    return( button );
  557. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement