Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*--------------------------------------------------------------------------------------
- Includes
- --------------------------------------------------------------------------------------*/
- #include <Wire.h>
- #include <LiquidCrystal.h> // include LCD library
- #include <SD.h>
- /*--------------------------------------------------------------------------------------
- Defines
- --------------------------------------------------------------------------------------*/
- // Pins in use
- #define BUTTON_ADC_PIN A0 // A0 is the button ADC input
- #define LCD_BACKLIGHT_PIN A1 // A1 controls LCD backlight
- // ADC readings expected for the 5 buttons on the ADC input
- #define RIGHT_10BIT_ADC 0 // right
- #define UP_10BIT_ADC 208 // up
- #define DOWN_10BIT_ADC 410 // down
- #define LEFT_10BIT_ADC 626 // left
- #define SELECT_10BIT_ADC 819 // select
- #define BUTTONHYSTERESIS 5 // hysteresis for valid button sensing window
- //return values for ReadButtons()
- #define BUTTON_NONE 0 //
- #define BUTTON_RIGHT 1 //
- #define BUTTON_UP 2 //
- #define BUTTON_DOWN 3 //
- #define BUTTON_LEFT 4 //
- #define BUTTON_SELECT 5 //
- //some example macros with friendly labels for LCD backlight/pin control, tested and can be swapped into the example code as you like
- #define LCD_BACKLIGHT_OFF() digitalWrite( LCD_BACKLIGHT_PIN, LOW )
- #define LCD_BACKLIGHT_ON() digitalWrite( LCD_BACKLIGHT_PIN, HIGH )
- #define LCD_BACKLIGHT(state) { if( state ){digitalWrite( LCD_BACKLIGHT_PIN, HIGH );}else{digitalWrite( LCD_BACKLIGHT_PIN, LOW );} }
- /*--------------------------------------------------------------------------------------
- Variables
- --------------------------------------------------------------------------------------*/
- byte buttonJustPressed = false; //this will be true after a ReadButtons() call if triggered
- byte buttonJustReleased = false; //this will be true after a ReadButtons() call if triggered
- byte buttonWas = BUTTON_NONE; //used by ReadButtons() for detection of button events
- int top_score = 0; //Value for top team's score
- int bot_score = 0; //Value for bottom team's score
- int hold_count = 1; //Value for hold of reset button
- int top_set = 0; //Value for top team's sets
- int bot_set = 0; //Value for bottom team's sets
- int line_num = 1;
- int team_num = 0;
- int file_len = 1;
- int topbot = 1;
- int pos = 0;
- int cur_pos = 0;
- int num_byte = 0;
- char top_set_char[2];
- char bot_set_char[2];
- char top_score_char[3];
- char bot_score_char[3];
- char character;
- char last_char;
- /*--------------------------------------------------------------------------------------
- Init the LCD library with the LCD pins to be used and DMD scan
- --------------------------------------------------------------------------------------*/
- 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 )
- #include "SPI.h"
- #include "DMD.h"
- #include "TimerOne.h"
- #include "SystemFont5x7.h"
- /* you can remove the fonts if unused */
- #define DISPLAYS_ACROSS 3
- #define DISPLAYS_DOWN 1
- DMD dmd( DISPLAYS_ACROSS , DISPLAYS_DOWN );
- void ScanDMD()
- {
- dmd.scanDisplayBySPI();
- }
- /*--------------------------------------------------------------------------------------
- setup()
- --------------------------------------------------------------------------------------*/
- void setup()
- {
- // Serial.begin(9600);
- //DMD setup
- Timer1.initialize( 5000 );
- Timer1.attachInterrupt( ScanDMD );
- dmd.clearScreen( true );
- dmd.selectFont( SystemFont5x7 );
- //LCD setup
- //button adc input
- pinMode( BUTTON_ADC_PIN, INPUT ); //ensure A0 is an input
- digitalWrite( BUTTON_ADC_PIN, LOW ); //ensure pullup is off on A0
- //lcd backlight control
- digitalWrite( LCD_BACKLIGHT_PIN, HIGH ); //backlight control pin A1 is high (on)
- pinMode( LCD_BACKLIGHT_PIN, OUTPUT ); //A1 is an output
- //set up the LCD number of columns and rows:
- lcd.begin( 16, 2 );
- //Print some initial text to the LCD.
- lcd.setCursor( 0, 0 ); //top left
- // 1234567890123456
- lcd.print( "Home" );
- // Set cursor to write top score and then write it
- lcd.setCursor(12,0);
- lcd.print(top_set);
- lcd.setCursor(14,0);
- lcd.print(top_score);
- lcd.setCursor( 0, 1 ); //bottom left
- // 1234567890123456
- lcd.print( "Away" );
- // Set cursor to write bottom score and then write it
- lcd.setCursor(12,1);
- lcd.print(bot_set);
- lcd.setCursor(14,1);
- lcd.print(bot_score);
- //DMD write names and zero score
- dmd.drawString( 0,0, "Home", 4, GRAPHICS_NORMAL );
- //convert int to char type
- itoa(top_set,top_set_char,10);
- dmd.drawString( 73,0, top_set_char, 1, GRAPHICS_NORMAL );
- itoa(top_score,top_score_char,10);
- dmd.drawString( 85,0, top_score_char, 2, GRAPHICS_NORMAL );
- dmd.drawString( 0,9, "Away", 4, GRAPHICS_NORMAL );
- //convert int to char type
- itoa(bot_set,bot_set_char,10);
- dmd.drawString( 73,9, bot_set_char, 1, GRAPHICS_NORMAL );
- itoa(bot_score,bot_score_char,10);
- dmd.drawString( 85,9, bot_score_char, 2, GRAPHICS_NORMAL );
- //
- }
- /*--------------------------------------------------------------------------------------
- loop()
- --------------------------------------------------------------------------------------*/
- void loop()
- {
- byte button;
- byte timestamp;
- //get the latest button pressed, also the buttonJustPressed, buttonJustReleased flags
- button = ReadButtons();
- switch( button )
- {
- case BUTTON_NONE:
- {
- break;
- }
- case BUTTON_RIGHT:
- {
- delay(200);
- byte button;
- byte buttonWAS;
- button = ReadButtons();
- if (button == BUTTON_NONE && buttonWAS == BUTTON_NONE)
- {
- //Decrease bottom score by one and redraw it
- bot_score = bot_score + 1;
- //check bot_score is above zero, otherwise set to zero
- if(bot_score < 0)
- {
- bot_score = 0;
- }
- // Set cursor to clear score, then set to write bottom score and then write it
- lcd.setCursor(14,1);
- lcd.print(" ");
- lcd.setCursor(14,1);
- lcd.print(bot_score);
- itoa(bot_score,bot_score_char,10);
- dmd.drawString( 85,9, bot_score_char, 2, GRAPHICS_NORMAL );
- delay(250);
- break;
- }
- else if (button == BUTTON_RIGHT)
- {
- //Decrease bottom set by one and redraw it
- bot_set = bot_set + 1;
- //check bot_score is above zero, otherwise set to zero
- if(bot_set < 0)
- {
- bot_set = 0;
- }
- lcd.setCursor( 0, 1 ); //bottom left
- // Set cursor to clear score, then set to write bottom score and then write it
- lcd.setCursor(12,1);
- lcd.print(" ");
- lcd.setCursor(12,1);
- lcd.print(bot_set);
- itoa(bot_set,bot_set_char,10);
- dmd.drawString( 73,9, bot_set_char, 1, GRAPHICS_NORMAL );
- delay(250);
- break;
- }
- }
- case BUTTON_UP:
- {
- delay(200);
- byte button;
- byte buttonWAS;
- button = ReadButtons();
- if (button == BUTTON_NONE && buttonWAS == BUTTON_NONE)
- {
- //Decrease bottom score by one and redraw it
- top_score = top_score + 1;
- //check bot_score is above zero, otherwise set to zero
- if(top_score < 0)
- {
- top_score = 0;
- }
- // Set cursor to clear score, then set to write bottom score and then write it
- lcd.setCursor(14,0);
- lcd.print(" ");
- lcd.setCursor(14,0);
- lcd.print(top_score);
- itoa(top_score,top_score_char,10);
- dmd.drawString( 85,0, top_score_char, 2, GRAPHICS_NORMAL );
- delay(250);
- break;
- }
- else if (button == BUTTON_UP)
- {
- //Decrease bottom set by one and redraw it
- top_set = top_set + 1;
- //check bot_score is above zero, otherwise set to zero
- if(top_set < 0)
- {
- top_set = 0;
- }
- lcd.setCursor( 0, 1 ); //bottom left
- // Set cursor to clear score, then set to write bottom score and then write it
- lcd.setCursor(12,0);
- lcd.print(" ");
- lcd.setCursor(12,0);
- lcd.print(top_set);
- itoa(top_set,top_set_char,10);
- dmd.drawString( 73,0, top_set_char, 1, GRAPHICS_NORMAL );
- delay(250);
- break;
- }
- }
- case BUTTON_DOWN:
- {
- delay(200);
- byte button;
- byte buttonWAS;
- button = ReadButtons();
- if (button == BUTTON_NONE && buttonWAS == BUTTON_NONE)
- {
- //Decrease bottom score by one and redraw it
- top_score = top_score - 1;
- //check bot_score is above zero, otherwise set to zero
- if(top_score < 0)
- {
- top_score = 0;
- }
- // Set cursor to clear score, then set to write bottom score and then write it
- lcd.setCursor(14,0);
- lcd.print(" ");
- lcd.setCursor(14,0);
- lcd.print(top_score);
- itoa(top_score,top_score_char,10);
- dmd.drawString( 85,0, top_score_char, 2, GRAPHICS_NORMAL );
- delay(250);
- break;
- }
- else if (button == BUTTON_DOWN)
- {
- //Decrease bottom set by one and redraw it
- top_set = top_set - 1;
- //check bot_score is above zero, otherwise set to zero
- if(top_set < 0)
- {
- top_set = 0;
- }
- lcd.setCursor( 0, 1 ); //bottom left
- // Set cursor to clear score, then set to write bottom score and then write it
- lcd.setCursor(12,0);
- lcd.print(" ");
- lcd.setCursor(12,0);
- lcd.print(top_set);
- itoa(top_set,top_set_char,10);
- dmd.drawString( 73,0, top_set_char, 1, GRAPHICS_NORMAL );
- delay(250);
- break;
- }
- }
- case BUTTON_LEFT:
- {
- delay(200);
- byte button;
- byte buttonWAS;
- button = ReadButtons();
- if (button == BUTTON_NONE && buttonWAS == BUTTON_NONE)
- {
- //Decrease bottom score by one and redraw it
- bot_score = bot_score - 1;
- //check bot_score is above zero, otherwise set to zero
- if(bot_score < 0)
- {
- bot_score = 0;
- }
- lcd.setCursor( 0, 1 ); //bottom left
- // Set cursor to clear score, then set to write bottom score and then write it
- lcd.setCursor(14,1);
- lcd.print(" ");
- lcd.setCursor(14,1);
- lcd.print(bot_score);
- itoa(bot_score,bot_score_char,10);
- dmd.drawString( 85,9, bot_score_char, 2, GRAPHICS_NORMAL );
- delay(250);
- break;
- }
- else if (button == BUTTON_LEFT)
- {
- //Decrease bottom set by one and redraw it
- bot_set = bot_set - 1;
- //check bot_score is above zero, otherwise set to zero
- if(bot_set < 0)
- {
- bot_set = 0;
- }
- lcd.setCursor( 0, 1 ); //bottom left
- // Set cursor to clear score, then set to write bottom score and then write it
- lcd.setCursor(12,1);
- lcd.print(" ");
- lcd.setCursor(12,1);
- lcd.print(bot_set);
- itoa(bot_set,bot_set_char,10);
- dmd.drawString( 73,9, bot_set_char, 1, GRAPHICS_NORMAL );
- delay(250);
- break;
- }
- }
- case BUTTON_SELECT:
- {
- delay(500);
- byte button;
- byte buttonWAS;
- button = ReadButtons();
- if (button == BUTTON_NONE && buttonWAS == BUTTON_NONE)
- {
- //Reset scores to 0 but not set values
- top_score = 0;
- bot_score = 0;
- // Set cursor to clear score, then set to write 0
- lcd.setCursor(14,1);
- lcd.print(" ");
- lcd.setCursor(14,1);
- lcd.print(0);
- dmd.drawString( 85,9, "0 ", 2, GRAPHICS_NORMAL );
- // Set cursor to clear score, then set to write 0
- lcd.setCursor(14,0);
- lcd.print(" ");
- lcd.setCursor(14,0);
- lcd.print(0);
- dmd.drawString( 85,0, "0 ", 2, GRAPHICS_NORMAL );
- delay(250);
- break;
- }
- else if (button == BUTTON_SELECT)
- {
- lcd.setCursor(0,0);
- lcd.print(" ");
- lcd.setCursor(0,1);
- lcd.print("Team Name? ");
- button = ReadButtons();
- File myFile;
- myFile = SD.open("teamlist.txt", FILE_READ);
- file_len = 1;
- num_byte = myFile.size();
- lcd.setCursor(0,0);
- lcd.print(num_byte);
- for (int x = 0; x <=num_byte; x++)
- {
- character = myFile.read();
- if (last_char == '\n')
- {
- file_len = file_len + 1;
- }
- last_char = character;
- }
- lcd.setCursor(3,0);
- lcd.print(file_len);
- myFile.close();
- while(button == BUTTON_SELECT)
- {
- button = ReadButtons();
- }
- while(button != BUTTON_SELECT)
- {
- if (button == BUTTON_NONE)
- {
- button = ReadButtons();
- }
- else if(button == BUTTON_DOWN || button == BUTTON_UP)
- {
- topbot = topbot * -1;
- if (topbot == -1)
- {
- pos = 1;
- }
- else if (topbot == 1)
- {
- pos = 0;
- }
- lcd.setCursor(11,0);
- lcd.print(" ");
- lcd.setCursor(11,1);
- lcd.print(" ");
- lcd.setCursor(11,pos);
- lcd.print("<");
- }
- else if(button == BUTTON_RIGHT)
- {
- team_num = team_num + 1;
- if (team_num > file_len)
- {
- team_num = 1;
- }
- Serial.begin(9600);
- Serial.print("team_num: ");
- Serial.println(team_num);
- Serial.end();
- myFile = SD.open("teamlist.txt");
- while (myFile.available())
- {
- character = myFile.read();
- if (last_char == '\n')
- {
- line_num = line_num + 1;
- }
- else if (line_num == team_num)
- {
- lcd.setCursor(cur_pos,pos);
- lcd.print(character);
- cur_pos = cur_pos + 1;
- }
- last_char = character;
- }
- myFile.close();
- }
- delay(250);
- // Serial.begin(9600);
- button = ReadButtons();
- // Serial.end();
- }
- break;
- }
- break;
- }
- default:
- {
- break;
- }
- }
- //clear the buttonJustPressed or buttonJustReleased flags, they've already done their job now.
- if( buttonJustPressed )
- buttonJustPressed = false;
- if( buttonJustReleased )
- buttonJustReleased = false;
- }
- /*--------------------------------------------------------------------------------------
- ReadButtons()
- Detect the button pressed and return the value
- Uses global values buttonWas, buttonJustPressed, buttonJustReleased.
- --------------------------------------------------------------------------------------*/
- byte ReadButtons()
- {
- unsigned int buttonVoltage;
- byte button = BUTTON_NONE; // return no button pressed if the below checks don't write to btn
- //read the button ADC pin voltage
- buttonVoltage = analogRead( BUTTON_ADC_PIN );
- // Serial.println(buttonVoltage);
- //sense if the voltage falls within valid voltage windows
- if( buttonVoltage < ( RIGHT_10BIT_ADC + BUTTONHYSTERESIS ) )
- {
- button = BUTTON_RIGHT;
- }
- else if( buttonVoltage >= ( UP_10BIT_ADC - BUTTONHYSTERESIS )
- && buttonVoltage <= ( UP_10BIT_ADC + BUTTONHYSTERESIS ) )
- {
- button = BUTTON_UP;
- }
- else if( buttonVoltage >= ( DOWN_10BIT_ADC - BUTTONHYSTERESIS )
- && buttonVoltage <= ( DOWN_10BIT_ADC + BUTTONHYSTERESIS ) )
- {
- button = BUTTON_DOWN;
- }
- else if( buttonVoltage >= ( LEFT_10BIT_ADC - BUTTONHYSTERESIS )
- && buttonVoltage <= ( LEFT_10BIT_ADC + BUTTONHYSTERESIS ) )
- {
- button = BUTTON_LEFT;
- }
- else if( buttonVoltage >= ( SELECT_10BIT_ADC - BUTTONHYSTERESIS )
- && buttonVoltage <= ( SELECT_10BIT_ADC + BUTTONHYSTERESIS ) )
- {
- button = BUTTON_SELECT;
- }
- //handle button flags for just pressed and just released events
- if( ( buttonWas == BUTTON_NONE ) && ( button != BUTTON_NONE ) )
- {
- //the button was just pressed, set buttonJustPressed, this can optionally be used to trigger a once-off action for a button press event
- //it's the duty of the receiver to clear these flags if it wants to detect a new button change event
- buttonJustPressed = true;
- buttonJustReleased = false;
- }
- if( ( buttonWas != BUTTON_NONE ) && ( button == BUTTON_NONE ) )
- {
- buttonJustPressed = false;
- buttonJustReleased = true;
- }
- //save the latest button value, for change event detection next time round
- buttonWas = button;
- return( button );
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement