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 <SdFat.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 button = BUTTON_NONE;
- byte buttonWas = BUTTON_NONE; //used by ReadButtons() for detection of button events
- byte buttonWas2 = BUTTON_NONE;
- int top_score = 0; //Value for top team's score
- int bot_score = 0; //Value for bottom team's score
- 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 = 1;
- int file_len = 1;
- int file_size;
- int counter[30];
- char top_set_char[2];
- char bot_set_char[2];
- char top_score_char[3];
- char bot_score_char[3];
- char teams[1];
- Sd2Card card;
- SdVolume volume;
- SdFile root;
- SdFile file;
- /*--------------------------------------------------------------------------------------
- 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()
- {
- int file_track = 0;
- pinMode(10, OUTPUT);
- digitalWrite(10, LOW);
- dmd.scanDisplayBySPI();
- if (!card.init(SPI_FULL_SPEED, 10)) {
- Serial.println(F("initialization failed!"));
- return;
- }
- // open the file. note that only one file can be open at a time,
- // so you have to close this one before opening another.
- if (!volume.init(&card)) {
- // Serial.println(F("volume.init failed"));
- return;
- }
- // open the root directory
- if (!root.openRoot(&volume))
- {
- // Serial.println(F("openRoot failed"));
- return;
- }
- // open a file
- if (file.open(&root, "teamlist.TXT", O_READ))
- {
- file_size = file.fileSize();
- file.rewind();
- char character;
- for(int x = 0; x <= file_size; x++ )
- {
- character = file.read();
- if( character == '\n' )
- {
- line_num = line_num + 1;
- }
- }
- int index = 0;
- for( int x=0; x<=line_num; x++)
- {
- counter[x] = 0;
- }
- file.rewind();
- int readvar;
- while(file_track <= file_size)
- {
- while(readvar != 10 && readvar != -1)
- {
- counter[index] = counter[index] + 1;
- readvar = file.read();
- file_track = file_track + 1;
- }
- index = index + 1;
- counter[index] = counter[index-1];
- readvar = 0;
- }
- file.rewind();
- }
- else {
- // Serial.println(F("file.open failed"));
- return;
- }
- int readvar;
- //Read team names into one char array, including line return characters (two at the end of each name except the last name).
- char teams[file_size];
- readvar = file.read( teams, file_size);
- digitalWrite(10, HIGH);
- dmd.scanDisplayBySPI();
- //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("0");
- lcd.setCursor(14,0);
- lcd.print("0");
- 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("0");
- lcd.setCursor(14,1);
- lcd.print("0");
- //DMD write names and zero score
- int zero = 0;
- dmd.drawString( 0,0, "Home", 4, GRAPHICS_NORMAL );
- //convert int to char type
- itoa(zero,top_set_char,10);
- dmd.drawString( 73,0, top_set_char, 1, GRAPHICS_NORMAL );
- itoa(zero,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(zero,bot_set_char,10);
- dmd.drawString( 73,9, bot_set_char, 1, GRAPHICS_NORMAL );
- itoa(zero,bot_score_char,10);
- dmd.drawString( 85,9, bot_score_char, 2, GRAPHICS_NORMAL );
- //
- delay(10);
- }
- /*--------------------------------------------------------------------------------------
- loop()
- --------------------------------------------------------------------------------------*/
- void loop()
- {
- //get the latest button pressed, also the buttonJustPressed, buttonJustReleased flags
- button = ReadButtons();
- digitalWrite(10, HIGH);
- switch( button )
- {
- case BUTTON_NONE:
- {
- break;
- }
- case BUTTON_RIGHT:
- {
- delay(400);
- button = ReadButtons();
- if (button == BUTTON_NONE && buttonWas2 == BUTTON_NONE)
- {
- //Increase 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;
- }
- // 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;
- }
- break;
- }
- case BUTTON_UP:
- {
- delay(400);
- button = ReadButtons();
- if (button == BUTTON_NONE && buttonWas2 == 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;
- }
- // 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;
- }
- break;
- }
- case BUTTON_DOWN:
- {
- delay(400);
- button = ReadButtons();
- if (button == BUTTON_NONE && buttonWas2 == 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;
- }
- // 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;
- }
- break;
- }
- case BUTTON_LEFT:
- {
- delay(400);
- button = ReadButtons();
- if (button == BUTTON_NONE && buttonWas2 == 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_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;
- }
- // 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;
- }
- break;
- }
- case BUTTON_SELECT:
- {
- delay(1000);
- button = ReadButtons();
- if (button == BUTTON_NONE && buttonWas2 == 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 Names? ");
- button = ReadButtons();
- int topbot = 1;
- while(button == BUTTON_SELECT)
- {
- button = ReadButtons();
- }
- int pos = 0;
- 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 > line_num)
- {
- team_num = 1;
- }
- int readvar;
- //Read team names into one char array, including line return characters (two at the end of each name except the last name).
- char team_name[12] = " ";
- if( team_num == 1 )
- {
- file.seekSet(0);
- int readsize = counter[0] - 2 ;
- readvar = file.read( team_name, readsize);
- }
- else if( team_num != 1 && team_num < line_num)
- {
- file.seekSet(counter[team_num-2]);
- int readsize = counter[team_num-1]-counter[team_num-2]-2;
- readvar = file.read( team_name, readsize);
- }
- else if( team_num == line_num )
- {
- file.seekSet(counter[team_num-2]);
- int readsize = counter[team_num-1]-counter[team_num-2]-1;
- readvar = file.read( team_name, readsize);
- }
- lcd.setCursor(0, pos);
- lcd.print(" ");
- lcd.setCursor(0, pos);
- lcd.print(team_name);
- //Write team name to DMD displays
- int teamline = 9*pos;
- dmd.drawString( 0, teamline, team_name, 11, GRAPHICS_NORMAL );
- }
- else if(button == BUTTON_LEFT)
- {
- team_num = team_num - 1;
- if (team_num == 0)
- {
- team_num = line_num;
- }
- int readvar;
- //Read team names into one char array.
- char team_name[12] = " ";
- if( team_num == 1 )
- {
- file.seekSet(0);
- int readsize = counter[0] - 2 ;
- readvar = file.read( team_name, readsize);
- }
- else if( team_num != 1 && team_num < line_num)
- {
- file.seekSet(counter[team_num-2]);
- int readsize = counter[team_num-1]-counter[team_num-2]-2;
- readvar = file.read( team_name, readsize);
- }
- else if( team_num == line_num )
- {
- file.seekSet(counter[team_num-2]);
- int readsize = counter[team_num-1]-counter[team_num-2]-1;
- readvar = file.read( team_name, readsize);
- }
- //Write team name to LCD screen
- lcd.setCursor(0, pos);
- lcd.print(" ");
- lcd.setCursor(0, pos);
- lcd.print(team_name);
- //Write team name to DMD displays
- int teamline = 9*pos;
- dmd.drawString( 0, teamline, team_name, 11, GRAPHICS_NORMAL );
- }
- delay(200);
- button = ReadButtons();
- }
- //Reset scores on LCD screeen
- lcd.setCursor(11,0);
- lcd.print(" ");
- lcd.setCursor(11,1);
- lcd.print(" ");
- lcd.setCursor(12,0);
- lcd.print("0");
- lcd.setCursor(14,0);
- lcd.print("0");
- lcd.setCursor(12,1);
- lcd.print("0");
- lcd.setCursor(14,1);
- lcd.print("0");
- //Reset score on DMD displays
- top_score = 0;
- bot_score = 0;
- top_set = 0;
- bot_set = 0;
- itoa(bot_score,bot_score_char,10);
- dmd.drawString( 85,9, bot_score_char, 2, GRAPHICS_NORMAL );
- itoa(top_score,top_score_char,10);
- dmd.drawString( 85,0, top_score_char, 2, GRAPHICS_NORMAL );
- itoa(bot_set,bot_set_char,10);
- dmd.drawString( 73,9, bot_set_char, 2, GRAPHICS_NORMAL );
- itoa(top_set,top_set_char,10);
- dmd.drawString( 73,0, top_set_char, 2, GRAPHICS_NORMAL );
- 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()
- {
- //save the latest button value, for change event detection next time round
- buttonWas2 = buttonWas;
- buttonWas = button;
- 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;
- }
- return( button );
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement