#include <TimerOne.h>
#include <Keypad.h>
const byte KeyROWS = 4; //four rows
const byte KeyCOLS = 4; //four columns
//define the cymbols on the buttons of the keypads
char hexaKeys[KeyROWS][KeyCOLS] = {
{'1','2','3','a'},
{'4','5','6','b'},
{'7','8','9','c'},
{'*','0','#','d'}
};
byte rowPins[KeyROWS] = {37, 36, 35, 34}; //connect to the row pinouts of the keypad
byte colPins[KeyCOLS] = {33, 32, 31, 30}; //connect to the column pinouts of the keypad
char keypressed;
//initialize an instance of class NewKeypad
Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, KeyROWS, KeyCOLS);
// mapa do hardware
int latch=9; //74HC595 pin 9 STCP
int clock=10; //74HC595 pin 10 SHCP
int data=8; //74HC595 pin 8 DS
#define pinDig0 22 //Pino de ativação do Display DIG 0 LSB
#define pinDig1 23 //Pino de ativação do Display DIG 1
#define pinDig2 24 //Pino de ativação do Display DIG 2
#define pinDig3 25 //Pino de ativação do Display DIG 3
#define pinDig4 26 //Pino de ativação do Display DIG 4
#define pinDig5 27 //Pino de ativação do Display DIG 5
#define pinDig6 28 //Pino de ativação do Display DIG 6
#define pinDig7 29 //Pino de ativação do Display DIG 7
#define qtdDigitos 8 //Numero de Digitos do display 1
byte pinDigito[qtdDigitos] = {pinDig0, pinDig1, pinDig2, pinDig3, pinDig4, pinDig5, pinDig6, pinDig7};
// variáveis
// bool armado = false;
// int DigtRound = 1;
unsigned long frameRate = 3000; // milisecs de intervalo entre varreduras
unsigned int frameCount = 0; // variavel que guarda o digito da vez a varrer
unsigned int blinkfreq = 60; // define o periodo do blinker
unsigned int timeFineAdj = 333; // ajusta o passo do relogio 333
// array to hold screen buffer area
String screenBuffer;
String display[2] = { "" , "" };
String PassTeam[2] = { "123a" , "a321"};
bool ClockGoTeam0 = false;
bool ClockGoTeam1 = false;
bool ClockStartedTeam0 = false;
bool ClockStartedTeam1 = false;
bool blinker = true;
bool TextAtDisp[2] = { true, true };
bool Gamestart = false;
unsigned long Tics[2] = { 0 , 0 };
unsigned long TicsGame = 0 ;
unsigned long segundosTeam[2] = { 0 , 0 };
unsigned long segundosGame = 0 ;
unsigned long t = 0;
int Displayshift = 0;
int noOfSegments = 8;
unsigned int ActiveDisplay = 0b11111110;
void setup() {
Timer1.initialize(frameRate); // inicializa scan com período de [frameRate] miliseg
Timer1.attachInterrupt(segmentScan); // anexar função a ser excutada a cada período
for ( int nL=0 ; nL < 8 ; nL++){
pinMode (pinDigito[nL], OUTPUT);
digitalWrite (pinDigito[nL], HIGH);
}
pinMode(latch,OUTPUT);
pinMode(clock,OUTPUT);
pinMode(data,OUTPUT);
Serial.begin(9600);
}
void loop()
{
while ( !Gamestart )
{
keypressed = customKeypad.getKey();
if ( char (keypressed) == 'a')
{
Gamestart = true;
Displayshift = 0 ;
}
DoubleDisplayText ( " -- benvindo ao jogo de aniversario da equipe bravo riot airsoft - pressione (a) para iniciar -- " , 1);
}
if (Tics[0] > 0 ) ClockUp(0); else DisplayText( 0, "stop", 100);
if (Tics[1] > 0 ) ClockUp(1); else DisplayText( 1, "stop", 100);
keypressed = customKeypad.getKey();
if ( char (keypressed) == '*')
{
SUB_enterdata(0) ;
TestPass(0);
if (!ClockGoTeam0) DisplayText ( 0 , " errooouuu", 400);
}
if ( char (keypressed) == '#')
{
SUB_enterdata(1) ;
TestPass(1);
if (!ClockGoTeam1) DisplayText ( 1 , " errooouuu", 400);
}
}
void SUB_enterdata(int Team)
{
int carac;
char x; // char key;
display[Team] = "----";
long timmerstart;
long timeelapse;
boolean exit = false;
TextAtDisp[Team]=true;
for (carac = 1; carac <= 4; carac ++)
{
timmerstart = millis(); // guarda inicio do tempo de tecla
do
{
x = customKeypad.getKey(); // verifica teclado
timeelapse = millis()-timmerstart; // mede tempo decorrido desde a ultima tecla
if (timeelapse > 1000) // se passou o tempo maximo
{
carac = 4 ; // termina o loop for
exit = true ; // termina o loop while
}
if (Team == 0 && ClockGoTeam1 == true ) ClockUp(1);
if (Team == 1 && ClockGoTeam0 == true ) ClockUp(0);
} while (!x && !exit );
if (x) display[Team] = display[Team].substring(1,4) + x; // atualiza string somente se houve press de tecla
}
delay(250);
TextAtDisp[Team]=false;
}
void TestPass(int Team)
{
if ( display[Team] == PassTeam[Team])
{
if ( Team == 0)
{
ClockGoTeam0 = true;
ClockGoTeam1 = false;
}
else
{
ClockGoTeam0 = false;
ClockGoTeam1 = true;
}
}
}
void ClockUp(int Team)
{
display[Team].setCharAt( 0 , (segundosTeam[Team] / 60 / 10 ) +48 );
display[Team].setCharAt( 1 , (segundosTeam[Team] / 60 % 10 ) +48 );
display[Team].setCharAt( 2 , (segundosTeam[Team] % 60 / 10 ) +48 );
display[Team].setCharAt( 3 , (segundosTeam[Team] % 60 % 10 ) +48 );
}
void DisplayText(int Team, String Data, int Tempo)
{
int i;
int Caracs = Data.length();
TextAtDisp[Team] = true;
if (Caracs <= 4)
display[Team] = Data;
else
{
for (i= 0 ; i <= (Caracs-4) ; i++)
{
display[Team] = Data.substring(i,i+4);
delay(150);
}
}
delay (Tempo);
TextAtDisp[Team] = false;
}
void DoubleDisplayText( String Data, int Tempo)
{
int i;
int Caracs = Data.length();
if (Caracs <= 4)
{
display[1] = Data;
display[0] = "";
}
else
{
i = Displayshift;
display[0] = Data.substring(i,i+4);
display[1] = Data.substring(i+4,i+8);
delay(300);
Displayshift++;
if (Displayshift > Caracs-8) Displayshift = 0;
}
delay (Tempo);
}
void segmentScan() // Está OK, apenas escrever no campo Display 1 ou 2
{
int digit;
// for statement turns on one segment at a time for each of the (n) segments and also
// send data to the data port one at a time
screenBuffer = display[0] + display[1]; // monta a string a apresentar
PORTA = 0xFF; // Turn off control pins
// Point to one character and get the segment equivalent for that character
// Afterwards, send the segment equivalent to SUB_scanleds
// SUB_scanleds (getSegmentEquiv(screenBuffer[frameCount]));
digit = getSegmentEquiv(screenBuffer[frameCount]);
if (frameCount == 1 && !TextAtDisp[0] && ( ClockGoTeam0 && blinker ||
!ClockGoTeam0 && ClockStartedTeam0 )) bitSet(digit, 7); // Pto Display 0
if (frameCount == 5 && !TextAtDisp[1] &&( ClockGoTeam1 && blinker || !ClockGoTeam1 && ClockStartedTeam1 )) bitSet(digit, 7); // Pto Dispaly 1
SUB_scanleds (digit);
// send control signal to turn on one segment
PORTA = ActiveDisplay;
// shift the “on” scan bit for each of the six segments to the right by 1
// to turn a specific segment
ActiveDisplay <<= 1; // shift once to the left
// clear last bit in ActiveDisplay to prevent noise or glitches when displaying
bitSet(ActiveDisplay, 0); // bitClear has been changed to bitSet for CC
frameCount++; // aponta para o próximo dígito
if (frameCount==noOfSegments) // se for o último dígito
{
frameCount = 0; // retorna ao dígito 0
ActiveDisplay = 0b11111110;
}
if ( ClockGoTeam0 )
{
ClockStartedTeam0 = true;
Tics[0]++;
segundosTeam[0] = Tics[0] / timeFineAdj; // faz a contagem de tempo
}
if ( ClockGoTeam1 )
{
ClockStartedTeam1 = true;
Tics[1]++;
segundosTeam[1] = Tics[1] / timeFineAdj ; // faz a contagem de tempo DO TIME!
}
if ( t < blinkfreq ) t++; else {t=0; blinker=!blinker;}
if ( Gamestart )
{
TicsGame++;
segundosGame = TicsGame / timeFineAdj ; // faz a contagem do tempo DO JOGO !
}
} // end scan
void SUB_scanleds(unsigned char num)
{
digitalWrite(latch,LOW);
shiftOut(data,clock,MSBFIRST,num);
digitalWrite(latch,HIGH);
}
int getSegmentEquiv(char value) // for common anode
{
int segValue = 0;
// for common anode
if (value == '0') segValue = 0x3F;// 0x79; // end 0
else if (value == '1') segValue = 0x06;//0xF9; // 0x06;// end 1
else if (value == '2') segValue = 0x5B;//0xA4; //0x5B;// end 2llkl
else if (value == '3') segValue = 0x4F;//0xB0; // 0x4F;// end 3
else if (value == '4') segValue = 0x66;//0x99; // 0x66;// end 4
else if (value == '5') segValue = 0x6D;//0x92; // 0x6D;// end 5
else if (value == '6') segValue = 0x7D;//0x82; // 0x7D;// end 6
else if (value == '7') segValue = 0x07;//0xF8; // 0x07;// end 7
else if (value == '8') segValue = 0x7F;//0x80; // 0x7F;// end 8
else if (value == '9') segValue = 0x6F;//0x90; // 0x6F;// end 9
else if (value == ' ') segValue = 0x00; //0xFF;// end space
else if (value == 'a') segValue = 0x77; //0x20;
else if (value == 'b') segValue = 0x7C; //0x03;
else if (value == 'c') segValue = 0x58; //0x27;
else if (value == 'd') segValue = 0x5E; //0x21;
else if (value == 'e') segValue = 0x79; //0x04;
else if (value == 'f') segValue = 0x71; //0x0E;
else if (value == 'g') segValue = 0x6F; //0x10;
else if (value == 'h') segValue = 0x74; //0x0B;
else if (value == 'i') segValue = 0x04; //0x7B;
else if (value == 'j') segValue = 0x0E; //0x71;
else if (value == 'k') segValue = 0x7A; //0xFF;
else if (value == 'l') segValue = 0x38; //0x47;
else if (value == 'm') segValue = 0x37; //0xFF;
else if (value == 'n') segValue = 0x54; //0x2B;
else if (value == 'o') segValue = 0x5C;//0x23;
else if (value == 'p') segValue = 0x73;//0x08;
else if (value == 'q') segValue = 0x67; //0xFF;
else if (value == 'r') segValue = 0x50; //0x2F;
else if (value == 's') segValue = 0x6D; //0x92;
else if (value == 't') segValue = 0x78; //0x07;
else if (value == 'u') segValue = 0x1C; //0x63;
else if (value == 'v') segValue = 0x3E; //0xFF;
else if (value == 'w') segValue = 0x1D; //0xFF;
else if (value == 'x') segValue = 0x70; //0xFF;
else if (value == 'y') segValue = 0x6E; //0x91;
else if (value == 'z') segValue = 0x49; //0xFF;
else if (value == '_') segValue = 0x08; //0xFF;
else if (value == '-') segValue = 0x40; //0xFF;
else if (value == '#') segValue = 0x36; //0xFF;
else if (value == '*') segValue = 0x49; //0xFF;
else if (value == '(') segValue = 0x39; //0xFF;
else if (value == ')') segValue = 0x0F; //0xFF;
else segValue = 0xD3; //0xFF; // use ' '
return segValue;
} // end function segment Equivalent FIM3