Advertisement
ASZK

Boom_barrel_Slave

Mar 19th, 2020
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 12.03 KB | None | 0 0
  1.  
  2. #include <Arduino.h>
  3. #include <Wire.h>
  4.  
  5. #define txPinIR   12   //IR carrier output /
  6.  
  7. unsigned char carrierFreq = 0; //default
  8. unsigned char period = 0; //calculated once for each signal sent in initSoftPWM
  9. unsigned char periodHigh = 0; //calculated once for each signal sent in initSoftPWM
  10. unsigned char periodLow = 0; //calculated once for each signal sent in initSoftPWM
  11. unsigned long sigTime = 0; //used in mark & space functions to keep track of time
  12. unsigned long sigStart = 0; //used to calculate correct length of existing signal, to handle some repeats
  13.  
  14.  
  15.  
  16. #define NEC_HEX_VALUE 0x20DF22DDUL //UL makes this an unsigned long
  17. #define NEC_BIT_COUNT 24 // header +24 for message
  18.  
  19. /////////////////////////////////////////////////////////////////////
  20. // MilesTag command pack  (0x...  ...UL)
  21. #define New_GameIM 0x8305E8UL // New Game (Immediate)
  22. #define Clear_Scores 0x8314E8UL //Clear Scores
  23. #define Admin_Kill 0x8300E8UL //Admin Kill
  24. #define Team_Red 0xA900E8UL // Team Red
  25. #define Team_Blue 0xA901E8UL // Team Blue
  26. #define Team_Yellow 0xA902E8UL // Team Yellow
  27. #define Team_Green 0xA903E8UL //Team Green
  28. #define Mode_Sniper 0xA802E8UL // Sniper
  29. #define Mode_Medic 0xA804EUL // Medic
  30. #define Explode 0x830BE8UL //Explode
  31. //////////////////////////////////////////////////////////////////
  32. //MilesTag shot pack
  33. #define Red_10dm 0x015C5UL
  34. #define Yellow_100dm 0x015EFUL
  35. ///////////////////////////////////
  36.  
  37.  
  38.  
  39. //
  40. #define led_red 4
  41. #define led_green 3
  42. #define led_blue 2
  43.  
  44. #define button1Pin 4
  45. #define button2Pin 2
  46. #define button3Pin 9
  47.  
  48. int button1State;
  49. int lastbutton1State = LOW;
  50. int button2State;
  51. int lastbuton2State = LOW;
  52. int button3State;
  53. int lasbutton3State = LOW;
  54.  
  55.  
  56. class Elapsed
  57. {
  58.     unsigned long startus, startms;
  59.  
  60.   public:
  61.  
  62.     // constructor resets time
  63.     Elapsed () {
  64.       reset ();
  65.     };
  66.  
  67.     // reset time to now
  68.     void reset () {
  69.       startus = micros ();
  70.       startms = millis ();
  71.     };
  72.  
  73.     // return Elapsed time in milliseconds
  74.     unsigned long intervalMs () {
  75.       return millis () - startms;
  76.     };
  77.  
  78.     // return Elapsed time in microseconds
  79.     unsigned long intervalUs () {
  80.       return micros () - startus;
  81.     };
  82.  
  83. };    // end of class Elapsed
  84.  
  85.  
  86. static Elapsed t_1, t_2, t_3, t_bazook, t_flash, t_itter;
  87. char message;
  88. int bazook = 0;
  89. int mes_count = 0;
  90.  
  91. void setup() {
  92.  
  93.   Wire.begin(9);
  94.   Serial.begin(2000000);
  95.   pinMode(txPinIR, OUTPUT);
  96.  
  97.   pinMode(led_red, OUTPUT);
  98.   pinMode(led_green, OUTPUT);
  99.   pinMode(led_blue, OUTPUT);
  100.  
  101.   Serial.println("\nReady");
  102.   Wire.onReceive(receiveEvent);
  103.  
  104.   led_white();
  105.   delay(200);
  106.   led_off();
  107. }
  108.  
  109. void receiveEvent(int bytes) {
  110.  
  111.   message = Wire.read();
  112.   Serial.println(message);
  113.   // Serial.println(Wire.read());
  114. }
  115.  
  116. void initSoftPWM(unsigned char carrierFreq) { // Assumes standard 8-bit Arduino, running at 16Mhz
  117.   //supported values are 30, 33, 36, 38, 40, 56 kHz, any other value defaults to 38kHz
  118.   //we will aim for a  duty cycle of circa 33%
  119.  
  120.   period =  (1000 + carrierFreq / 2) / carrierFreq;
  121.   periodHigh = (period + 1) / 3;
  122.   periodLow = period - periodHigh;
  123.   //  Serial.println (period);
  124.   //  Serial.println (periodHigh);
  125.   //  Serial.println (periodLow);
  126.   //Serial.println (carrierFreq);
  127.  
  128.   switch (carrierFreq) {
  129.     case 30  : //delivers a carrier frequency of 29.8kHz & duty cycle of 34.52%
  130.       periodHigh -= 6; //Trim it based on measurementt from Oscilloscope
  131.       periodLow  -= 10; //Trim it based on measurementt from Oscilloscope
  132.       break;
  133.  
  134.     case 33  : //delivers a carrier frequency of 32.7kHz & duty cycle of 34.64%
  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 36  : //delivers a carrier frequency of 36.2kHz & duty cycle of 35.14%
  140.       periodHigh -= 6; //Trim it based on measurementt from Oscilloscope
  141.       periodLow  -= 11; //Trim it based on measurementt from Oscilloscope
  142.       break;
  143.  
  144.     case 40  : //delivers a carrier frequency of 40.6kHz & duty cycle of 34.96%
  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 56  : //delivers a carrier frequency of 53.8kHz & duty cycle of 40.86%
  150.       periodHigh -= 6; //Trim it based on measurementt from Oscilloscope
  151.       periodLow  -= 12; //Trim it based on measurementt from Oscilloscope
  152.       //Serial.println(periodHigh);
  153.       //Serial.println(periodLow);
  154.  
  155.       break;
  156.  
  157.  
  158.     case 38  : //delivers a carrier frequency of 37.6kHz & duty cycle of 36.47%
  159.     default :
  160.       periodHigh -= 6; //Trim it based on measurementt from Oscilloscope
  161.       periodLow  -= 11; //Trim it based on measurementt from Oscilloscope
  162.       break;
  163.   }
  164. }
  165.  
  166. void mark(unsigned int mLen) { //uses sigTime as end parameter
  167.   sigTime += mLen; //mark ends at new sigTime
  168.   unsigned long now = micros();
  169.   unsigned long dur = sigTime - now; //allows for rolling time adjustment due to code execution delays
  170.   if (dur == 0) return;
  171.   while ((micros() - now) < dur) { //just wait here until time is up
  172.     digitalWrite(txPinIR, HIGH);
  173.     if (periodHigh) delayMicroseconds(periodHigh);
  174.     digitalWrite(txPinIR, LOW);
  175.     if (periodLow)  delayMicroseconds(periodLow);
  176.   }
  177. }
  178.  
  179. void space(unsigned int sLen) { //uses sigTime as end parameter
  180.   sigTime += sLen; //space ends at new sigTime
  181.   unsigned long now = micros();
  182.   unsigned long dur = sigTime - now; //allows for rolling time adjustment due to code execution delays
  183.   if (dur == 0) return;
  184.   while ((micros() - now) < dur) ; //just wait here until time is up
  185. }
  186.  
  187. void sendHexNEC(unsigned long sigCode, byte numBits, unsigned char repeats, unsigned char kHz) {
  188.   /*  A basic 32 bit NEC signal is made up of:
  189.       1 x 9000 uSec Header Mark, followed by
  190.       1 x 4500 uSec Header Space, followed by
  191.       32 x bits uSec ( 1- bit 560 uSec Mark followed by 1690 uSec space; 0 - bit 560 uSec Mark follwed by 560 uSec Space)
  192.       1 x 560 uSec Trailer Mark
  193.       There can also be a generic repeat signal, which is usually not neccessary & can be replaced by sending multiple signals
  194.   */
  195. #define NEC_HEADER_MARK 2400
  196. #define NEC_HEADER_SPACE 600
  197. #define NEC_ONE_MARK 1200
  198. #define NEC_ZERO_MARK 600
  199. #define NEC_ONE_SPACE 600
  200. #define NEC_ZERO_SPACE 600
  201. #define NEC_TRAILER_MARK 560
  202.  
  203.   unsigned long bitMask = (unsigned long) 1 << (numBits - 1); //allows for signal from 1 bit up to 32 bits
  204.   //
  205.   if (carrierFreq != kHz)  initSoftPWM(kHz); //we only need to re-initialise if it has changed from last signal sent
  206.  
  207.   sigTime = micros(); //keeps rolling track of signal time to avoid impact of loop & code execution delays
  208.   sigStart = sigTime; //remember for calculating first repeat gap (space), must end 108ms after signal starts
  209.  
  210.   // First send header Mark & Space
  211.   mark(NEC_HEADER_MARK);
  212.   space(NEC_HEADER_SPACE);
  213.  
  214.   while (bitMask) {
  215.     if (bitMask & sigCode) { //its a One bit
  216.       mark(NEC_ONE_MARK);
  217.       space(NEC_ONE_SPACE);
  218.     }
  219.     else { // its a Zero bit
  220.       mark(NEC_ZERO_MARK);
  221.       space(NEC_ZERO_SPACE);
  222.     }
  223.     bitMask = (unsigned long) bitMask >> 1; // shift the mask bit along until it reaches zero & we exit the while loop
  224.   }
  225.   // Last send NEC Trailer MArk
  226.   //mark(NEC_TRAILER_MARK);
  227.  
  228.   //now send the requested number of NEC repeat signals. Repeats can be useful for certain functions like Vol+, Vol- etc
  229.   /*  A repeat signal consists of
  230.        A space which ends 108ms after the start of the last signal in this sequence
  231.      1 x 9000 uSec Repeat Header Mark, followed by
  232.      1 x 2250 uSec Repeat Header Space, followed by
  233.      32 x bits uSec ( 1- bit 560 uSec Mark followed by 1690 uSec space; 0 - bit 560 uSec Mark follwed by 560 uSec Space)
  234.      1 x 560 uSec repeat Trailer Mark
  235.   */
  236.   //First calcualte length of space for first repeat
  237.   //by getting length of signal to date and subtracting from 108ms
  238.  
  239.   if (repeats == 0) return; //finished - no repeats
  240.   else if (repeats > 0) { //first repeat must start 108ms after first signal
  241.     space(108000 - (sigTime - sigStart)); //first repeat Header should start 108ms after first signal
  242.     mark(NEC_HEADER_MARK);
  243.     space(NEC_HEADER_SPACE / 2); //half the length for repeats
  244.     mark(NEC_TRAILER_MARK);
  245.   }
  246.  
  247.   while (--repeats > 0) { //now send any remaining repeats
  248.     space(108000 - NEC_HEADER_MARK - NEC_HEADER_SPACE / 2 - NEC_TRAILER_MARK); //subsequent repeat Header must start 108ms after previous repeat signal
  249.     mark(NEC_HEADER_MARK);
  250.     space(NEC_HEADER_SPACE / 2); //half the length for repeats
  251.     mark(NEC_TRAILER_MARK);
  252.   }
  253.  
  254. }
  255.  
  256.  
  257. void order() {
  258.   String commandline = Serial.readString();
  259.   //Serial.println(commandline);
  260.  
  261.  
  262.   if (commandline.startsWith("new game")) {
  263.     Serial.println(F("Sending New Game (Immediate)"));
  264.     sendHexNEC(New_GameIM, NEC_BIT_COUNT, 1, 56);
  265.   }
  266.  
  267.   if (commandline.startsWith("shoot rockets")) {
  268.     shootrocket();
  269.   }
  270.  
  271.  
  272.  
  273. }
  274.  
  275.  
  276. void shootrocket() {
  277.  
  278.  
  279.   Serial.println(F("Sending Explode"));
  280.   sendHexNEC(Explode, NEC_BIT_COUNT, 1, 56);
  281.  
  282.  
  283.   Serial.println("shooting rockets");
  284.   digitalWrite(led_blue, 1);
  285.   digitalWrite(led_green, 1);
  286.   digitalWrite(led_red, 0);
  287.  
  288.  
  289.   Serial.println(F("Sending Explode"));
  290.   sendHexNEC(Explode, NEC_BIT_COUNT, 1, 56);
  291.  
  292.   for (int i = 1000; i > 400; i--) {
  293.     tone(5, i);
  294.     delay(1);
  295.   }
  296.  
  297.  
  298.   Serial.println(F("Sending Explode"));
  299.   sendHexNEC(Explode, NEC_BIT_COUNT, 1, 56);
  300.  
  301.   for (int i = 100; i > 50; i--) {
  302.     tone(5, i);
  303.     delay(10);
  304.   }
  305.  
  306.   Serial.println(F("Sending Explode"));
  307.   sendHexNEC(Explode, NEC_BIT_COUNT, 1, 56);
  308.  
  309.   for (int i = 50; i > 32; i--) {
  310.     tone(5, i);
  311.     delay(50);
  312.   }
  313.   noTone(5);
  314.  
  315.  
  316.   Serial.println(F("Sending Explode"));
  317.   sendHexNEC(Explode, NEC_BIT_COUNT, 1, 56);
  318.  
  319.  
  320.  
  321.   digitalWrite(led_green, 0);
  322.   digitalWrite(led_red, 1);
  323.   digitalWrite(led_blue, 1);
  324.  
  325.  
  326. }
  327. void led_off() {
  328.   digitalWrite(led_red, 0);
  329.   digitalWrite(led_green, 0);
  330.   digitalWrite(led_blue, 0);
  331. }
  332. void led_white() {
  333.   digitalWrite(led_red, 1);
  334.   digitalWrite(led_green, 1);
  335.   digitalWrite(led_blue, 1);
  336. }
  337. void loop() {
  338.  
  339.   if (Serial.available() > 0) {
  340.     order();
  341.   }
  342.   if (message == 1) {
  343.     mes_count++;
  344.   }
  345.   if (message == 9) {
  346.     for (int i = 0; i < 50; i++) {
  347.       sendHexNEC(New_GameIM, NEC_BIT_COUNT, 1, 56);
  348.       delay(100);
  349.       led_white();
  350.       delay(50);
  351.       led_off();
  352.       delay(50);
  353.     }
  354.  
  355.   }
  356.   if (message != 0) {
  357.     message = 0;
  358.   }
  359.  
  360.  
  361.  
  362.   //Serial.println(message);
  363.   if (mes_count == 0) {
  364.     led_off();
  365.   }
  366.   if (mes_count == 1) {
  367.     led_off();
  368.     digitalWrite(led_green, 1);
  369.   }
  370.   if (mes_count == 2) {
  371.     led_off();
  372.     digitalWrite(led_blue, 1);
  373.   }
  374.   if (mes_count == 3) {
  375.  
  376.     for (int i = 0; i <= 5; i++) {
  377.       led_off();
  378.       digitalWrite(led_red, 1);
  379.  
  380.       Serial.println(F("Sending Explode"));
  381.       sendHexNEC(Explode, NEC_BIT_COUNT, 1, 56);
  382.       led_off();
  383.       delay(100);
  384.     }
  385.  
  386.     mes_count = 0;
  387.   }
  388.  
  389.  
  390.  
  391.  
  392.  
  393.  
  394.  
  395.  
  396.  
  397.  
  398.  
  399.   //////////////////////////////////////////
  400.  
  401.  
  402.  
  403.   //
  404.   //
  405.   //  Serial.println(F("Sending explode @ 56kHz"));
  406.   //  sendHexNEC(Explode, NEC_BIT_COUNT, 1, 56);
  407.   //  Serial.println(Explode,BIN);
  408.   //  delay(4000);
  409.   //
  410.   //  Serial.println(F("Sending New Game (Immediate)"));
  411.   //  sendHexNEC(New_GameIM, NEC_BIT_COUNT, 1, 56);
  412.   //  Serial.println(New_GameIM,BIN);
  413.   //  delay(2000);
  414.   //
  415.   //  Serial.println(F("Bue"));
  416.   //  sendHexNEC(Team_Blue, NEC_BIT_COUNT, 1, 56);
  417.   //  Serial.println(Team_Blue,BIN);
  418.   //  delay(5000);
  419.   //
  420.   //  Serial.println(F("ed Clear Scores"));
  421.   //  sendHexNEC(Team_Red, NEC_BIT_COUNT, 1, 56);
  422.   //  Serial.println(Team_Red,BIN);
  423.   //  delay(5000);
  424.   //
  425.   //  Serial.println(F("Sending sd Scores"));
  426.   //  sendHexNEC(Team_Yellow, NEC_BIT_COUNT, 1, 56);
  427.   //  Serial.println(Team_Yellow,BIN);
  428.   //  delay(5000);
  429.   //
  430.   //  Serial.println(F("Sending Clear ds"));
  431.   //  sendHexNEC(Team_Green, NEC_BIT_COUNT, 1, 56);
  432.   //  Serial.println(Team_Green,BIN);
  433.   //  delay(5000);
  434.  
  435. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement