MUstar

IoT 아두이노 0707 - 프로젝트(+주석)

Jul 7th, 2017
98
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <LiquidCrystal.h>
  2.  #include <MsTimer2.h>
  3.  
  4. int pin_LCD_DATA[8] = {A8,A9,A10,A11,A12,A13,A14,A15};
  5. int pin_LCD_CTRL[3] = {A5,A6,A7};
  6. LiquidCrystal lcd(A5,A6,A7,A8,A9,A10,A11,A12,A13,A14,A15);
  7. int sw[6] = {22,24,26,28,30}; // Switch
  8. int joy_sw = 48; //Joystick Switch
  9. const int joy_x = A3; //JoyStick X_AXIS
  10. const int joy_y = A4; //joyStic Y_AXIS
  11. const int piezo = 2;
  12. int pin_FND_DATA[8] = {23, 25, 27, 29, 31, 33, 35, 37};
  13. int pin_FND_SELECT[6] = {39, 41, 43, 45, 47, 49};
  14. uint8_t FND_data[16] = {0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x27, 0x7F, 0x6F,0x77, 0x7C, 0x39, 0x5E, 0x79, 0x1};
  15. uint8_t data_Cipher[6] = {0,72,137,0,0,0};
  16.  
  17. void setup() {
  18.   int i;
  19.   lcd.begin(16,2);
  20.   pinMode(sw,INPUT_PULLUP);
  21.   pinMode(sw,INPUT_PULLUP);
  22.   pinMode(joy_sw, INPUT_PULLUP);
  23.   randomSeed(analogRead(0));  
  24.     for(i=0; i<8; i++)
  25.     pinMode(pin_FND_DATA[i], OUTPUT);
  26.   for(i=0; i<6; i++)
  27.     pinMode(pin_FND_SELECT[i], OUTPUT);
  28.   FND_select(0x3F);
  29.   MsTimer2::set(2, FND_ISR);
  30.   MsTimer2::start();
  31.  
  32. }
  33. void loop() {
  34.    int h=0 , w=0, r_h=0 , r_w=0, cnt=0; //음식 위치와, 랜덤으로 떨어질 위치
  35.    int eat_h[8], eat_w[8], eat_f[8];  //먹었을때 배열에다가 1저장
  36.    double sec=0;
  37.    while(1)
  38.    {
  39.     if(cnt==8){cnt=0; break;}
  40.     int cksum=0;
  41.     r_h = random(1,15);  //랜덤함수를 변수에 저장
  42.     r_w = random(0,2);   //랜덤함수를 변수에 저장
  43.     eat_h[cnt]=r_h; eat_w[cnt]=r_w;
  44.     for(int i=0;i<cnt;i++)
  45.     {
  46.       if((r_h==eat_h[i])&&(r_w==eat_w[i])){cksum=1; i=8;}
  47.     }
  48.     if(cksum==1) cnt=cnt;
  49.     else cnt++;
  50.    }
  51.    
  52.    for(int i=0;i<8;i++)
  53.    {
  54.       eat_f[i]=0;
  55.    }
  56.    
  57.    intro();
  58.    while(2){
  59.      int num_x = analogRead(joy_x); //조이스틱값을 변수에 저장
  60.      int num_y = analogRead(joy_y);  // 조이스틱값을 변수에 저장
  61.      lcd.clear();
  62.      lcd.setCursor(h,w);
  63.      lcd.print("*");
  64.      foods(h,w,eat_h,eat_w,eat_f);
  65.      for(int i=0; i<8; i++)
  66.      {
  67.       if(eat_f[i]==1) cnt++;
  68.      }
  69.      delay(200);
  70.      sec+=200;
  71.      if(cnt==8){FND_output(sec/1000,cnt); gameover(sec); break;}
  72.      else {FND_output(sec/1000,cnt);cnt=0;}
  73.      
  74.      if((num_y>=0)&&(num_y<500)) {w=0;}//UP  , x축,y축 범위안에 들어오면 위로!!
  75.      if((num_y>530)&&(num_y<=1024)) {w=1;}//DOWN, x축,y축 범위안에 들어오면 아래로!!
  76.      if((num_x>530)&&(num_x<=1024)) {if(h<=0){continue;}h--;}//LEFT, x축,y축 범위안에 들어오면 왼쪽으로!!
  77.      if((num_x>=0)&&(num_x<500)) {if(h>=15){continue;}h++;}//RIGHT, x축,y축 범위안에 들어오면 오른쪽으로!!
  78.    }
  79. }
  80.  
  81. int foods(int *h,int *w,int *eat_h, int *eat_w,int *eat_f){
  82.    for(int i=0;i<8;i++)
  83.    {
  84.      lcd.setCursor(eat_h[i],eat_w[i]);
  85.      if((eat_f[i]==0)&&((h==eat_h[i])&&(w==eat_w[i]))){lcd.print(""); eat_f[i]=1; sound();}  // 0~7배열에 전부 0이 들어가있는데 조이스틱으로 움직이고 있는 우리의 위치와 랜덤함수로 인해 랜덤하게 떨어진 음식이랑 좌표가 같으면 배열에 1이 들어가 카운트하고 소리나옴.
  86.      else if(eat_f[i]==1){lcd.print(""); }
  87.      else lcd.print("=");
  88.    }
  89. }
  90. int intro(void){   //처음화면
  91.   while(3){
  92.     lcd.clear();
  93.     lcd.setCursor(0,0);
  94.     lcd.print("=====MOZZI!=====");
  95.     lcd.setCursor(0,1);
  96.     //lcd.print("Press UP 2 START");
  97.     lcd.print("Press 2 JOYSTICK");
  98.     if(digitalRead(joy_sw) == LOW) break;
  99.     delay(100);
  100.   }
  101. }
  102. int gameover(double sec){  //게임이 종료되고 게임 클리어 화면 출력, 게임하는동안 걸린 시간 출력
  103.   while(4)
  104.   {
  105.     lcd.clear();
  106.     lcd.setCursor(0,0);  
  107.     lcd.print("GAME CLEAR!");  
  108.     lcd.setCursor(0,1);
  109.     lcd.print("PlayTime:");
  110.     lcd.setCursor(9,1);
  111.     lcd.print(sec/1000);  
  112.     lcd.print("s");
  113.     if(digitalRead(joy_sw) == LOW){break;}
  114.     delay(100);
  115.   }
  116. }
  117.  
  118. int sound(void)  // 음식먹을때 나오는 사운드
  119. {
  120.   //lcd.clear();
  121.  tone(piezo,8);  delay(20); noTone(piezo);//delay(100);
  122.  tone(piezo,1319); delay(20); noTone(piezo);//delay(100);
  123.  tone(piezo,850); delay(20); noTone(piezo);//delay(100);
  124. // noTone(pin_PIEZO);
  125. }
  126.  
  127. int FND_output(long sec,int cnt)  // FND 게임하는 동안 걸리시간을 실시간으로 출력
  128. {
  129.   long temp_num = sec;
  130.   data_Cipher[0] = cnt;
  131.   //data_Cipher[1] = 72;
  132.   //data_Cipher[2] = 137;
  133.   temp_num = temp_num%1000; data_Cipher[3] = temp_num/100;    // 과제 타이머를 응용해서 사용
  134.   temp_num = temp_num%100;  data_Cipher[4] = temp_num/10;
  135.   data_Cipher[5] =  temp_num%10;
  136. }
  137.  
  138. void FND_select(uint8_t da)
  139. {
  140.   for(int i=0; i<6; i++)
  141.     digitalWrite(pin_FND_SELECT[i], (da>>i) & 0x01);
  142. }
  143.  
  144. void FND_data_out(uint8_t da)
  145. {
  146.   for(int i=0; i<8; i++)
  147.     digitalWrite(pin_FND_DATA[i], (da>>i) & 0x01);
  148. }
  149.  
  150. void FND_ISR(void)
  151. {
  152.   static int i=0;
  153.   FND_data_out(0);
  154.   FND_select(~(0x001<<i));
  155.   FND_data_out(FND_data[data_Cipher[i]]);
  156.   if(i==5){i=0;}
  157.   else{i++;}
  158. }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×