MUstar

IoT 아두이노 0628 - FND EX_HD2

Jul 2nd, 2017
88
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <MsTimer2.h>
  2.  
  3. int sw[5] ={2,3,4,5,6};
  4. int vib = 8;
  5. int LED[8] = {22, 24, 26, 28, 30, 32, 34, 36};
  6. int pin_FND_DATA[8] = {23, 25, 27, 29, 31, 33, 35, 37};
  7. int pin_FND_SELECT[6] = {39, 41, 43, 45, 47, 49};
  8. //uint8_t FND_data[16] = {0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x27, 0x7F, 0x6F,0x77, 0x7C, 0x39, 0x5E, 0x79, 0x1};
  9. uint8_t FND_data[16] = {63,6,91,79,102,109,125,39,127,111,119,124,57,94,121,1};
  10. uint8_t data_Cipher[6] = {0,0,0,0,0,0};
  11.  
  12. void setup() {
  13.   int i;
  14.   for(i=0; i<8; i++)
  15.   {
  16.     pinMode(LED[i],OUTPUT);
  17.     digitalWrite(LED[i], LOW);
  18.   }
  19.   for(i=0; i<5; i++)
  20.     pinMode(sw[i],INPUT_PULLUP);
  21.   for(i=0; i<8; i++)
  22.     pinMode(pin_FND_DATA[i], OUTPUT);
  23.   for(i=0; i<6; i++)
  24.     pinMode(pin_FND_SELECT[i], OUTPUT);
  25.   pinMode(vib, OUTPUT);
  26.   FND_select(0x3F);
  27.   MsTimer2::set(2, FND_ISR);
  28.   MsTimer2::start();
  29. }
  30.  
  31. void loop() {
  32.   clear();
  33.   long num_h=0, num_m=0, num_s=0; //시간설정용 변수
  34.   long long  pss_h=0, pss_m=0, pss_s=0, pss_t = 0; //실제타이머용변수
  35.   int stc=0, led_num=0; //기타 컨트롤용 변수
  36.   while(1)
  37.   {
  38.     if(digitalRead(sw[0]) == HIGH) break; //입력후 입력창에서 나가기
  39.     if(digitalRead(sw[1]) == HIGH){clear(); num_h=0; num_m=0; num_s=0;}
  40.     if(digitalRead(sw[2]) == HIGH){if(num_h<99){num_h+=1;}else num_h=0;delay(200);} //시입력
  41.     if(digitalRead(sw[4]) == HIGH){if(num_h<59){num_m+=1;}else num_m=0;delay(200);} //분입력
  42.     if(digitalRead(sw[3]) == HIGH){if(num_h<59){num_s+=1;}else num_s=0;delay(200);} //초입력
  43.    
  44.     data_Cipher[0] = num_h/10; data_Cipher[1] = num_h%10;
  45.     data_Cipher[2] = num_m/10; data_Cipher[3] = num_m%10;
  46.     data_Cipher[4] = num_s/10; data_Cipher[5] = num_s%10;
  47.   }
  48.   clear();
  49.   delay(500);
  50.   pss_t=(num_h*3600) + (num_m*60) + num_s; //입력한 값을 초로 변환.
  51.   while(2)
  52.   {
  53.     if(digitalRead(sw[0]) == HIGH){stc=0; continue;}
  54.     if(digitalRead(sw[1]) == HIGH)
  55.     {
  56.       int sw_sec=0;
  57.       while(3){sw_sec++; delay(1000); if(digitalRead(sw[1]) == LOW) break;}
  58.       if(sw_sec>1) break;
  59.       else stc=1;
  60.     }
  61.     if(digitalRead(sw[2]) == HIGH){pss_t+=3600; delay(200);}
  62.     if(digitalRead(sw[4]) == HIGH){pss_t+=60; delay(200);}
  63.     if(digitalRead(sw[3]) == HIGH){pss_t++; delay(200);}
  64.  
  65.     LED_control(0);
  66.     if(stc==1){LED_control(255); continue;}
  67.     else if(pss_t==0)
  68.     {
  69.         clear(); //FND값클리어
  70.         delay(500);
  71.         digitalWrite(vib,HIGH);
  72.         delay(500);  
  73.         continue;
  74.     }
  75.     else
  76.     {
  77.       pss_h = pss_t/3600;            //시구하기
  78.       pss_m = (pss_t/60)-(pss_h*60); //분구하기
  79.       pss_s = pss_t%60;              //초구하기
  80.       pss_t--; //1초씩 아래로
  81.       data_Cipher[0] = pss_h/10; data_Cipher[1] = pss_h%10; //시출력
  82.       data_Cipher[2] = pss_m/10; data_Cipher[3] = pss_m%10; //분출력
  83.       data_Cipher[4] = pss_s/10; data_Cipher[5] = pss_s%10; //초출력
  84.     }
  85.     delay(1000); //최소단위가 1초이므로 Dealy을 1000ms로 설정.
  86.   }
  87. }
  88.  
  89. int clear(void) //FND화면 값을 시원하게 클리어
  90. {
  91.   data_Cipher[0] = 0;
  92.   data_Cipher[1] = 0;
  93.   data_Cipher[2] = 0;
  94.   data_Cipher[3] = 0;
  95.   data_Cipher[4] = 0;
  96.   data_Cipher[5] = 0;
  97.   LED_control(0);
  98.   digitalWrite(vib,LOW);
  99. }
  100.  
  101.  
  102. void LED_control(uint8_t da)
  103. {
  104.   for(int i=0; i<8; i++)
  105.   {
  106.     digitalWrite(LED[i], (da >> i) & 0x1);
  107.   }
  108. }
  109.  
  110. void FND_select(uint8_t da)
  111. {
  112.   for(int i=0; i<6; i++)
  113.     digitalWrite(pin_FND_SELECT[i], (da>>i) & 0x01);
  114. }
  115.  
  116. void FND_data_out(uint8_t da)
  117. {
  118.   for(int i=0; i<8; i++)
  119.     digitalWrite(pin_FND_DATA[i], (da>>i) & 0x01);
  120. }
  121.  
  122. void FND_ISR(void)
  123. {
  124.   static int i=0;
  125.   FND_data_out(0);
  126.   FND_select(~(0x001<<i));
  127.   FND_data_out(FND_data[data_Cipher[i]]);
  128.   if(i==5){i=0;}
  129.   else{i++;}
  130. }
RAW Paste Data