Advertisement
WielkaSpluwa

Pilot

Apr 10th, 2021 (edited)
312
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 10.14 KB | None | 0 0
  1. //Software for laserwar remote
  2. #include <Keypad.h>
  3. #include <IRremote.h>
  4.  
  5. const byte GREEN_PIN = 2;
  6. const byte RED_PIN = 3;
  7. const byte IR_SEND_PIN = 4;  
  8.  
  9. //IR code signal lenghts, divided by 100
  10. const byte EMPTY=6;
  11. const byte ZER=6;
  12. const byte ONE=12;
  13. const byte HEADER=24;
  14.  
  15. //Ir Frequency in Kilohertz
  16. const int KHZ = 56;
  17. //IR message lenght
  18. const byte SIGNAL_LENGTH=50;
  19.  
  20.  
  21. //Keyboard dimensions
  22. const byte ROWS = 4;
  23. const byte COLS = 4;
  24.  
  25. byte rowPins[ROWS] = {5,6,7,8};
  26. byte colPins[COLS] = {9,10,11,12};
  27.  
  28. // Codes used in remote
  29. //Used very often, generally not harmfull //0
  30. const long NEW_GAME = 0x8305E8; //1
  31. const long ADMIN_KILL = 0x8300E8; //2
  32. const long NEW_GAME_DELAYED = 0x8302E8; //3 check with 0х0C
  33. const long DISARM = 0x8317E8; //4 Usefull on kids
  34. const long DOUBLE_HEALTH = 0x8310E8;//5
  35.  
  36. const long TEAM_RED = 0xA900E8; //A
  37. const long TEAM_BLUE = 0xA901E8; //B
  38. const long TEAM_YELLOW = 0xA902E8; //C
  39. const long TEAM_GREEN = 0xA903E8; //D
  40. //Quick presets
  41.  
  42.  
  43.  
  44. const long CLEAR_STATS = 0x8314E8; //* 1
  45. const long SWAP_IR_POWER = 0x830EE8; //* 2
  46.  
  47.  
  48. //RARE_PRESETS                          
  49. const long PRESET2_SNIPER_POWER_8 = 0xA802E8; //6 Indoor
  50. const long PRESET1_STORMTROOPER_POWER_35 = 0xA801E8; //7 Medium Outdoor
  51. const long PRESET3_MACHINEGUNNER_POWER_100 = 0xA803E8; //8 Long Outdoor
  52. const long PRESET4_MEDIC_AUTORESPAWN = 0xA804E8; //9 Deathmatch
  53. const long PRESET5_RECON_BOOST = 0xA805E8; //#      1
  54. const long PRESET6_TERMINATOR = 0xA806E8; //#      2
  55. const long PRESET7_ZOMBIE = 0xA807E8; //#      3
  56. const long PRESET8_LAST_MAN_STANDING = 0xA808E8; //#      4
  57. const long PRESET9_AUTO_RELOAD = 0xA809E8; //#       5
  58. const long PRESET10_HANDICAP = 0xA80AE8; //#      6
  59. const long PRESET11_BBOY_SECRET = 0xA80BE8; //#      7
  60. const long PRESET12_DRUM_MAGAZINE = 0xA80CE8; //#      8
  61. const long PRESET13_POWER_2 = 0xA80DE8; //#      9
  62. const long PRESET14_POWER_4 = 0xA80EE8; //#
  63. const long PRESET15_POWER_50 = 0xA80FE8; //* 3 Visible Boost
  64. const long PRESET16_POWER_75 = 0xA810E8; //* 4 Terminator
  65. const long PRESET17_MILSIM = 0xA811E8; //* 5 Zombie
  66. const long PRESET18_FAST_RELOAD = 0xA812E8; //* 6 Battle Royale
  67. const long PRESET19_TIME_TO_START_5 = 0xA813E8; //* 6 Battle Royale
  68. const long PRESET20_AUTO_RESPAWN_30 = 0xA814E8; //* 6 Battle Royale
  69. const long PRESET21_GLOW = 0xA815E8; //* 6 Battle Royale
  70. const long PRESET22_FRIENDLY_FIRE_OFF = 0xA816E8; //* 6 Battle Royale
  71. const long PRESET23_FRIENDLY_FIRE_ON = 0xA817E8; //* 6 Battle Royale
  72. const long PRESET24_JUGGERNAUT_300 = 0xA818E8; //* 6 Battle Royale
  73. const long PRESET25_MAGNUM = 0xA819E8; //* 6 Battle Royale
  74. const long PRESET26_SLOW_GUN = 0xA81AE8; //* 6 Battle Royale
  75. const long PRESET27_REGENERATION = 0xA81BE8; //* 6 Battle Royale
  76. const long PRESET28_TIME_TO_START_10 = 0xA81CE8; //* 6 Battle Royale
  77. const long PRESET29_SHOCK = 0xA81DE8; //* 6 Battle Royale
  78. const long PRESET30_IMPOSTOR_CREW = 0xA81EE8; //* 6 Battle Royale
  79. const long PRESET31_IMPOSTOR = 0xA81FE8; //* 6 Battle Royale
  80.  
  81. const long PLAY_ACTIVATION_SOUND = 0xAA3AE8; //Play the sound to warn about statictics deletion
  82.  
  83.  
  84. //GAME MODES:
  85. //0 1 Deathmatch
  86. //0 2 Terminator
  87. //0 3 Battle Royale: FF ON
  88. //0 A Battle Royale/Zombie
  89. //0 4 Battle Royale: FF Off
  90. //0 5 Milsim Realistic
  91. //0 6 Imposter Alien
  92. //0 B Imposter Crew
  93. //0 7 Autorespawn 30
  94. //0 8 Time to start 5
  95. //0 9 Time to start 10
  96. //0 C Shock
  97. //0 # Regeneration
  98. //0 D
  99.  
  100. //Mods:
  101. //
  102. //
  103. //
  104. //
  105. //
  106. //
  107. //
  108. //
  109. //
  110. //
  111. //
  112.  
  113. //Power 8, 2, 4, 35, 50, 75, 100 *1
  114. //Power 2, 4, 8, 35, 50, 75, 100 *2
  115. //Power 2, 4, 8, 35, 50, 75, 100 * 3
  116. //Power 2, 4, 8, 35, 50, 75, 100 * A
  117. //Power 2, 4, 8, 35, 50, 75, 100 * 4
  118. //Power 2, 4, 8, 35, 50, 75, 100 * 5
  119. //Power 2, 4, 8, 35, 50, 75, 100 * 6
  120. //Scout Bboy jawny *B
  121. //Drum magazine *7
  122. //Magnum *8
  123. //Juggernaut *9
  124. //Fast reload *C
  125. //Slow Gun *#
  126. //Handicap *D
  127.  
  128.  
  129. //Commands:
  130. //Team red, blue, green, yellow A
  131. //Team red, blue, green, yellow B
  132. // Team red, blue, green, yellow C
  133. // Team red, blue, green, yellow D
  134. //Newgame 1
  135. //Disarm 2
  136. //Delayed start 3
  137. //Kill 4
  138. //Glow 5
  139. //Change Power  6
  140. //BBoy tajny 7
  141. //Autoreload 8
  142. //Double health jednorazowy 9
  143. //Clear stats # //Wymaga przytrzymania, by potwierdzić, potem idzie bezpośrednio
  144.  
  145. //3*16=48
  146. //3*15=45
  147. //3*14=42
  148.  
  149. //11+31 =42 = 4*12
  150. //123A
  151. //456B
  152. //789C
  153. //*0#D
  154. const byte KEY_1=1;const byte KEY_2=2;const byte KEY_3=3;const byte KEY_A=4;
  155. const byte KEY_4=5;const byte KEY_5=6;const byte KEY_6=7;const byte KEY_B=8;
  156. const byte KEY_7=9;const byte KEY_8=10;const byte KEY_9=11;const byte KEY_C=12;
  157. const byte KEY_SHIFT_1=13;const byte KEY_SHIFT_2=14;const byte KEY_HASH=15;const byte KEY_D=16;
  158. const byte KEY_BIAS=1;
  159.  
  160.  
  161. char hexKeys[ROWS][COLS] = {
  162.    {(char)KEY_1,(char)KEY_2,(char)KEY_3,(char)KEY_A},
  163.    {(char)KEY_4,(char)KEY_5,(char)KEY_6,(char)KEY_B},
  164.    {(char)KEY_7,(char)KEY_8,(char)KEY_9,(char)KEY_C},
  165.    {(char)KEY_SHIFT_1,(char)KEY_SHIFT_2,(char)KEY_HASH,(char)KEY_D}
  166. };
  167.  
  168. Keypad customKeypad = Keypad(makeKeymap(hexKeys),rowPins,colPins,ROWS,COLS);
  169.  
  170. long remoteCodes[ROWS*COLS] = {
  171.   NEW_GAME,DISARM,NEW_GAME_DELAYED,TEAM_RED,
  172.   ADMIN_KILL,PRESET21_GLOW,SWAP_IR_POWER,TEAM_BLUE,
  173.   PRESET11_BBOY_SECRET,PRESET9_AUTO_RELOAD,DOUBLE_HEALTH,TEAM_GREEN,
  174.   NEW_GAME,NEW_GAME,CLEAR_STATS,TEAM_YELLOW};
  175.  
  176. long remoteCodesShift1[ROWS*COLS] = {
  177.   PRESET2_SNIPER_POWER_8,PRESET13_POWER_2,PRESET14_POWER_4,PRESET1_STORMTROOPER_POWER_35,
  178.   PRESET15_POWER_50,PRESET16_POWER_75,PRESET3_MACHINEGUNNER_POWER_100,PRESET5_RECON_BOOST,
  179.   PRESET12_DRUM_MAGAZINE,PRESET25_MAGNUM,PRESET24_JUGGERNAUT_300,PRESET18_FAST_RELOAD,
  180.   NEW_GAME,NEW_GAME,PRESET26_SLOW_GUN,PRESET10_HANDICAP};
  181.  
  182. long remoteCodesShift2[ROWS*COLS] = {
  183.     PRESET4_MEDIC_AUTORESPAWN,PRESET6_TERMINATOR,PRESET23_FRIENDLY_FIRE_ON,PRESET7_ZOMBIE,
  184.     PRESET22_FRIENDLY_FIRE_OFF,PRESET17_MILSIM,PRESET31_IMPOSTOR,PRESET30_IMPOSTOR_CREW,
  185.     PRESET20_AUTO_RESPAWN_30,PRESET19_TIME_TO_START_5,PRESET28_TIME_TO_START_10,PRESET29_SHOCK,
  186.     NEW_GAME,NEW_GAME,PRESET27_REGENERATION,ADMIN_KILL
  187.   };
  188.  
  189. long * remoteCodesPointer=remoteCodes;
  190. long LongIrCode=0;
  191. byte clearStatsSafe = 0;
  192. const byte CLEAR_STATS_THRESHOLD = 3;
  193.  
  194. enum remoteModeEnum{QUICK, SHIFT1, SHIFT2};
  195. remoteModeEnum remoteMode = QUICK;
  196. //IrSend object - library irremote
  197. IRsend irsend;
  198. byte irCodeArray[SIGNAL_LENGTH];
  199.  
  200. void setup() {
  201.     Serial.begin(9600);
  202.     customKeypad.setHoldTime(500);
  203.  
  204.   // pin IR_DIODE ma pracowac jako wyjscie
  205.   pinMode(RED_PIN, OUTPUT);
  206.   pinMode(GREEN_PIN, OUTPUT);
  207.   IrSender.begin(IR_SEND_PIN, ENABLE_LED_FEEDBACK); // Specify send pin and enable feedback LED at default feedback LED pin
  208.  
  209.   fillWithHex(irCodeArray,NEW_GAME);
  210. }
  211.  
  212. byte keypadState = 0;
  213. byte loopCount=0;
  214. void loop() {
  215.   indicateMode();
  216.     char customKey = customKeypad.getKey();
  217.     if(customKey){
  218.     Serial.println("Key:");
  219.     Serial.println((byte)customKey);
  220.     Serial.println("Shift mode: ");
  221.     Serial.println(remoteMode);
  222.     Serial.println("Keypad state: ");
  223.         int row=0;
  224.         int col=0;
  225.         switch (customKey)
  226.         {
  227.             case KEY_SHIFT_1 :  changeMode(SHIFT1); break;
  228.             case KEY_SHIFT_2 :  changeMode(SHIFT2); break;
  229.             default :   switch (remoteMode) {
  230.                             case SHIFT1: remoteCodesPointer = remoteCodesShift1; break;
  231.                             case SHIFT2: remoteCodesPointer = remoteCodesShift2; break;
  232.                             default: remoteMode=QUICK; remoteCodesPointer = remoteCodes;  break;
  233.                         }
  234.            long remoteCode = checkClearStats(remoteCodesPointer[customKey-KEY_BIAS]);
  235.            
  236.             Serial.println("irCode :");
  237.            Serial.println(remoteCode);
  238.                         sendCode(remoteCode);
  239.                         break;
  240.         }
  241.     }
  242.   delay(50);
  243. }
  244. long checkClearStats(long remoteCode)
  245. {
  246.   if(remoteCode==CLEAR_STATS)
  247.   {
  248.     if(clearStatsSafe < CLEAR_STATS_THRESHOLD)
  249.     {
  250.       clearStatsSafe++;
  251.       remoteCode= PLAY_ACTIVATION_SOUND;
  252.     }
  253.   } else {
  254.     Serial.println("Other button");
  255.     clearStatsSafe = 0;
  256.   }
  257.   return remoteCode;
  258. }
  259. void changeMode(remoteModeEnum mode)
  260. {
  261.     if(mode==remoteMode){
  262.         remoteMode=QUICK;
  263.     }
  264.     else{
  265.         remoteMode = mode;
  266.     }
  267. }
  268.  
  269. void sendCode(long hexIrCode)
  270. {
  271.     digitalWrite(RED_PIN, HIGH);
  272.     fillWithHex(irCodeArray,hexIrCode);
  273.     sendCode(irCodeArray);
  274. }
  275. //Sends MILESTAG 2 ir code using specified IR Sender
  276. //irCode - a byte array
  277. void sendCode(byte *irCode){
  278.   uint16_t intIrCode[SIGNAL_LENGTH];
  279.   Serial.println("IrCode");
  280.   for(int i=0;i<SIGNAL_LENGTH;i++){
  281.     intIrCode[i]=irCode[i]*100;
  282.     Serial.print(irCode[i]);
  283.   }
  284.   IrSender.sendRaw(intIrCode, SIGNAL_LENGTH, KHZ); //Note the approach used to automatically calculate the size of the array.
  285. }
  286.  
  287. //This funtion fills given byteArray with code lenghts corresponding with a long representing a hex code
  288. //Every second byte will be filled with "empty" signal to satisfly MILESTAG 2 Protocol
  289. //byteIRArray - a pointer to byte array. For laserwar a length of 50 is required
  290. //hexIrCode - long, preferrably written in a hex, which will be coded in MILESTAG 2 Protocol. Maximum size is 3 bytes. Longer signals are supported by the protocol
  291. //for configuration purposes, but that would require a representation longer than
  292. void fillWithHex(byte * byteIrArray, long hexIrCode)
  293. {
  294.   for(int i=SIGNAL_LENGTH-2;i>1;i-=2)
  295.   {
  296.     if(hexIrCode % 2 == 0){
  297.       byteIrArray[i]= ZER;
  298.     }
  299.     else {
  300.       byteIrArray[i]=ONE;
  301.     }
  302.     byteIrArray[SIGNAL_LENGTH-1] = EMPTY;
  303.     byteIrArray[i-1] = EMPTY;
  304.     hexIrCode/=2;
  305.   }
  306.   byteIrArray[1]= EMPTY; byteIrArray[0]=HEADER;
  307. }
  308.  
  309. byte diodeModulation = 2;
  310. const byte DIODE_FAST = 2;
  311. const byte DIODE_SLOW = 10;
  312. void indicateMode()
  313. {
  314.    
  315.   loopCount= ((loopCount+1) % diodeModulation);
  316.   if(loopCount==0)
  317.   {
  318.       if(remoteMode==QUICK)
  319.       {
  320.           digitalWrite(GREEN_PIN, HIGH);
  321.           digitalWrite(RED_PIN, LOW);
  322.           diodeModulation = DIODE_FAST;
  323.       }
  324.       else {
  325.           diodeModulation = DIODE_SLOW;
  326.       }
  327.     if(remoteMode==SHIFT1){
  328.       digitalWrite(GREEN_PIN, HIGH);
  329.       digitalWrite(RED_PIN, LOW);
  330.     } else if (remoteMode==SHIFT2){
  331.         digitalWrite(GREEN_PIN, LOW);
  332.       digitalWrite(RED_PIN, HIGH);
  333.     }
  334.     if(clearStatsSafe>=CLEAR_STATS_THRESHOLD){
  335.       digitalWrite(GREEN_PIN, HIGH);
  336.       digitalWrite(RED_PIN, HIGH);
  337.     }
  338.   }
  339.   else
  340.   {
  341.       digitalWrite(GREEN_PIN, LOW);
  342.       digitalWrite(RED_PIN, LOW);
  343.   }
  344. }
  345.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement