Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Arduino.h>
- #include <Wire.h>
- #define IRpin_PIN PIND
- #define IRpin 2
- #define MAXPULSE 70
- #define NUMPULSES 30
- #define RESOLUTION 50
- String Packet = "";
- String pack = "";
- String empty = "";
- unsigned long lastDebounceTime = 0; // the last time the output pin was toggled
- unsigned long debounceDelay = 50; // the debounce time; increase if the output flickers
- /////////////////////////////////////////////////
- const String New_GameIM = "100000110000010111101000";
- const String Admin_Kill = "100000110000000011101000";
- const String Clear_Scores = "100000110001010011101000";
- ///////////////////////////////////////////////////
- uint16_t pulses[NUMPULSES][2]; // pair is high and low pulse
- uint16_t MilesMessageBIN[24]; // binary message
- uint16_t MilesShotBIN[14]; // binary message
- uint8_t currentpulse = 0; // index for pulses we're storing
- 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_standby;
- int bazook = 0;
- int zero = 0;
- int message = 0;
- void setup(void) {
- Wire.begin();
- Serial.begin(9600);
- Serial.println("Ready to decode IR!");
- }
- String RawToBin() {
- for (uint32_t i = 0; i < currentpulse; i++) {
- // check for Header
- if ((2200 <= (pulses[i][1] * RESOLUTION)) && (2600 >= (pulses[i][1] * RESOLUTION))) {
- int Header = i;
- i++;
- for (uint32_t m = Header; m <= currentpulse - 1; m++) {
- if (400 < (pulses[m][1] * RESOLUTION) && (pulses[m][1] * RESOLUTION) < 800) {
- pack.concat(0);
- }
- if ((1000 < (pulses[m][1] * RESOLUTION) && (pulses[m][1] * RESOLUTION) < 1400)) {
- pack.concat(1);
- }
- }
- return pack;
- }
- }
- return "error";
- }
- void printpulses(void) {
- Serial.println("\n\r\n\rReceived: \n\rRAW");
- for (uint8_t i = 0; i < currentpulse; i++) {
- Serial.print(pulses[i][1] * RESOLUTION, DEC);
- Serial.print("\t");
- Serial.println(" ");
- }
- }
- int listenForIR(void) {
- currentpulse = 0;
- while (1) {
- uint16_t highpulse, lowpulse; // temporary storage timing
- highpulse = lowpulse = 0; // start out with no pulse length
- while (IRpin_PIN & (1 << IRpin)) {
- highpulse++;
- delayMicroseconds(RESOLUTION);
- if (((highpulse >= MAXPULSE) && (currentpulse != 0)) || currentpulse == NUMPULSES) {
- // printpulses(); //RAW data
- return currentpulse;
- }
- }
- pulses[currentpulse][0] = highpulse;
- while (! (IRpin_PIN & _BV(IRpin))) {
- lowpulse++;
- delayMicroseconds(RESOLUTION);
- if (((lowpulse >= MAXPULSE) && (currentpulse != 0)) || currentpulse == NUMPULSES) {
- // printpulses(); //RAW data
- return currentpulse;
- }
- }
- pulses[currentpulse][1] = lowpulse;
- currentpulse++;
- }
- }
- void decode_shoot() {
- if (Packet.charAt(0) == '0')//shot packet
- {
- message = 1;
- //Serial.println("Shot Packet");
- //////////////
- }
- }
- void decode_message() {
- if (Packet.charAt(0) == '1') //Message packet
- {
- Serial.println("Message Packet");
- if (Packet.equals(Admin_Kill)) {
- Serial.println("Received Admin Kill");
- }
- if (Packet.equals(New_GameIM)) {
- message = 9;
- Serial.println("Received New Game Immediate");
- }
- if (Packet.equals(Clear_Scores)) {
- Serial.println("Received Clear Scores");
- }
- }
- //else{Serial.println("Not in database");}
- }
- void sendmessage() {
- Wire.beginTransmission(9);
- Wire.write(message);
- Wire.endTransmission();
- //Serial.println(Packet);
- }
- void order() {
- String commandline = Serial.readString();
- //Serial.println(commandline);
- if (commandline.startsWith("safety off")) {
- }
- }
- void loop(void) {
- if (Serial.available() > 0) {
- order();
- }
- int numberpulses;
- numberpulses = listenForIR(); //czeka na sygnal
- Serial.print("\nHeard ");
- Serial.print(numberpulses);
- Serial.println("-pulse long IR signal");
- // Shot
- if (numberpulses == 15) {
- Packet = RawToBin();
- Serial.println(Packet);
- decode_shoot();
- //
- }
- if (numberpulses == 25) {
- Packet = RawToBin();
- Serial.println(Packet);
- decode_message();
- }
- sendmessage();
- if (message != 0) {
- message = 0;
- }
- Packet = empty;
- pack = empty;
- // for( int i=0; i<=9;i++){
- //
- // Serial.write(message,15);
- //
- // if(i==9){
- // i=0;
- // }
- // delay(500);
- // }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement