Advertisement
ASZK

Untitled

Jul 25th, 2020
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 16.57 KB | None | 0 0
  1. #include <Arduino.h>
  2. #include <DFPlayer_Mini_Mp3.h>
  3.  
  4. //////////////////////////////////////////////  SET PINS   //////////////////////////
  5. SoftwareSerial mySerial(10, 11); // RX, TX
  6. #define txPinIR 3 //IR carrier output
  7.  
  8. #define IRpin_PIN      PINB
  9. #define IRpin          1   // PIN D9 - atmega PB1
  10.  
  11.  
  12. //#define TSOP 8
  13. #define led_red A0
  14. #define led_green A1
  15. #define led_blue A2
  16. //////////////////////////////////////////////  SET PINS   //////////////////////////
  17.  
  18. unsigned long duration = 0;
  19. unsigned long durationold = 0;
  20. long timeout = 10000; //
  21. long Red_score = 0;
  22. long Blue_score = 0;
  23. long Yellow_score = 0;
  24. long Green_score = 0;
  25. long score = 0;
  26. int Team = 5;
  27. //unsigned long start_time;
  28. String Received = "";
  29. String Receivedold = "";
  30. unsigned char carrierFreq = 56; //default
  31. unsigned char period = 0;      //calculated once for each signal sent in initSoftPWM
  32. unsigned char periodHigh = 0;  //calculated once for each signal sent in initSoftPWM
  33. unsigned char periodLow = 0;   //calculated once for each signal sent in initSoftPWM
  34. unsigned long sigTime = 0;     //used in mark & space functions to keep track of time
  35. unsigned long sigStart = 0;    //used to calculate correct length of existing signal, to handle some repeats
  36.  
  37. #define NEC_HEX_VALUE 0x20DF22DDUL //UL makes this an unsigned long
  38. #define NEC_BIT_COUNT 24           // header +24 for message
  39. /////////////////////////////////////////////////////////////////////
  40. // MilesTag command pack  (0x...  ...UL)
  41. #define New_GameIM 0x8305E8UL // New Game (Immediate)
  42. #define Explode 0x830BE8UL    //Explode
  43. //////////////////////////////////////////////////////////////////
  44.  
  45. /////////////////////////////////////////////////
  46. const String New_GameIM_rcv = "100000110000010111101000";
  47. const String Admin_Kill_rcv = "100000110000000011101000";
  48. const String Clear_Scores_rcv = "100000110001010011101000";
  49. ///////////////////////////////////////////////////
  50.  
  51.  
  52. uint8_t currentpulse = 0; // index for pulses we're storing
  53. #define MAXPULSE 30
  54. #define NUMPULSES 30
  55.  
  56. #define RESOLUTION 50
  57. uint16_t pulses[NUMPULSES][2];  // pair is high and low pulse
  58.  
  59.  
  60. class Elapsed
  61. {
  62.   unsigned long startus, startms;
  63.  
  64. public:
  65.   // constructor resets time
  66.   Elapsed()
  67.   {
  68.     reset();
  69.   };
  70.  
  71.   // reset time to now
  72.   void reset()
  73.   {
  74.     startus = micros();
  75.     startms = millis();
  76.   };
  77.  
  78.   // return Elapsed time in milliseconds
  79.   unsigned long intervalMs()
  80.   {
  81.     return millis() - startms;
  82.   };
  83.  
  84.   // return Elapsed time in microseconds
  85.   unsigned long intervalUs()
  86.   {
  87.     return micros() - startus;
  88.   };
  89.  
  90. }; // end of class Elapsed
  91.  
  92. static Elapsed t_1, t_2, t_3, t_bazook, t_flash, t_blink, t_itter;
  93. int blink = 0;
  94. int bit = 0;
  95. int loopNr = 0;
  96.  
  97. void setup()
  98. {
  99.   Serial.begin(2000000); // 2MHz- fastest one
  100.   Serial.println("---Ready----");
  101.   Serial.println("\n");
  102.   //pinMode(TSOP, INPUT);
  103.   pinMode(txPinIR, OUTPUT);
  104.   pinMode(led_red, OUTPUT);
  105.   pinMode(led_green, OUTPUT);
  106.   pinMode(led_blue, OUTPUT);
  107.   digitalWrite(led_red, 1);
  108.   digitalWrite(led_blue, 1);
  109.   digitalWrite(led_green, 1);
  110.   //white();
  111.  
  112.   delay(300);
  113.   mySerial.begin(9600);
  114.   mp3_set_serial(mySerial); //set softwareSerial for DFPlayer-mini mp3 module
  115.   mp3_set_volume(30);
  116.   mp3_play(10);
  117.   delay(300);
  118.  
  119.   //led_off();
  120.  
  121. }
  122.  
  123. void initSoftPWM(unsigned char carrierFreq)
  124. { // Assumes standard 8-bit Arduino, running at 16Mhz
  125.   //supported values are 30, 33, 36, 38, 40, 56 kHz, any other value defaults to 38kHz
  126.   //we will aim for a  duty cycle of circa 33%
  127.  
  128.   period = (1000 + carrierFreq / 2) / carrierFreq;
  129.   periodHigh = (period + 1) / 3; // 3
  130.   periodLow = period - periodHigh;
  131.  
  132.   switch (carrierFreq)
  133.   {
  134.   case 30:           //delivers a carrier frequency of 29.8kHz & duty cycle of 34.52%
  135.     periodHigh -= 6; //Trim it based on measurementt from Oscilloscope
  136.     periodLow -= 10; //Trim it based on measurementt from Oscilloscope
  137.     break;
  138.  
  139.   case 33:           //delivers a carrier frequency of 32.7kHz & duty cycle of 34.64%
  140.     periodHigh -= 6; //Trim it based on measurementt from Oscilloscope
  141.     periodLow -= 10; //Trim it based on measurementt from Oscilloscope
  142.     break;
  143.  
  144.   case 36:           //delivers a carrier frequency of 36.2kHz & duty cycle of 35.14%
  145.     periodHigh -= 6; //Trim it based on measurementt from Oscilloscope
  146.     periodLow -= 11; //Trim it based on measurementt from Oscilloscope
  147.     break;
  148.  
  149.   case 40:           //delivers a carrier frequency of 40.6kHz & duty cycle of 34.96%
  150.     periodHigh -= 6; //Trim it based on measurementt from Oscilloscope
  151.     periodLow -= 11; //Trim it based on measurementt from Oscilloscope
  152.     break;
  153.  
  154.   case 56:           //delivers a carrier frequency of 53.8kHz & duty cycle of 40.86%
  155.     periodHigh -= 6; //Trim it based on measurementt from Oscilloscope 6
  156.     periodLow -= 12; //Trim it based on measurementt from Oscilloscope 12
  157.     //Serial.println(periodHigh);
  158.     //Serial.println(periodLow);
  159.  
  160.     break;
  161.  
  162.   case 38: //delivers a carrier frequency of 37.6kHz & duty cycle of 36.47%
  163.   default:
  164.     periodHigh -= 6; //Trim it based on measurementt from Oscilloscope
  165.     periodLow -= 11; //Trim it based on measurementt from Oscilloscope
  166.     break;
  167.   }
  168. }
  169.  
  170. void mark(unsigned int mLen)
  171. {                  //uses sigTime as end parameter
  172.   sigTime += mLen; //mark ends at new sigTime
  173.   unsigned long now = micros();
  174.   unsigned long dur = sigTime - now; //allows for rolling time adjustment due to code execution delays
  175.   if (dur == 0)
  176.     return;
  177.   while ((micros() - now) < dur)
  178.   { //just wait here until time is up
  179.     digitalWrite(txPinIR, HIGH);
  180.     if (periodHigh)
  181.       delayMicroseconds(periodHigh);
  182.     digitalWrite(txPinIR, LOW);
  183.     if (periodLow)
  184.       delayMicroseconds(periodLow);
  185.   }
  186. }
  187.  
  188. void space(unsigned int sLen)
  189. {                  //uses sigTime as end parameter
  190.   sigTime += sLen; //space ends at new sigTime
  191.   unsigned long now = micros();
  192.   unsigned long dur = sigTime - now; //allows for rolling time adjustment due to code execution delays
  193.   if (dur == 0)
  194.     return;
  195.   while ((micros() - now) < dur)
  196.     ; //just wait here until time is up
  197. }
  198.  
  199. void sendHexNEC(unsigned long sigCode, byte numBits, unsigned char repeats, unsigned char kHz)
  200. {
  201.  
  202. #define NEC_HEADER_MARK 2400
  203. #define NEC_HEADER_SPACE 600
  204. #define NEC_ONE_MARK 1200
  205. #define NEC_ZERO_MARK 600
  206. #define NEC_ONE_SPACE 600
  207. #define NEC_ZERO_SPACE 600
  208. #define NEC_TRAILER_MARK 560
  209.  
  210.   unsigned long bitMask = (unsigned long)1 << (numBits - 1); //allows for signal from 1 bit up to 32 bits
  211.   //
  212.   if (carrierFreq != kHz)
  213.     initSoftPWM(kHz); //we only need to re-initialise if it has changed from last signal sent
  214.  
  215.   sigTime = micros(); //keeps rolling track of signal time to avoid impact of loop & code execution delays
  216.   sigStart = sigTime; //remember for calculating first repeat gap (space), must end 108ms after signal starts
  217.  
  218.   // First send header Mark & Space
  219.   mark(NEC_HEADER_MARK);
  220.   space(NEC_HEADER_SPACE);
  221.  
  222.   while (bitMask)
  223.   {
  224.     if (bitMask & sigCode)
  225.     { //its a One bit
  226.       mark(NEC_ONE_MARK);
  227.       space(NEC_ONE_SPACE);
  228.     }
  229.     else
  230.     { // its a Zero bit
  231.       mark(NEC_ZERO_MARK);
  232.       space(NEC_ZERO_SPACE);
  233.     }
  234.     bitMask = (unsigned long)bitMask >> 1; // shift the mask bit along until it reaches zero & we exit the while loop
  235.   }
  236.   // Last send NEC Trailer MArk
  237.   // mark(NEC_TRAILER_MARK);
  238.  
  239.   if (repeats == 0)
  240.     return; //finished - no repeats
  241.   else if (repeats > 0)
  242.   {                                       //first repeat must start 108ms after first signal
  243.     space(108000 - (sigTime - sigStart)); //first repeat Header should start 108ms after first signal
  244.     mark(NEC_HEADER_MARK);
  245.     space(NEC_HEADER_SPACE / 2); //half the length for repeats
  246.     mark(NEC_TRAILER_MARK);
  247.   }
  248.  
  249.   while (--repeats > 0)
  250.   {                                                                            //now send any remaining repeats
  251.     space(108000 - NEC_HEADER_MARK - NEC_HEADER_SPACE / 2 - NEC_TRAILER_MARK); //subsequent repeat Header must start 108ms after previous repeat signal
  252.     mark(NEC_HEADER_MARK);
  253.     space(NEC_HEADER_SPACE / 2); //half the length for repeats
  254.     mark(NEC_TRAILER_MARK);
  255.   }
  256. }
  257.  
  258.  
  259.  
  260. void red()
  261. {
  262.   digitalWrite(led_red, 0);
  263.   digitalWrite(led_blue, 1);
  264.   digitalWrite(led_green, 1);
  265. }
  266. void blue()
  267. {
  268.   digitalWrite(led_red, 1);
  269.   digitalWrite(led_blue, 0);
  270.   digitalWrite(led_green, 1);
  271. }
  272. void green()
  273. {
  274.   digitalWrite(led_red, 1);
  275.   digitalWrite(led_blue, 1);
  276.   digitalWrite(led_green, 0);
  277. }
  278. void white()
  279. {
  280.   digitalWrite(led_red, 0);
  281.   digitalWrite(led_blue, 0);
  282.   digitalWrite(led_green, 0);
  283. }
  284. void yellow()
  285. {
  286.   digitalWrite(led_red, 0);
  287.   digitalWrite(led_blue, 1);
  288.   digitalWrite(led_green, 0);
  289. }
  290. void led_off()
  291. {
  292.   digitalWrite(led_red, 1);
  293.   digitalWrite(led_blue, 1);
  294.   digitalWrite(led_green, 1);
  295. }
  296.  
  297. void reloading(){
  298. for (int i = 0; i < 25; i++)
  299. {
  300.    white();
  301.   delay(400);
  302.   led_off();
  303.   delay(600);
  304. }
  305. }
  306.  
  307. void score_clear()
  308. {
  309.   //Red_score = Blue_score = Yellow_score = Green_score = score = 0;
  310.   Red_score = 0;
  311.   Blue_score = 0;
  312.   Yellow_score = 0;
  313.   Green_score = 0;
  314.   score = 0;
  315.   //led_off();
  316.   Serial.println("cleared scores");
  317. }
  318.  
  319.  
  320. void overscore()
  321. {
  322.   if (Red_score > timeout)
  323.     {
  324.       Serial.println("Red wins");
  325.       Red_score = Blue_score = Yellow_score = Green_score = score = 0;
  326.    
  327.      }
  328.   if (Blue_score > timeout)
  329.     {
  330.       Serial.println("Blue wins");
  331.       Red_score = Blue_score = Yellow_score = Green_score = score = 0;
  332.     }
  333.   if (Yellow_score > timeout)
  334.     {
  335.       Serial.println("Yellow wins");
  336.       Red_score = Blue_score = Yellow_score = Green_score = score = 0;
  337.     }
  338.   if (Green_score > timeout)
  339.     {
  340.       Serial.println("Green wins");
  341.       Red_score = Blue_score = Yellow_score = Green_score = score = 0;
  342.     }
  343.   Serial.println("overscore");
  344. }
  345.  
  346.  
  347.  
  348.  
  349.  
  350. void shootrocket()
  351. {
  352.  
  353.   led_off();
  354.   delay(100);
  355.     for (int i = 0; i < 3; i++)
  356.     {
  357.       sendHexNEC(Explode, NEC_BIT_COUNT, 1, 56);
  358.       delay(100);
  359.     }
  360.   delay(100);
  361.  
  362.   mp3_play(8);
  363.   red();
  364.   delay(3000);
  365.   led_off();
  366.   //reloading();
  367.         for (int i = 0; i < 25; i++)
  368.     {
  369.       led_off();
  370.       delay(600);
  371.       blue();
  372.       delay(400);
  373.      
  374.     }
  375.   green();
  376. }
  377.  
  378. void testowanie()
  379. {
  380.   Serial.println("test");
  381.   for (int i = 0; i < 5000; i++)
  382.   {
  383.     blue();
  384.     delay(200);
  385.     led_off();
  386.     delay(200);
  387.     sendHexNEC(New_GameIM, NEC_BIT_COUNT, 1, 56);
  388.     delay(100);
  389.     red();
  390.     delay(200);
  391.     led_off();
  392.     delay(200);
  393.     sendHexNEC(New_GameIM, NEC_BIT_COUNT, 1, 56);
  394.     delay(100);
  395.     Serial.println("test");
  396.   }
  397. }
  398.  
  399. void update_team(){
  400.  
  401.  // duration = millis();
  402.  
  403.   //      if (durationold != 0){
  404.    //       score = (duration - durationold);
  405.     //      }
  406.  
  407.         //Serial.println(Received); // odebrany binarny
  408.        
  409.         int Team = (Received.substring(9,11).toInt());
  410.  
  411.  
  412.         //Serial.println(Team);
  413.  
  414.             switch (Team)
  415.                 {
  416.                 case 0:
  417.                   /* RED */
  418.        //           Red_score += score;
  419.                   Serial.println("RED");
  420.                   red();
  421.                   break;
  422.                 case 1:
  423.                   /* BLUE */
  424.         //          Blue_score += score;
  425.                   //Serial.println("BLUE");
  426.                   blue();
  427.                   break;
  428.                 case 10:
  429.                   /* YELLOW */
  430.         //          Yellow_score += score;
  431.                  // Serial.println("YELLOW");
  432.                   yellow();
  433.                   break;
  434.                 case 11:
  435.                   /* GREEN */
  436.          //         Green_score += score;
  437.                   //Serial.println("GREEN");
  438.                   green();
  439.                   break;
  440.                   case 5:
  441.                   //itter start
  442.                   break;
  443.                  
  444.                
  445.                 default:
  446.                 //  Serial.println("default");
  447.                   break;
  448.                 }
  449.  
  450.         durationold = duration;
  451.  
  452.  
  453.   }
  454.  
  455. void update_time(){
  456.  duration = millis();
  457.  
  458.         if (durationold != 0){
  459.           score = (duration - durationold);
  460.           }
  461.  
  462.         //Serial.println(Received); // odebrany binarny ;
  463.  
  464.         //Serial.println(Team);
  465.  
  466.             switch (Team)
  467.                 {
  468.                 case 0:
  469.                   /* RED */
  470.                   Red_score += score;
  471.                   Serial.println("RED+1");
  472.                   red();
  473.                   break;
  474.                 case 1:
  475.                   /* BLUE */
  476.                   Blue_score += score;
  477.                   //Serial.println("BLUE");
  478.                   blue();
  479.                   break;
  480.                 case 10:
  481.                   /* YELLOW */
  482.                   Yellow_score += score;
  483.                  // Serial.println("YELLOW");
  484.                   yellow();
  485.                   break;
  486.                 case 11:
  487.                   /* GREEN */
  488.                   Green_score += score;
  489.                   //Serial.println("GREEN");
  490.                   green();
  491.                   break;
  492.                  
  493.                
  494.                 default:
  495.                  // Serial.println(Team);
  496.                   break;
  497.                 }
  498.  
  499.         durationold = duration;
  500.  
  501.  
  502. }
  503.  
  504.  
  505. void action(){
  506.  
  507.       if ((Received.length() == 15) && (Received.startsWith("20"))) //Shoot
  508.       {
  509.  
  510.         update_team();
  511.  
  512.  
  513.        
  514.          
  515.       }
  516.  
  517.     if ((Received.length() == 25) && (Received.startsWith("21"))) //Message
  518.       {
  519.         Serial.println("Message");
  520.         testowanie();
  521.         Serial.println(Received);
  522.         mp3_play(10);
  523.       }
  524. }
  525.  
  526.  
  527.  
  528.  
  529.  
  530.  
  531. void printpulses(void) {
  532.   //Serial.println("\n\r\n\rReceived:");
  533.       for (uint8_t i = 0; i < currentpulse; i++) {
  534.         bit = (log(((((pulses[i][1]) * (RESOLUTION)) + 50) / 600)) / (log(2))) ;
  535.         Received.concat(bit);  
  536.       }
  537.  
  538. }
  539.  
  540. int listenForIR(void) {
  541.   currentpulse = 0;
  542.   while (1) {
  543.     uint16_t highpulse, lowpulse;  // temporary storage timing
  544.     highpulse = lowpulse = 0; // start out with no pulse length
  545.  
  546.     while (IRpin_PIN & (1 << IRpin)) {
  547.       highpulse++;
  548.       //Serial.println(highpulse);
  549.  
  550.       if (highpulse >= 200)
  551.       {
  552.  
  553.        
  554.  
  555.         return currentpulse;
  556.       }
  557.      
  558.       delayMicroseconds(RESOLUTION);
  559.        
  560.  
  561.       if (((highpulse >= MAXPULSE) && (currentpulse != 0)) || currentpulse == NUMPULSES) {
  562.         //printpulses();   //RAW data
  563.             for (uint8_t i = 0; i < currentpulse; i++) {
  564.             bit = (log(((((pulses[i][1]) * (RESOLUTION)) + 50) / 600)) / (log(2))) ;
  565.             Received.concat(bit);  
  566.           }
  567.  
  568.       Serial.println(Received);    
  569.  
  570.  
  571.  
  572.         return currentpulse;
  573.       }
  574.     }
  575.     pulses[currentpulse][0] = highpulse;
  576.  
  577.  
  578.     while (! (IRpin_PIN & _BV(IRpin))) {
  579.       lowpulse++;
  580.       delayMicroseconds(RESOLUTION);
  581.      
  582.       if (((lowpulse >= MAXPULSE)  && (currentpulse != 0)) || currentpulse == NUMPULSES) {
  583.         //printpulses(); //RAW data
  584.            
  585.             for (uint8_t i = 0; i < currentpulse; i++)
  586.               {
  587.               bit = (log(((((pulses[i][1]) * (RESOLUTION)) + 50) / 600)) / (log(2))) ;
  588.               Received.concat(bit);  
  589.               }
  590.             return currentpulse;
  591.       }
  592.     }
  593.     pulses[currentpulse][1] = lowpulse;
  594.     currentpulse++;
  595.   }
  596. }
  597.  
  598.  
  599.  
  600.  
  601.  
  602. void test_speaker()
  603. {
  604.   for (int i = 0; i < 9; i++)
  605.   {
  606.    
  607.   mp3_play(i);
  608.   delay(1000);
  609.   }
  610.  
  611. }
  612.  
  613. void order()
  614. {
  615.   String commandline = Serial.readString();
  616.   //Serial.println(commandline);
  617.  
  618.   if (commandline.startsWith("new game"))
  619.   {
  620.     Serial.println(F("Sending New Game (Immediate)"));
  621.     sendHexNEC(New_GameIM, NEC_BIT_COUNT, 1, 56);
  622.   }
  623.  
  624.   if (commandline.startsWith("shoot"))
  625.   {
  626.     shootrocket();
  627.   }
  628.   if (commandline.startsWith("test"))
  629.   {
  630.     testowanie();
  631.   }
  632. }
  633.  
  634. void reloadingold()
  635. {
  636.  
  637.   if (blink == 2)
  638.   {
  639.     blink = 0;
  640.   }
  641.  
  642.   if (t_blink.intervalMs() > 800)
  643.   {
  644.     if (blink == 0)
  645.     {
  646.       blue();
  647.     }
  648.     if (blink == 1)
  649.     {
  650.       led_off();
  651.     }
  652.     blink++;
  653.     t_blink.reset();
  654.     t_itter.reset();
  655.   }
  656. }
  657.  
  658. void loop()
  659. {
  660.  
  661.  // testowanie();
  662.  
  663.       Received = "";
  664.       listenForIR();
  665.  
  666.       if(Received != Receivedold){
  667.  
  668.         action();
  669.  
  670.         Receivedold = Received;
  671.       }  
  672.      
  673.      
  674.       //update_team();
  675.       update_time();
  676.  
  677.  
  678.  
  679.  
  680.  
  681.  
  682.  
  683.  
  684.  
  685.  
  686.  
  687.  
  688.  
  689.  
  690.  
  691.  
  692.  
  693.  
  694.  
  695.  
  696.       if (Received != "")
  697.       {
  698.         action();
  699.         /* code */
  700.       }
  701.      
  702.  
  703.   //update();
  704.  
  705.   // action();
  706.   // overscore();
  707.  
  708.  
  709.  
  710.  
  711.  
  712.  
  713.  
  714. }
  715.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement