Advertisement
Guest User

Arduino Bomb 0.5 AnalogRead

a guest
Feb 26th, 2012
491
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 12.28 KB | None | 0 0
  1. #include <LiquidCrystal.h>;
  2.  
  3.  
  4. //
  5. int  adc_key_val[5] ={30, 150, 360, 535, 760 };
  6. int NUM_KEYS = 5;
  7. int adc_key_in;
  8. int key=-1;
  9. int oldkey=-1;
  10.  
  11. //Buttons for lcd shield
  12. int BT_RIGHT = 0;
  13. int BT_UP = 1;
  14. int BT_DOWN = 2;
  15. int BT_LEFT = 3;
  16. int BT_RED = 4;   // Ok key  
  17. int BT_DEFUSER = 40;   // not implemented
  18.  
  19.                    
  20. //leds
  21. const int ledVerde = 3;  // para el shield lcd
  22. const int ledRojo = 2 ;
  23. const int ledDefuser = 2;
  24. //
  25.  
  26. //LSD
  27. LiquidCrystal lcd( 8, 9, 4, 5, 6, 7);
  28.  
  29. //TIME INTS
  30. int MINUTOSJUEGO = 45;
  31. int MINUTOSBOMBA = 4;
  32. int SEGUNDOSACTIVAR = 10;
  33.  
  34. // SOUND TONES
  35. int tonepin = 13; // si no tiene sonido, el led 13 funcionará
  36. int tonoPitido = 3000;
  37. int tonoAlarma1 = 700;
  38. int tonoAlarma2 = 2600;
  39. int tonoActivada = 1330;
  40.  
  41. void setup(){
  42.  
  43.   lcd.begin(16, 2);
  44.   lcd.setCursor(4,0);
  45.   lcd.print("MANIACS");
  46.   lcd.setCursor(1,1);
  47.   lcd.print("TACTICAL UNIT");
  48.   delay(2000);
  49.   // CONFIGURE THE BARS OF PROGRESS BAR
  50.   byte bar1[8] = {
  51.     B10000,
  52.     B10000,
  53.     B10000,
  54.     B10000,
  55.     B10000,
  56.     B10000,
  57.     B10000,
  58.   };
  59.   byte bar2[8] = {
  60.     B11000,
  61.     B11000,
  62.     B11000,
  63.     B11000,
  64.     B11000,
  65.     B11000,
  66.     B11000,
  67.   };
  68.   byte bar3[8] = {
  69.     B11100,
  70.     B11100,
  71.     B11100,
  72.     B11100,
  73.     B11100,
  74.     B11100,
  75.     B11100,
  76.   };
  77.   byte bar4[8] = {
  78.     B11110,
  79.     B11110,
  80.     B11110,
  81.     B11110,
  82.     B11110,
  83.     B11110,
  84.     B11110,
  85.   };
  86.   byte bar5[8] = {
  87.     B11111,
  88.     B11111,
  89.     B11111,
  90.     B11111,
  91.     B11111,
  92.     B11111,
  93.     B11111,
  94.   };
  95.   lcd.createChar(0,bar1);
  96.   lcd.createChar(1,bar2);
  97.   lcd.createChar(2,bar3);
  98.   lcd.createChar(3,bar4);
  99.   lcd.createChar(4,bar5);
  100.  
  101. }
  102.  
  103. // initialize the library with the numbers of the interface pins
  104. void loop(){
  105.   menuPrincipal();
  106.   }
  107.  
  108. void menuPrincipal(){   //MAIN MENU
  109.  
  110.   //Draw menu
  111.   lcd.clear();
  112.   lcd.setCursor(0, 0);
  113.   int i=0;
  114.   char* menu1[]={
  115.     "Custom Game","Quick Game", "Config"  }; // HERE YOU CAN ADD MORE ITEMS ON THE MAIN MENU
  116.   lcd.print(menu1[i]);
  117.  
  118.   while(1){  
  119.  
  120.     if(BT_UP == get_key() && i>0){
  121.       tone(tonepin,2400,30);
  122.       i--;
  123.       lcd.clear();  
  124.       lcd.print(menu1[i]);
  125.       delay(500);
  126.  
  127.     }
  128.     if(BT_DOWN == get_key() && i<2){
  129.       tone(tonepin,2400,30);
  130.       i++;
  131.       lcd.clear();  
  132.       lcd.print(menu1[i]);
  133.       delay(500);
  134.     }
  135.  
  136.     if(BT_RED == get_key()){
  137.       tone(tonepin,2400,30);
  138.       lcd.clear();
  139.       switch (i){
  140.  
  141.       case 0:
  142.         configJuegoRapido();
  143.         break;
  144.  
  145.       case 1:
  146.         configJuegoRapido();
  147.         break;
  148.  
  149.       case 2:
  150.         config();
  151.         break;        
  152.  
  153.       }
  154.     }
  155.   }
  156. }
  157.  
  158. void juegoCustom(){
  159.  
  160.   game();
  161.  
  162. }
  163.  
  164. void config(){
  165.  
  166.   // need to implement a read and write config function... to terminate this option
  167.  
  168.     lcd.setCursor(0,0);
  169.     lcd.print("config menu test");
  170.     delay(3000);
  171.     menuPrincipal();
  172.  
  173. }
  174.  
  175. void game(){
  176.   delay(500);
  177.   while(!isPressed(BT_RED)){
  178.     lcd.clear();
  179.     lcd.setCursor(0,0);
  180.     lcd.print("Ready");
  181.     lcd.setCursor(0,1);
  182.     lcd.print("to begin");
  183.     delay(1000);
  184.     lcd.clear();
  185.     lcd.setCursor(0,0);
  186.     lcd.print("Push RED button");
  187.     lcd.setCursor(0,1);
  188.     lcd.print("to Start");
  189.     delay(1000);
  190.   }
  191.  
  192.   lcd.clear();
  193.   lcd.setCursor(0,0);
  194.   lcd.print("Starting Game");
  195.   lcd.setCursor(0,1);
  196.   tone(tonepin,2000,100);
  197.   lcd.print(" IN 5 ");
  198.   delay(1000);
  199.   lcd.clear();
  200.   lcd.setCursor(0,0);
  201.   lcd.print("Starting Game");
  202.   lcd.setCursor(0,1);
  203.   tone(tonepin,2000,100);
  204.   lcd.print(" IN 4 ");
  205.   delay(1000);
  206.   lcd.clear();
  207.   lcd.setCursor(0,0);
  208.   lcd.print("Starting Game");
  209.   lcd.setCursor(0,1);
  210.   tone(tonepin,2000,100);
  211.   lcd.print(" IN 3 ");
  212.   delay(1000);
  213.   lcd.clear();
  214.   lcd.setCursor(0,0);
  215.   lcd.print("Starting Game");
  216.   lcd.setCursor(0,1);
  217.   tone(tonepin,2000,100);
  218.   lcd.print(" IN 2 ");
  219.   delay(1000);
  220.   lcd.clear();
  221.   lcd.setCursor(0,0);
  222.   lcd.print("Starting Game");
  223.   lcd.setCursor(3,1);
  224.   tone(tonepin,2000,100);
  225.   lcd.print(MINUTOSJUEGO);
  226.   lcd.print(":00:000");
  227.   delay(1000);
  228.   lcd.clear();
  229.   lcd.setCursor(3,0);
  230.   lcd.print("GAME TIME");
  231.   int minutos=MINUTOSJUEGO-1;
  232.   unsigned long iTiempo=millis();
  233.   unsigned long aTiempo;
  234.     while(1){  // this is the important code, is a little messy but works good.
  235.  
  236.     if(((millis()- iTiempo)%1000)>= 0 && (millis()- iTiempo)%1000 <= 20)
  237.     {
  238.       analogWrite(ledVerde, 255);
  239.     }
  240.     if(((millis()- iTiempo)%1000)>= 50 && (millis()- iTiempo)%1000 <= 100)
  241.  
  242.     {    
  243.       analogWrite(ledVerde,0);
  244.     }
  245.  
  246.     lcd.setCursor(3,0);
  247.     lcd.print("GAME TIME");
  248.     aTiempo=millis()- iTiempo;
  249.     lcd.setCursor(3,1);
  250.     if(minutos-aTiempo/60000==0 && 59-((aTiempo/1000)%60)==0)
  251.     {
  252.       lcd.clear();
  253.       while(1){
  254.  
  255.         lcd.setCursor(0,0);
  256.         lcd.print(" GAME OVER! ");
  257.         lcd.setCursor(0,1);
  258.         lcd.print(" DEFENDERS WIN ");  
  259.        
  260.         for(int i = 1000; i>200; i--){
  261.  
  262.           tone(tonepin,i);
  263.           delay(5);
  264.  
  265.         }
  266.  
  267.         noTone(tonepin);
  268.         delay(5000);
  269.         lcd.clear();
  270.         menuPrincipal();
  271.       }
  272.     }
  273.  
  274.     if((minutos-aTiempo/60000)<10){
  275.       lcd.print("0");
  276.       lcd.print(minutos-aTiempo/60000);
  277.     }
  278.     else
  279.     {
  280.       lcd.print(minutos-aTiempo/60000);
  281.     }
  282.     lcd.print(":");
  283.     if((59-((aTiempo/1000)%60))<10){
  284.  
  285.       lcd.print("0");
  286.       lcd.print(59-((aTiempo/1000)%60));
  287.     }
  288.     else {
  289.       lcd.print(59-((aTiempo/1000)%60));
  290.     }
  291.     lcd.print(":");
  292.     lcd.print(millis()%1000);
  293.  
  294.     if(isPressed(BT_RED))
  295.     {
  296.       lcd.clear();
  297.       lcd.setCursor(0,0);
  298.       lcd.print("ARMING");
  299.       lcd.setCursor(0,1);
  300.       unsigned int porcentaje=0;
  301.       unsigned long xTiempo=millis();
  302.       while(isPressed(BT_RED))
  303.       {
  304.         if(((millis()- xTiempo)%1000)>= 0 && (millis()- xTiempo)%1000 <= 20)
  305.         {
  306.           analogWrite(ledRojo, 255);
  307.           tone(tonepin,tonoAlarma1,200);
  308.         }
  309.         if(((millis()- xTiempo)%1000)>= 480 && (millis()- xTiempo)%1000 <= 500)
  310.         {
  311.           tone(tonepin,tonoAlarma2,200);
  312.           analogWrite(ledRojo,0);
  313.         }
  314.        
  315.         unsigned long segundos=(millis()- xTiempo);
  316.         porcentaje=(segundos*100)/(SEGUNDOSACTIVAR*1000);
  317.         drawPorcent(porcentaje);
  318.         if(porcentaje >= 100)
  319.         {
  320.           lcd.clear();
  321.           /*lcd.setCursor(0,0);
  322.           lcd.print("ACTIVATED");
  323.           delay(2000); */
  324.           juegoBomba();
  325.         }
  326.       }
  327.       lcd.clear();
  328.       analogWrite(ledRojo, 0);
  329.     }
  330.   }
  331. }
  332. void juegoBomba(){
  333.   lcd.clear();
  334.   lcd.setCursor(0,0);
  335.   lcd.print(" ARMED ");
  336.   delay(1000);
  337.   int minutos=MINUTOSBOMBA-1;
  338.   unsigned long iTiempo=millis();
  339.   unsigned long aTiempo;
  340.   int largoTono = 50;
  341.   while(1){
  342.  
  343.     if(((millis()- iTiempo)%1000)>= 0 && (millis()- iTiempo)%1000 <= 20)
  344.     {
  345.       analogWrite(ledRojo, 255);
  346.       tone(tonepin,tonoActivada,largoTono);
  347.     }
  348.     if(((millis()- iTiempo)%1000)>= 190 && (millis()- iTiempo)%1000 <= 200){analogWrite(ledRojo,0);}
  349.     if(((millis()- iTiempo)%1000)>= 245 && (millis()- iTiempo)%1000 <= 255 && minutos-aTiempo/60000<2)tone(tonepin,tonoActivada,largoTono);
  350.     if(((millis()- iTiempo)%1000)>= 495 && (millis()- iTiempo)%1000 <= 510 && minutos-aTiempo/60000<4)tone(tonepin,tonoActivada,largoTono);
  351.     if(((millis()- iTiempo)%1000)>= 745 && (millis()- iTiempo)%1000 <= 760 && minutos-aTiempo/60000<2)tone(tonepin,tonoActivada,largoTono);
  352.    
  353.     if( minutos-aTiempo/60000==0 && 59-((aTiempo/1000)%60) < 10)largoTono = 200;
  354.     lcd.setCursor(0,0);
  355.     lcd.print(" DETONATION IN ");
  356.     aTiempo=millis()- iTiempo;
  357.     lcd.setCursor(3,1);
  358.     if(minutos-aTiempo/60000==0 && 59-((aTiempo/1000)%60)==0)
  359.     {
  360.       lcd.clear();
  361.  
  362.       while(1){
  363.  
  364.         lcd.setCursor(0,0);
  365.         lcd.print("TERRORISTS WIN");
  366.         lcd.setCursor(0,1);
  367.         lcd.print("GAME OVER");
  368.  
  369.         for(int i = 200; i>0; i--)
  370.         {
  371.           tone(tonepin,i);
  372.           delay(20);
  373.         }
  374.         noTone(tonepin);
  375.         delay(5000);
  376.         lcd.clear();
  377.         menuPrincipal();
  378.       }
  379.     }
  380.  
  381.     if((minutos-aTiempo/60000)<10)
  382.     {
  383.       lcd.print("0");
  384.       lcd.print(minutos-aTiempo/60000);
  385.     }
  386.     else
  387.     {
  388.       lcd.print(minutos-aTiempo/60000);
  389.     }
  390.     lcd.print(":");
  391.     if((59-((aTiempo/1000)%60))<10)
  392.     {
  393.       lcd.print("0");
  394.       lcd.print(59-((aTiempo/1000)%60));
  395.     }
  396.     else
  397.     {
  398.       lcd.print(59-((aTiempo/1000)%60));
  399.     }
  400.     lcd.print(":");
  401.     lcd.print(millis()%1000);
  402.  
  403.     if(isPressed(BT_RED))
  404.     {
  405.       lcd.clear();
  406.       analogWrite(ledRojo, 0);
  407.       lcd.setCursor(0,0);
  408.       lcd.print("DISARMING");
  409.       lcd.setCursor(0,1);
  410.       unsigned int porcentaje=0;
  411.       unsigned long xTiempo=millis();
  412.       while(isPressed(BT_RED))
  413.       {
  414.         if(((millis()- xTiempo)%1000)>= 0 && (millis()- xTiempo)%1000 <= 20)
  415.         {
  416.           analogWrite(ledVerde, 255);
  417.           tone(tonepin,tonoAlarma1,200);
  418.         }
  419.         if(((millis()- xTiempo)%1000)>= 480 && (millis()- xTiempo)%1000 <= 500)
  420.         {
  421.           tone(tonepin,tonoAlarma2,200);
  422.           analogWrite(ledVerde,0);
  423.         }
  424.         unsigned long segundos=(millis()- xTiempo);
  425.         porcentaje=(segundos*100)/(SEGUNDOSACTIVAR*1000);
  426.         drawPorcent(porcentaje);  
  427.         if(porcentaje >= 100)
  428.         {
  429.           lcd.clear();
  430.           while(1){
  431.             lcd.setCursor(0,0);
  432.             lcd.print("BOMB DISARMED");
  433.             /*lcd.setCursor(0,1);
  434.             lcd.print("AT GANA PARTIDA");*/
  435.             analogWrite(ledVerde, 255);
  436.             delay(2000);
  437.             //menuPrincipal();
  438.             return;
  439.           }
  440.         }
  441.       }
  442.  
  443.       lcd.clear();
  444.       analogWrite(ledRojo, 0);
  445.     }
  446.   }
  447. }
  448.  
  449. void drawPorcent(int porcent){
  450.   int i=1;
  451.   int aDibujar=(80*porcent)/100;
  452.   lcd.setCursor(0,1);
  453.  
  454.   while(aDibujar>=5){
  455.     if(aDibujar>=5)
  456.     {
  457.       lcd.write(4);
  458.       aDibujar-=5;
  459.     }
  460.     if(aDibujar<5)
  461.     {
  462.  
  463.       switch(aDibujar){
  464.       case 0:
  465.         break;
  466.       case 1:
  467.         lcd.write(0);
  468.         break;
  469.       case 2:
  470.         lcd.write(1);
  471.         break;
  472.       case 3:
  473.         lcd.write(2);
  474.         break;
  475.       case 4:
  476.         lcd.write(3);
  477.         break;
  478.       }
  479.     }
  480.   }
  481. }
  482.  
  483. void configJuegoRapido(){
  484.  
  485.   lcd.clear();
  486.   lcd.setCursor(0,0);
  487.   lcd.print("Game Time :");
  488.   lcd.setCursor(0,1);
  489.   delay(800);
  490.   while(!isPressed(BT_RED)){
  491.     lcd.setCursor(0,1);  
  492.     lcd.print(MINUTOSJUEGO);  
  493.     lcd.print(" minutes");
  494.  
  495.     if(isPressed(BT_LEFT) && MINUTOSJUEGO>1){
  496.       tone(tonepin,2400,30);
  497.       MINUTOSJUEGO--;
  498.       delay(300);
  499.     }
  500.     if(isPressed(BT_RIGHT) && MINUTOSJUEGO<180){
  501.       tone(tonepin,2400,30);
  502.       MINUTOSJUEGO++;
  503.       delay(300);
  504.     }
  505.   }
  506.   tone(tonepin,2400,30);
  507.   lcd.clear();
  508.   lcd.setCursor(0,0);
  509.   lcd.print("Bomb Timer :");
  510.   lcd.setCursor(0,1);
  511.   delay(800);
  512.  
  513.   while(!isPressed(BT_RED)){
  514.     lcd.setCursor(0,1);  
  515.     lcd.print(MINUTOSBOMBA);  
  516.     lcd.print(" minutes");
  517.  
  518.     if(isPressed(BT_LEFT) && MINUTOSBOMBA>1){
  519.       tone(tonepin,2400,30);
  520.       MINUTOSBOMBA--;
  521.       delay(300);
  522.     }
  523.     if(isPressed(BT_RIGHT) && MINUTOSBOMBA<20){
  524.       tone(tonepin,2400,30);
  525.       MINUTOSBOMBA++;
  526.       delay(300);
  527.     }
  528.   }
  529.   tone(tonepin,2400,30);
  530.   lcd.clear();
  531.   lcd.setCursor(0,0);
  532.   lcd.print("Activation time:");
  533.   lcd.setCursor(0,1);
  534.   delay(800);
  535.  
  536.   while(!isPressed(BT_RED)){
  537.     lcd.setCursor(0,1);  
  538.     lcd.print(SEGUNDOSACTIVAR);  
  539.     lcd.print(" seconds");
  540.  
  541.     if(isPressed(BT_LEFT) && SEGUNDOSACTIVAR>5){
  542.       tone(tonepin,2400,30);
  543.       SEGUNDOSACTIVAR--;
  544.       delay(300);
  545.     }
  546.     if(isPressed(BT_RIGHT) && SEGUNDOSACTIVAR<30){
  547.       SEGUNDOSACTIVAR++;
  548.       tone(tonepin,2400,30);
  549.       delay(300);
  550.     }
  551.   }
  552.   tone(tonepin,2400,30);
  553.   lcd.clear();
  554.   game();  
  555. }
  556.  
  557. //Get Key
  558. boolean isPressed( int button) {
  559.  
  560.   if( get_key() == button ) return true;
  561.   return false;
  562. }
  563.  
  564. // Convert ADC value to key number
  565. int get_key()
  566. {
  567.        
  568.         int input = analogRead(0);
  569.     int k;
  570.     for (k = 0; k < NUM_KEYS; k++)
  571.     {
  572.         if (input < adc_key_val[k])
  573.         {
  574.                   return k;
  575.                 }
  576.     }
  577.    
  578.     if (k >= NUM_KEYS)
  579.         k = -1;     // No valid key pressed
  580.    
  581.     return k;
  582. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement