//Author: cantone-electonics
//More information welcome to : http://www.canton-electronics.com 
//Arduino 1.6.
//for Arduino pro mini
//4 bit Common Cathode Digital Tube Module test code,use Timer2

/*---Segment Display Screen----
--A--
F---B
--G--
E---C
--D-- 
 __  __   __  __
|__||__|.|__||__|
|__||__|'|__||__|
----------------------*/   
#include <FlexiTimer2.h>
#define uchar unsigned char 
#define uint  unsigned int

int display_dat; 
int key_value;

const int led = 13;

//Pin connected to latch of Digital Tube Module
const int latch = 8;
//Pin connected to click of Digital Tube Module
const int clock = 9;
//Pin connected to data of Digital Tube Module
const int data = 7;

const int K1 = 2;
const int K2 = 3;
const int K3 = 4;
const int K4 = 5;

const int INPUT1 = A1;
const int INPUT2 = A0;
const int INPUT3 = A3;
const int INPUT4 = A2;

const int j1 = 10;
const int j2 = 11;
const int j3 = 12;
const int j4 = 6;

int relay1_delay_time = 1;//1-9999 seconds,Modify the number change the delay time
int relay2_delay_time = 2;//1-9999 seconds,Modify the number change the delay time
int relay3_delay_time = 3;//1-9999 seconds,Modify the number change the delay time
int relay4_delay_time = 4;//1-9999 seconds,Modify the number change the delay time

int relay1_time_left;
int relay2_time_left;
int relay3_time_left;
int relay4_time_left;

int relay1_display;
int relay2_display;
int relay3_display;
int relay4_display;


/*       NO.:0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22 23 24 25 26 27 28      
/*Character :0,1,2,3,4,5,6,7,8,9,A, b, C, c, d, E, F, H, h, L, n, N, o, P, r, t, U, -,  ,*/
uchar  TUBE_SEG[] = 
{0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa7,0xa1,0x86,0x8e,0x89,0x8b,0xc7,0xab,0xc8,0xa3,0x8c,0xaf,0x87,0xc1,0xbf,0xff};//Common anode Digital Tube Character Gallery

uchar TUBE_NUM[8]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};//Tuble bit number

uchar dat_buf[8];

//***********************************************
// dat : Data to be displayed
// com_num :  Digital Tube Common 
//***********************************************
uchar dat;
uchar com_num;
void TubeDisplayOneBit()
{
  uchar tube_dat;
  uchar bit_num;
   tube_dat = TUBE_SEG[dat]; //Common Cathode Digital Tube,bit negated
   bit_num = ~TUBE_NUM[com_num];
  
    //ground latchPin and hold low for as long as you are transmitting
     digitalWrite(latch, LOW);
     
     shiftOut(data, clock, MSBFIRST, bit_num);
     shiftOut(data, clock, MSBFIRST, tube_dat);   
     
     //return the latch pin high to signal chip that it 
     //no longer needs to listen for information
     digitalWrite(latch, HIGH);
}

uchar OneSecondCnt;
void TubeDisplay4Bit(void)
{
  if(com_num<3) com_num ++;else com_num=0;
  dat = dat_buf[com_num];
  TubeDisplayOneBit();
  
  OneSecondCnt--;
  if(OneSecondCnt==0)
  {
    OneSecondCnt = 200;
    if(relay1_time_left>0){relay1_time_left--; relay1_display = relay1_time_left;} else {digitalWrite(j1, LOW);relay1_display = relay1_delay_time;}
    if(relay2_time_left>0){relay2_time_left--; relay2_display = relay2_time_left;} else {digitalWrite(j2, LOW);relay2_display = relay2_delay_time;}
    if(relay3_time_left>0){relay3_time_left--; relay3_display = relay3_time_left;} else {digitalWrite(j3, LOW);relay3_display = relay3_delay_time;}
    if(relay4_time_left>0){relay4_time_left--; relay4_display = relay4_time_left;} else {digitalWrite(j4, LOW);relay4_display = relay4_delay_time;}
   
  }
  
  if(key_value==0) display_dat = relay1_display; 
  else if(key_value==1) display_dat = relay2_display;
  else if(key_value==2) display_dat = relay3_display;  
  else  display_dat = relay4_display;
  
   dat_buf[0] = display_dat/1000;
   display_dat = display_dat%1000;
   dat_buf[1] = display_dat/100;
   display_dat = display_dat%100;
   dat_buf[2] = display_dat/10;
   dat_buf[3] = display_dat%10;
  
}

void setup() {
   //set pins to output because they are addressed in the main loop
   pinMode(led, OUTPUT);
   
   
   pinMode(latch, OUTPUT);
   pinMode(clock, OUTPUT);
   pinMode(data, OUTPUT);
   
   pinMode(K1, INPUT);
   pinMode(K2, INPUT);
   pinMode(K3, INPUT);
   pinMode(K4, INPUT);
   
   pinMode(INPUT1, INPUT);
   pinMode(INPUT2, INPUT);
   pinMode(INPUT3, INPUT);
   pinMode(INPUT4, INPUT);
   
   pinMode(j1, OUTPUT);
   pinMode(j2, OUTPUT);
   pinMode(j3, OUTPUT);
   pinMode(j4, OUTPUT);
   
  FlexiTimer2::set(5, 1.0/1000, TubeDisplay4Bit); // call every 5ms "ticks"
  FlexiTimer2::start();
  
  digitalWrite(j1, LOW);
  digitalWrite(j2, LOW);
  digitalWrite(j3, LOW);
  digitalWrite(j4, LOW);
  
  digitalWrite(INPUT1, HIGH);
  digitalWrite(INPUT2, HIGH);
  digitalWrite(INPUT3, HIGH);
  digitalWrite(INPUT4, HIGH);
  
  
  digitalWrite(K1, HIGH);
  digitalWrite(K2, HIGH);
  digitalWrite(K3, HIGH);
  digitalWrite(K4, HIGH);
  
  key_value = 0;
}

void loop() {
  
  while(1)
  {

    if(digitalRead(INPUT1)==LOW) {relay1_time_left = relay1_delay_time;digitalWrite(j1, HIGH);digitalWrite(led, HIGH);}else { digitalWrite(led, LOW);}

    if(digitalRead(INPUT2)==LOW) {relay2_time_left = relay2_delay_time;digitalWrite(j2, HIGH);digitalWrite(led, HIGH);}else { digitalWrite(led, LOW);}

    if(digitalRead(INPUT3)==LOW) {relay3_time_left = relay3_delay_time;digitalWrite(j3, HIGH);digitalWrite(led, HIGH);}else { digitalWrite(led, LOW);}

    if(digitalRead(INPUT4)==LOW) {relay4_time_left = relay4_delay_time;digitalWrite(j4, HIGH);digitalWrite(led, HIGH);}else { digitalWrite(led, LOW);}
    
    if     (digitalRead(K1)==LOW) {key_value = 0; } 
    else if(digitalRead(K2)==LOW) {key_value = 1; } 
    else if(digitalRead(K3)==LOW) {key_value = 2; } 
    else if(digitalRead(K4)==LOW) {key_value = 3; } 

      
  }
} 


