Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Arduino.h>
- #include <Wire.h>
- #define txPinIR 12 //IR carrier output /
- unsigned char carrierFreq = 0; //default
- unsigned char period = 0; //calculated once for each signal sent in initSoftPWM
- unsigned char periodHigh = 0; //calculated once for each signal sent in initSoftPWM
- unsigned char periodLow = 0; //calculated once for each signal sent in initSoftPWM
- unsigned long sigTime = 0; //used in mark & space functions to keep track of time
- unsigned long sigStart = 0; //used to calculate correct length of existing signal, to handle some repeats
- #define NEC_HEX_VALUE 0x20DF22DDUL //UL makes this an unsigned long
- #define NEC_BIT_COUNT 24 // header +24 for message
- /////////////////////////////////////////////////////////////////////
- // MilesTag command pack (0x... ...UL)
- #define New_GameIM 0x8305E8UL // New Game (Immediate)
- #define Clear_Scores 0x8314E8UL //Clear Scores
- #define Admin_Kill 0x8300E8UL //Admin Kill
- #define Team_Red 0xA900E8UL // Team Red
- #define Team_Blue 0xA901E8UL // Team Blue
- #define Team_Yellow 0xA902E8UL // Team Yellow
- #define Team_Green 0xA903E8UL //Team Green
- #define Mode_Sniper 0xA802E8UL // Sniper
- #define Mode_Medic 0xA804EUL // Medic
- #define Explode 0x830BE8UL //Explode
- //////////////////////////////////////////////////////////////////
- //MilesTag shot pack
- #define Red_10dm 0x015C5UL
- #define Yellow_100dm 0x015EFUL
- ///////////////////////////////////
- //
- #define led_red 4
- #define led_green 3
- #define led_blue 2
- #define button1Pin 4
- #define button2Pin 2
- #define button3Pin 9
- int button1State;
- int lastbutton1State = LOW;
- int button2State;
- int lastbuton2State = LOW;
- int button3State;
- int lasbutton3State = LOW;
- class Elapsed
- {
- unsigned long startus, startms;
- public:
- // constructor resets time
- Elapsed () {
- reset ();
- };
- // reset time to now
- void reset () {
- startus = micros ();
- startms = millis ();
- };
- // return Elapsed time in milliseconds
- unsigned long intervalMs () {
- return millis () - startms;
- };
- // return Elapsed time in microseconds
- unsigned long intervalUs () {
- return micros () - startus;
- };
- }; // end of class Elapsed
- static Elapsed t_1, t_2, t_3, t_bazook, t_flash, t_itter;
- char message;
- int bazook = 0;
- int mes_count = 0;
- void setup() {
- Wire.begin(9);
- Serial.begin(2000000);
- pinMode(txPinIR, OUTPUT);
- pinMode(led_red, OUTPUT);
- pinMode(led_green, OUTPUT);
- pinMode(led_blue, OUTPUT);
- Serial.println("\nReady");
- Wire.onReceive(receiveEvent);
- led_white();
- delay(200);
- led_off();
- }
- void receiveEvent(int bytes) {
- message = Wire.read();
- Serial.println(message);
- // Serial.println(Wire.read());
- }
- void initSoftPWM(unsigned char carrierFreq) { // Assumes standard 8-bit Arduino, running at 16Mhz
- //supported values are 30, 33, 36, 38, 40, 56 kHz, any other value defaults to 38kHz
- //we will aim for a duty cycle of circa 33%
- period = (1000 + carrierFreq / 2) / carrierFreq;
- periodHigh = (period + 1) / 3;
- periodLow = period - periodHigh;
- // Serial.println (period);
- // Serial.println (periodHigh);
- // Serial.println (periodLow);
- //Serial.println (carrierFreq);
- switch (carrierFreq) {
- case 30 : //delivers a carrier frequency of 29.8kHz & duty cycle of 34.52%
- periodHigh -= 6; //Trim it based on measurementt from Oscilloscope
- periodLow -= 10; //Trim it based on measurementt from Oscilloscope
- break;
- case 33 : //delivers a carrier frequency of 32.7kHz & duty cycle of 34.64%
- periodHigh -= 6; //Trim it based on measurementt from Oscilloscope
- periodLow -= 10; //Trim it based on measurementt from Oscilloscope
- break;
- case 36 : //delivers a carrier frequency of 36.2kHz & duty cycle of 35.14%
- periodHigh -= 6; //Trim it based on measurementt from Oscilloscope
- periodLow -= 11; //Trim it based on measurementt from Oscilloscope
- break;
- case 40 : //delivers a carrier frequency of 40.6kHz & duty cycle of 34.96%
- periodHigh -= 6; //Trim it based on measurementt from Oscilloscope
- periodLow -= 11; //Trim it based on measurementt from Oscilloscope
- break;
- case 56 : //delivers a carrier frequency of 53.8kHz & duty cycle of 40.86%
- periodHigh -= 6; //Trim it based on measurementt from Oscilloscope
- periodLow -= 12; //Trim it based on measurementt from Oscilloscope
- //Serial.println(periodHigh);
- //Serial.println(periodLow);
- break;
- case 38 : //delivers a carrier frequency of 37.6kHz & duty cycle of 36.47%
- default :
- periodHigh -= 6; //Trim it based on measurementt from Oscilloscope
- periodLow -= 11; //Trim it based on measurementt from Oscilloscope
- break;
- }
- }
- void mark(unsigned int mLen) { //uses sigTime as end parameter
- sigTime += mLen; //mark ends at new sigTime
- unsigned long now = micros();
- unsigned long dur = sigTime - now; //allows for rolling time adjustment due to code execution delays
- if (dur == 0) return;
- while ((micros() - now) < dur) { //just wait here until time is up
- digitalWrite(txPinIR, HIGH);
- if (periodHigh) delayMicroseconds(periodHigh);
- digitalWrite(txPinIR, LOW);
- if (periodLow) delayMicroseconds(periodLow);
- }
- }
- void space(unsigned int sLen) { //uses sigTime as end parameter
- sigTime += sLen; //space ends at new sigTime
- unsigned long now = micros();
- unsigned long dur = sigTime - now; //allows for rolling time adjustment due to code execution delays
- if (dur == 0) return;
- while ((micros() - now) < dur) ; //just wait here until time is up
- }
- void sendHexNEC(unsigned long sigCode, byte numBits, unsigned char repeats, unsigned char kHz) {
- /* A basic 32 bit NEC signal is made up of:
- 1 x 9000 uSec Header Mark, followed by
- 1 x 4500 uSec Header Space, followed by
- 32 x bits uSec ( 1- bit 560 uSec Mark followed by 1690 uSec space; 0 - bit 560 uSec Mark follwed by 560 uSec Space)
- 1 x 560 uSec Trailer Mark
- There can also be a generic repeat signal, which is usually not neccessary & can be replaced by sending multiple signals
- */
- #define NEC_HEADER_MARK 2400
- #define NEC_HEADER_SPACE 600
- #define NEC_ONE_MARK 1200
- #define NEC_ZERO_MARK 600
- #define NEC_ONE_SPACE 600
- #define NEC_ZERO_SPACE 600
- #define NEC_TRAILER_MARK 560
- unsigned long bitMask = (unsigned long) 1 << (numBits - 1); //allows for signal from 1 bit up to 32 bits
- //
- if (carrierFreq != kHz) initSoftPWM(kHz); //we only need to re-initialise if it has changed from last signal sent
- sigTime = micros(); //keeps rolling track of signal time to avoid impact of loop & code execution delays
- sigStart = sigTime; //remember for calculating first repeat gap (space), must end 108ms after signal starts
- // First send header Mark & Space
- mark(NEC_HEADER_MARK);
- space(NEC_HEADER_SPACE);
- while (bitMask) {
- if (bitMask & sigCode) { //its a One bit
- mark(NEC_ONE_MARK);
- space(NEC_ONE_SPACE);
- }
- else { // its a Zero bit
- mark(NEC_ZERO_MARK);
- space(NEC_ZERO_SPACE);
- }
- bitMask = (unsigned long) bitMask >> 1; // shift the mask bit along until it reaches zero & we exit the while loop
- }
- // Last send NEC Trailer MArk
- //mark(NEC_TRAILER_MARK);
- //now send the requested number of NEC repeat signals. Repeats can be useful for certain functions like Vol+, Vol- etc
- /* A repeat signal consists of
- A space which ends 108ms after the start of the last signal in this sequence
- 1 x 9000 uSec Repeat Header Mark, followed by
- 1 x 2250 uSec Repeat Header Space, followed by
- 32 x bits uSec ( 1- bit 560 uSec Mark followed by 1690 uSec space; 0 - bit 560 uSec Mark follwed by 560 uSec Space)
- 1 x 560 uSec repeat Trailer Mark
- */
- //First calcualte length of space for first repeat
- //by getting length of signal to date and subtracting from 108ms
- if (repeats == 0) return; //finished - no repeats
- else if (repeats > 0) { //first repeat must start 108ms after first signal
- space(108000 - (sigTime - sigStart)); //first repeat Header should start 108ms after first signal
- mark(NEC_HEADER_MARK);
- space(NEC_HEADER_SPACE / 2); //half the length for repeats
- mark(NEC_TRAILER_MARK);
- }
- while (--repeats > 0) { //now send any remaining repeats
- space(108000 - NEC_HEADER_MARK - NEC_HEADER_SPACE / 2 - NEC_TRAILER_MARK); //subsequent repeat Header must start 108ms after previous repeat signal
- mark(NEC_HEADER_MARK);
- space(NEC_HEADER_SPACE / 2); //half the length for repeats
- mark(NEC_TRAILER_MARK);
- }
- }
- void order() {
- String commandline = Serial.readString();
- //Serial.println(commandline);
- if (commandline.startsWith("new game")) {
- Serial.println(F("Sending New Game (Immediate)"));
- sendHexNEC(New_GameIM, NEC_BIT_COUNT, 1, 56);
- }
- if (commandline.startsWith("shoot rockets")) {
- shootrocket();
- }
- }
- void shootrocket() {
- Serial.println(F("Sending Explode"));
- sendHexNEC(Explode, NEC_BIT_COUNT, 1, 56);
- Serial.println("shooting rockets");
- digitalWrite(led_blue, 1);
- digitalWrite(led_green, 1);
- digitalWrite(led_red, 0);
- Serial.println(F("Sending Explode"));
- sendHexNEC(Explode, NEC_BIT_COUNT, 1, 56);
- for (int i = 1000; i > 400; i--) {
- tone(5, i);
- delay(1);
- }
- Serial.println(F("Sending Explode"));
- sendHexNEC(Explode, NEC_BIT_COUNT, 1, 56);
- for (int i = 100; i > 50; i--) {
- tone(5, i);
- delay(10);
- }
- Serial.println(F("Sending Explode"));
- sendHexNEC(Explode, NEC_BIT_COUNT, 1, 56);
- for (int i = 50; i > 32; i--) {
- tone(5, i);
- delay(50);
- }
- noTone(5);
- Serial.println(F("Sending Explode"));
- sendHexNEC(Explode, NEC_BIT_COUNT, 1, 56);
- digitalWrite(led_green, 0);
- digitalWrite(led_red, 1);
- digitalWrite(led_blue, 1);
- }
- void led_off() {
- digitalWrite(led_red, 0);
- digitalWrite(led_green, 0);
- digitalWrite(led_blue, 0);
- }
- void led_white() {
- digitalWrite(led_red, 1);
- digitalWrite(led_green, 1);
- digitalWrite(led_blue, 1);
- }
- void loop() {
- if (Serial.available() > 0) {
- order();
- }
- if (message == 1) {
- mes_count++;
- }
- if (message == 9) {
- for (int i = 0; i < 50; i++) {
- sendHexNEC(New_GameIM, NEC_BIT_COUNT, 1, 56);
- delay(100);
- led_white();
- delay(50);
- led_off();
- delay(50);
- }
- }
- if (message != 0) {
- message = 0;
- }
- //Serial.println(message);
- if (mes_count == 0) {
- led_off();
- }
- if (mes_count == 1) {
- led_off();
- digitalWrite(led_green, 1);
- }
- if (mes_count == 2) {
- led_off();
- digitalWrite(led_blue, 1);
- }
- if (mes_count == 3) {
- for (int i = 0; i <= 5; i++) {
- led_off();
- digitalWrite(led_red, 1);
- Serial.println(F("Sending Explode"));
- sendHexNEC(Explode, NEC_BIT_COUNT, 1, 56);
- led_off();
- delay(100);
- }
- mes_count = 0;
- }
- //////////////////////////////////////////
- //
- //
- // Serial.println(F("Sending explode @ 56kHz"));
- // sendHexNEC(Explode, NEC_BIT_COUNT, 1, 56);
- // Serial.println(Explode,BIN);
- // delay(4000);
- //
- // Serial.println(F("Sending New Game (Immediate)"));
- // sendHexNEC(New_GameIM, NEC_BIT_COUNT, 1, 56);
- // Serial.println(New_GameIM,BIN);
- // delay(2000);
- //
- // Serial.println(F("Bue"));
- // sendHexNEC(Team_Blue, NEC_BIT_COUNT, 1, 56);
- // Serial.println(Team_Blue,BIN);
- // delay(5000);
- //
- // Serial.println(F("ed Clear Scores"));
- // sendHexNEC(Team_Red, NEC_BIT_COUNT, 1, 56);
- // Serial.println(Team_Red,BIN);
- // delay(5000);
- //
- // Serial.println(F("Sending sd Scores"));
- // sendHexNEC(Team_Yellow, NEC_BIT_COUNT, 1, 56);
- // Serial.println(Team_Yellow,BIN);
- // delay(5000);
- //
- // Serial.println(F("Sending Clear ds"));
- // sendHexNEC(Team_Green, NEC_BIT_COUNT, 1, 56);
- // Serial.println(Team_Green,BIN);
- // delay(5000);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement