Guest User

Arduino Bomb 0.1 AnalogRead()

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