Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Software for laserwar remote
- #include <Keypad.h>
- #include <IRremote.h>
- const byte GREEN_PIN = 2;
- const byte RED_PIN = 3;
- const byte IR_SEND_PIN = 4;
- //IR code signal lenghts, divided by 100
- const byte EMPTY=6;
- const byte ZER=6;
- const byte ONE=12;
- const byte HEADER=24;
- //Ir Frequency in Kilohertz
- const int KHZ = 56;
- //IR message lenght
- const byte SIGNAL_LENGTH=50;
- //Keyboard dimensions
- const byte ROWS = 4;
- const byte COLS = 4;
- byte rowPins[ROWS] = {5,6,7,8};
- byte colPins[COLS] = {9,10,11,12};
- // Codes used in remote
- //Used very often, generally not harmfull //0
- const long NEW_GAME = 0x8305E8; //1
- const long ADMIN_KILL = 0x8300E8; //2
- const long NEW_GAME_DELAYED = 0x8302E8; //3 check with 0х0C
- const long DISARM = 0x8317E8; //4 Usefull on kids
- const long DOUBLE_HEALTH = 0x8310E8;//5
- const long TEAM_RED = 0xA900E8; //A
- const long TEAM_BLUE = 0xA901E8; //B
- const long TEAM_YELLOW = 0xA902E8; //C
- const long TEAM_GREEN = 0xA903E8; //D
- //Quick presets
- const long CLEAR_STATS = 0x8314E8; //* 1
- const long SWAP_IR_POWER = 0x830EE8; //* 2
- //RARE_PRESETS
- const long PRESET2_SNIPER_POWER_8 = 0xA802E8; //6 Indoor
- const long PRESET1_STORMTROOPER_POWER_35 = 0xA801E8; //7 Medium Outdoor
- const long PRESET3_MACHINEGUNNER_POWER_100 = 0xA803E8; //8 Long Outdoor
- const long PRESET4_MEDIC_AUTORESPAWN = 0xA804E8; //9 Deathmatch
- const long PRESET5_RECON_BOOST = 0xA805E8; //# 1
- const long PRESET6_TERMINATOR = 0xA806E8; //# 2
- const long PRESET7_ZOMBIE = 0xA807E8; //# 3
- const long PRESET8_LAST_MAN_STANDING = 0xA808E8; //# 4
- const long PRESET9_AUTO_RELOAD = 0xA809E8; //# 5
- const long PRESET10_HANDICAP = 0xA80AE8; //# 6
- const long PRESET11_BBOY_SECRET = 0xA80BE8; //# 7
- const long PRESET12_DRUM_MAGAZINE = 0xA80CE8; //# 8
- const long PRESET13_POWER_2 = 0xA80DE8; //# 9
- const long PRESET14_POWER_4 = 0xA80EE8; //#
- const long PRESET15_POWER_50 = 0xA80FE8; //* 3 Visible Boost
- const long PRESET16_POWER_75 = 0xA810E8; //* 4 Terminator
- const long PRESET17_MILSIM = 0xA811E8; //* 5 Zombie
- const long PRESET18_FAST_RELOAD = 0xA812E8; //* 6 Battle Royale
- const long PRESET19_TIME_TO_START_5 = 0xA813E8; //* 6 Battle Royale
- const long PRESET20_AUTO_RESPAWN_30 = 0xA814E8; //* 6 Battle Royale
- const long PRESET21_GLOW = 0xA815E8; //* 6 Battle Royale
- const long PRESET22_FRIENDLY_FIRE_OFF = 0xA816E8; //* 6 Battle Royale
- const long PRESET23_FRIENDLY_FIRE_ON = 0xA817E8; //* 6 Battle Royale
- const long PRESET24_JUGGERNAUT_300 = 0xA818E8; //* 6 Battle Royale
- const long PRESET25_MAGNUM = 0xA819E8; //* 6 Battle Royale
- const long PRESET26_SLOW_GUN = 0xA81AE8; //* 6 Battle Royale
- const long PRESET27_REGENERATION = 0xA81BE8; //* 6 Battle Royale
- const long PRESET28_TIME_TO_START_10 = 0xA81CE8; //* 6 Battle Royale
- const long PRESET29_SHOCK = 0xA81DE8; //* 6 Battle Royale
- const long PRESET30_IMPOSTOR_CREW = 0xA81EE8; //* 6 Battle Royale
- const long PRESET31_IMPOSTOR = 0xA81FE8; //* 6 Battle Royale
- const long PLAY_ACTIVATION_SOUND = 0xAA3AE8; //Play the sound to warn about statictics deletion
- //GAME MODES:
- //0 1 Deathmatch
- //0 2 Terminator
- //0 3 Battle Royale: FF ON
- //0 A Battle Royale/Zombie
- //0 4 Battle Royale: FF Off
- //0 5 Milsim Realistic
- //0 6 Imposter Alien
- //0 B Imposter Crew
- //0 7 Autorespawn 30
- //0 8 Time to start 5
- //0 9 Time to start 10
- //0 C Shock
- //0 # Regeneration
- //0 D
- //Mods:
- //
- //
- //
- //
- //
- //
- //
- //
- //
- //
- //
- //Power 8, 2, 4, 35, 50, 75, 100 *1
- //Power 2, 4, 8, 35, 50, 75, 100 *2
- //Power 2, 4, 8, 35, 50, 75, 100 * 3
- //Power 2, 4, 8, 35, 50, 75, 100 * A
- //Power 2, 4, 8, 35, 50, 75, 100 * 4
- //Power 2, 4, 8, 35, 50, 75, 100 * 5
- //Power 2, 4, 8, 35, 50, 75, 100 * 6
- //Scout Bboy jawny *B
- //Drum magazine *7
- //Magnum *8
- //Juggernaut *9
- //Fast reload *C
- //Slow Gun *#
- //Handicap *D
- //Commands:
- //Team red, blue, green, yellow A
- //Team red, blue, green, yellow B
- // Team red, blue, green, yellow C
- // Team red, blue, green, yellow D
- //Newgame 1
- //Disarm 2
- //Delayed start 3
- //Kill 4
- //Glow 5
- //Change Power 6
- //BBoy tajny 7
- //Autoreload 8
- //Double health jednorazowy 9
- //Clear stats # //Wymaga przytrzymania, by potwierdzić, potem idzie bezpośrednio
- //3*16=48
- //3*15=45
- //3*14=42
- //11+31 =42 = 4*12
- //123A
- //456B
- //789C
- //*0#D
- const byte KEY_1=1;const byte KEY_2=2;const byte KEY_3=3;const byte KEY_A=4;
- const byte KEY_4=5;const byte KEY_5=6;const byte KEY_6=7;const byte KEY_B=8;
- const byte KEY_7=9;const byte KEY_8=10;const byte KEY_9=11;const byte KEY_C=12;
- const byte KEY_SHIFT_1=13;const byte KEY_SHIFT_2=14;const byte KEY_HASH=15;const byte KEY_D=16;
- const byte KEY_BIAS=1;
- char hexKeys[ROWS][COLS] = {
- {(char)KEY_1,(char)KEY_2,(char)KEY_3,(char)KEY_A},
- {(char)KEY_4,(char)KEY_5,(char)KEY_6,(char)KEY_B},
- {(char)KEY_7,(char)KEY_8,(char)KEY_9,(char)KEY_C},
- {(char)KEY_SHIFT_1,(char)KEY_SHIFT_2,(char)KEY_HASH,(char)KEY_D}
- };
- Keypad customKeypad = Keypad(makeKeymap(hexKeys),rowPins,colPins,ROWS,COLS);
- long remoteCodes[ROWS*COLS] = {
- NEW_GAME,DISARM,NEW_GAME_DELAYED,TEAM_RED,
- ADMIN_KILL,PRESET21_GLOW,SWAP_IR_POWER,TEAM_BLUE,
- PRESET11_BBOY_SECRET,PRESET9_AUTO_RELOAD,DOUBLE_HEALTH,TEAM_GREEN,
- NEW_GAME,NEW_GAME,CLEAR_STATS,TEAM_YELLOW};
- long remoteCodesShift1[ROWS*COLS] = {
- PRESET2_SNIPER_POWER_8,PRESET13_POWER_2,PRESET14_POWER_4,PRESET1_STORMTROOPER_POWER_35,
- PRESET15_POWER_50,PRESET16_POWER_75,PRESET3_MACHINEGUNNER_POWER_100,PRESET5_RECON_BOOST,
- PRESET12_DRUM_MAGAZINE,PRESET25_MAGNUM,PRESET24_JUGGERNAUT_300,PRESET18_FAST_RELOAD,
- NEW_GAME,NEW_GAME,PRESET26_SLOW_GUN,PRESET10_HANDICAP};
- long remoteCodesShift2[ROWS*COLS] = {
- PRESET4_MEDIC_AUTORESPAWN,PRESET6_TERMINATOR,PRESET23_FRIENDLY_FIRE_ON,PRESET7_ZOMBIE,
- PRESET22_FRIENDLY_FIRE_OFF,PRESET17_MILSIM,PRESET31_IMPOSTOR,PRESET30_IMPOSTOR_CREW,
- PRESET20_AUTO_RESPAWN_30,PRESET19_TIME_TO_START_5,PRESET28_TIME_TO_START_10,PRESET29_SHOCK,
- NEW_GAME,NEW_GAME,PRESET27_REGENERATION,ADMIN_KILL
- };
- long * remoteCodesPointer=remoteCodes;
- long LongIrCode=0;
- byte clearStatsSafe = 0;
- const byte CLEAR_STATS_THRESHOLD = 3;
- enum remoteModeEnum{QUICK, SHIFT1, SHIFT2};
- remoteModeEnum remoteMode = QUICK;
- //IrSend object - library irremote
- IRsend irsend;
- byte irCodeArray[SIGNAL_LENGTH];
- void setup() {
- Serial.begin(9600);
- customKeypad.setHoldTime(500);
- // pin IR_DIODE ma pracowac jako wyjscie
- pinMode(RED_PIN, OUTPUT);
- pinMode(GREEN_PIN, OUTPUT);
- IrSender.begin(IR_SEND_PIN, ENABLE_LED_FEEDBACK); // Specify send pin and enable feedback LED at default feedback LED pin
- fillWithHex(irCodeArray,NEW_GAME);
- }
- byte keypadState = 0;
- byte loopCount=0;
- void loop() {
- indicateMode();
- char customKey = customKeypad.getKey();
- if(customKey){
- Serial.println("Key:");
- Serial.println((byte)customKey);
- Serial.println("Shift mode: ");
- Serial.println(remoteMode);
- Serial.println("Keypad state: ");
- int row=0;
- int col=0;
- switch (customKey)
- {
- case KEY_SHIFT_1 : changeMode(SHIFT1); break;
- case KEY_SHIFT_2 : changeMode(SHIFT2); break;
- default : switch (remoteMode) {
- case SHIFT1: remoteCodesPointer = remoteCodesShift1; break;
- case SHIFT2: remoteCodesPointer = remoteCodesShift2; break;
- default: remoteMode=QUICK; remoteCodesPointer = remoteCodes; break;
- }
- long remoteCode = checkClearStats(remoteCodesPointer[customKey-KEY_BIAS]);
- Serial.println("irCode :");
- Serial.println(remoteCode);
- sendCode(remoteCode);
- break;
- }
- }
- delay(50);
- }
- long checkClearStats(long remoteCode)
- {
- if(remoteCode==CLEAR_STATS)
- {
- if(clearStatsSafe < CLEAR_STATS_THRESHOLD)
- {
- clearStatsSafe++;
- remoteCode= PLAY_ACTIVATION_SOUND;
- }
- } else {
- Serial.println("Other button");
- clearStatsSafe = 0;
- }
- return remoteCode;
- }
- void changeMode(remoteModeEnum mode)
- {
- if(mode==remoteMode){
- remoteMode=QUICK;
- }
- else{
- remoteMode = mode;
- }
- }
- void sendCode(long hexIrCode)
- {
- digitalWrite(RED_PIN, HIGH);
- fillWithHex(irCodeArray,hexIrCode);
- sendCode(irCodeArray);
- }
- //Sends MILESTAG 2 ir code using specified IR Sender
- //irCode - a byte array
- void sendCode(byte *irCode){
- uint16_t intIrCode[SIGNAL_LENGTH];
- Serial.println("IrCode");
- for(int i=0;i<SIGNAL_LENGTH;i++){
- intIrCode[i]=irCode[i]*100;
- Serial.print(irCode[i]);
- }
- IrSender.sendRaw(intIrCode, SIGNAL_LENGTH, KHZ); //Note the approach used to automatically calculate the size of the array.
- }
- //This funtion fills given byteArray with code lenghts corresponding with a long representing a hex code
- //Every second byte will be filled with "empty" signal to satisfly MILESTAG 2 Protocol
- //byteIRArray - a pointer to byte array. For laserwar a length of 50 is required
- //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
- //for configuration purposes, but that would require a representation longer than
- void fillWithHex(byte * byteIrArray, long hexIrCode)
- {
- for(int i=SIGNAL_LENGTH-2;i>1;i-=2)
- {
- if(hexIrCode % 2 == 0){
- byteIrArray[i]= ZER;
- }
- else {
- byteIrArray[i]=ONE;
- }
- byteIrArray[SIGNAL_LENGTH-1] = EMPTY;
- byteIrArray[i-1] = EMPTY;
- hexIrCode/=2;
- }
- byteIrArray[1]= EMPTY; byteIrArray[0]=HEADER;
- }
- byte diodeModulation = 2;
- const byte DIODE_FAST = 2;
- const byte DIODE_SLOW = 10;
- void indicateMode()
- {
- loopCount= ((loopCount+1) % diodeModulation);
- if(loopCount==0)
- {
- if(remoteMode==QUICK)
- {
- digitalWrite(GREEN_PIN, HIGH);
- digitalWrite(RED_PIN, LOW);
- diodeModulation = DIODE_FAST;
- }
- else {
- diodeModulation = DIODE_SLOW;
- }
- if(remoteMode==SHIFT1){
- digitalWrite(GREEN_PIN, HIGH);
- digitalWrite(RED_PIN, LOW);
- } else if (remoteMode==SHIFT2){
- digitalWrite(GREEN_PIN, LOW);
- digitalWrite(RED_PIN, HIGH);
- }
- if(clearStatsSafe>=CLEAR_STATS_THRESHOLD){
- digitalWrite(GREEN_PIN, HIGH);
- digitalWrite(RED_PIN, HIGH);
- }
- }
- else
- {
- digitalWrite(GREEN_PIN, LOW);
- digitalWrite(RED_PIN, LOW);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement