Advertisement
ASZK

Boom_barrel_Master

Mar 19th, 2020
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <Arduino.h>
  2. #include <Wire.h>
  3.  
  4.  
  5. #define IRpin_PIN      PIND
  6. #define IRpin          2
  7.  
  8.  
  9. #define MAXPULSE 70
  10. #define NUMPULSES 30
  11.  
  12. #define RESOLUTION 50
  13. String Packet = "";
  14. String pack = "";
  15. String empty = "";
  16.  
  17. unsigned long lastDebounceTime = 0;  // the last time the output pin was toggled
  18. unsigned long debounceDelay = 50;    // the debounce time; increase if the output flickers
  19. /////////////////////////////////////////////////
  20. const String New_GameIM = "100000110000010111101000";
  21. const String Admin_Kill = "100000110000000011101000";
  22. const String Clear_Scores = "100000110001010011101000";
  23.  
  24. ///////////////////////////////////////////////////
  25.  
  26. uint16_t pulses[NUMPULSES][2];  // pair is high and low pulse
  27. uint16_t MilesMessageBIN[24];  // binary message
  28. uint16_t MilesShotBIN[14];  // binary message
  29.  
  30. uint8_t currentpulse = 0; // index for pulses we're storing
  31.  
  32.  
  33.  
  34. class Elapsed
  35. {
  36.     unsigned long startus, startms;
  37.  
  38.   public:
  39.  
  40.     // constructor resets time
  41.     Elapsed () {
  42.       reset ();
  43.     };
  44.  
  45.     // reset time to now
  46.     void reset () {
  47.       startus = micros ();
  48.       startms = millis ();
  49.     };
  50.  
  51.     // return Elapsed time in milliseconds
  52.     unsigned long intervalMs () {
  53.       return millis () - startms;
  54.     };
  55.  
  56.     // return Elapsed time in microseconds
  57.     unsigned long intervalUs () {
  58.       return micros () - startus;
  59.     };
  60.  
  61. };    // end of class Elapsed
  62.  
  63.  
  64. static Elapsed t_standby;
  65.  
  66. int bazook = 0;
  67. int zero = 0;
  68. int message = 0;
  69.  
  70.  
  71. void setup(void) {
  72.  
  73.   Wire.begin();
  74.   Serial.begin(9600);
  75.   Serial.println("Ready to decode IR!");
  76. }
  77.  
  78. String RawToBin() {
  79.   for (uint32_t i = 0; i < currentpulse; i++) {
  80.  
  81.     // check for Header
  82.     if ((2200 <= (pulses[i][1] * RESOLUTION)) && (2600 >= (pulses[i][1] * RESOLUTION))) {
  83.       int Header = i;
  84.       i++;
  85.       for (uint32_t m = Header; m <= currentpulse - 1; m++) {
  86.         if (400 < (pulses[m][1] * RESOLUTION) && (pulses[m][1] * RESOLUTION) < 800) {
  87.           pack.concat(0);
  88.         }
  89.         if ((1000 < (pulses[m][1] * RESOLUTION) && (pulses[m][1] * RESOLUTION) < 1400)) {
  90.           pack.concat(1);
  91.         }
  92.       }
  93.       return pack;
  94.     }
  95.   }
  96.   return "error";
  97. }
  98.  
  99.  
  100. void printpulses(void) {
  101.   Serial.println("\n\r\n\rReceived: \n\rRAW");
  102.   for (uint8_t i = 0; i < currentpulse; i++) {
  103.     Serial.print(pulses[i][1] * RESOLUTION, DEC);
  104.     Serial.print("\t");
  105.     Serial.println(" ");
  106.   }
  107. }
  108.  
  109. int listenForIR(void) {
  110.   currentpulse = 0;
  111.   while (1) {
  112.     uint16_t highpulse, lowpulse;  // temporary storage timing
  113.     highpulse = lowpulse = 0; // start out with no pulse length
  114.  
  115.     while (IRpin_PIN & (1 << IRpin)) {
  116.       highpulse++;
  117.       delayMicroseconds(RESOLUTION);
  118.       if (((highpulse >= MAXPULSE) && (currentpulse != 0)) || currentpulse == NUMPULSES) {
  119.         // printpulses();   //RAW data
  120.         return currentpulse;
  121.       }
  122.     }
  123.     pulses[currentpulse][0] = highpulse;
  124.     while (! (IRpin_PIN & _BV(IRpin))) {
  125.       lowpulse++;
  126.       delayMicroseconds(RESOLUTION);
  127.       if (((lowpulse >= MAXPULSE)  && (currentpulse != 0)) || currentpulse == NUMPULSES) {
  128.         //  printpulses(); //RAW data
  129.         return currentpulse;
  130.       }
  131.     }
  132.     pulses[currentpulse][1] = lowpulse;
  133.     currentpulse++;
  134.   }
  135. }
  136.  
  137.  
  138.  
  139. void decode_shoot() {
  140.  
  141.   if (Packet.charAt(0) == '0')//shot packet
  142.   {
  143.     message = 1;
  144.     //Serial.println("Shot Packet");
  145.  
  146.  
  147.  
  148.     //////////////
  149.   }
  150. }
  151. void decode_message() {
  152.   if (Packet.charAt(0) == '1') //Message packet
  153.   {
  154.     Serial.println("Message Packet");
  155.     if (Packet.equals(Admin_Kill)) {
  156.       Serial.println("Received Admin Kill");
  157.     }
  158.     if (Packet.equals(New_GameIM)) {
  159.       message = 9;
  160.       Serial.println("Received New Game Immediate");
  161.     }
  162.     if (Packet.equals(Clear_Scores)) {
  163.       Serial.println("Received Clear Scores");
  164.     }
  165.   }
  166.   //else{Serial.println("Not in database");}
  167.  
  168. }
  169.  
  170.  
  171. void sendmessage() {
  172.   Wire.beginTransmission(9);
  173.   Wire.write(message);
  174.   Wire.endTransmission();
  175.  
  176.   //Serial.println(Packet);
  177. }
  178.  
  179.  
  180. void order() {
  181.   String commandline = Serial.readString();
  182.   //Serial.println(commandline);
  183.  
  184.   if (commandline.startsWith("safety off")) {
  185.   }
  186. }
  187.  
  188.  
  189. void loop(void) {
  190.  
  191.  
  192.   if (Serial.available() > 0) {
  193.     order();
  194.   }
  195.  
  196.   int numberpulses;
  197.   numberpulses = listenForIR(); //czeka na sygnal
  198.   Serial.print("\nHeard ");
  199.   Serial.print(numberpulses);
  200.   Serial.println("-pulse long IR signal");
  201.  
  202.  
  203.  
  204.   // Shot
  205.   if (numberpulses == 15) {
  206.     Packet = RawToBin();
  207.     Serial.println(Packet);
  208.     decode_shoot();
  209.  
  210.     //
  211.   }
  212.  
  213.   if (numberpulses == 25) {
  214.     Packet = RawToBin();
  215.     Serial.println(Packet);
  216.     decode_message();
  217.  
  218.   }
  219.  
  220.  
  221.  
  222.   sendmessage();
  223.  
  224.   if (message != 0) {
  225.     message = 0;
  226.   }
  227.  
  228.  
  229.  
  230.  
  231.   Packet = empty;
  232.   pack = empty;
  233.  
  234.   //  for( int i=0; i<=9;i++){
  235.   //
  236.   //  Serial.write(message,15);
  237.   //
  238.   //  if(i==9){
  239.   //    i=0;
  240.   //  }
  241.   //  delay(500);
  242.   //  }
  243. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement