Guest User

DuinoGun

a guest
Aug 31st, 2015
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 8.27 KB | None | 0 0
  1. /*
  2. * Giving Credit where Credit Is Due
  3. *
  4. * Portions of this code were derived from code posted in the Arduino forums by Paul Malmsten.
  5. * You can find the original thread here: http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1176098434
  6. *
  7. * The Audio portion of the code was derived from the Melody tutorial on the Arduino wiki
  8. * You can find the original tutorial here: http://arduino.cc/en/Tutorial/Melody
  9. */
  10.  
  11. int sensorPin  = 2;      // Sensor pin 1
  12. int senderPin  = 3;      // Infrared LED on Pin 3
  13. int triggerPin = 4;      // Pushbutton Trigger on Pin 4
  14. int speakerPin = 12;     // Positive Lead on the Piezo
  15. int blinkPin   = 13;     // Positive Leg of the LED we will use to indicate signal is received
  16.  
  17. int startBit   = 2000;   // This pulse sets the threshold for a transmission start bit
  18. int endBit     = 3000;   // This pulse sets the threshold for a transmission end bit
  19. int one        = 1000;   // This pulse sets the threshold for a transmission that represents a 1
  20. int zero       = 400;    // This pulse sets the threshold for a transmission that represents a 0
  21. int trigger;             // This is used to hold the value of the trigger read;
  22. boolean fired  = false;  // Boolean used to remember if the trigger has already been read.
  23. int ret[2];              // Used to hold results from IR sensing.
  24. int waitTime = 300;     // The amount of time to wait between pulses
  25.  
  26. int playerLine = 14;     // Any player ID >= this value is a referee, < this value is a player;
  27. int myCode     = 1;      // This is your unique player code;
  28. int myLevel    = 1;      // This is your starting level;
  29. int maxShots   = 6;      // You can fire 6 safe shots;
  30. int maxHits    = 6;      // After 6 hits you are dead;
  31. int myShots    = 0;      // You can fire 6 safe shots;
  32. int myHits     = 0;      // After 6 hits you are dead;
  33.  
  34. int maxLevel   = 9;      // You cannot be promoted past level 9;
  35. int minLevel   = 0;      // You cannot be demoted past level 0
  36.  
  37. int refPromote = 0;      // The refCode for promotion;
  38. int refDemote  = 1;      // The refCode for demotion;
  39. int refReset   = 2;      // The refCode for ammo reset;
  40. int refRevive  = 3;      // The refCode for revival;
  41.  
  42. int replySucc  = 14;     // the player code for Success;
  43. int replyFail  = 15;     // the player code for Failed;
  44.  
  45. void setup() {
  46.   pinMode(blinkPin, OUTPUT);
  47.   pinMode(speakerPin, OUTPUT);
  48.   pinMode(senderPin, OUTPUT);
  49.   pinMode(triggerPin, INPUT);
  50.   pinMode(sensorPin, INPUT);
  51.   randomSeed(analogRead(0));
  52.   for (int i = 1;i < 4;i++) {
  53.     digitalWrite(blinkPin, HIGH);
  54.     playTone(900*i, 200);
  55.     digitalWrite(blinkPin, LOW);
  56.     delay(200);
  57.   }
  58.  
  59.   Serial.begin(9600);
  60.   Serial.println("Ready: ");
  61. }
  62.  
  63. void loop() {
  64.   senseFire();
  65.   senseIR();
  66.  
  67.   if (ret[0] != -1) {
  68.     playTone(1000, 50);
  69.     Serial.print("Who: ");
  70.     Serial.print(ret[0]);
  71.     Serial.print(" What: ");
  72.     Serial.println(ret[1]);
  73.     if (ret[0] >= playerLine) {
  74.       if (ret[1] == refPromote) {
  75.         // Promote
  76.         if (myLevel < maxLevel) {
  77.           Serial.println("PROMOTED!");
  78.           myLevel++;
  79.           playTone(900, 50);
  80.           playTone(1800, 50);
  81.           playTone(2700, 50);
  82.         }
  83.       } else if (ret[1] == refDemote) {
  84.         // demote
  85.         if (myLevel > minLevel) {
  86.           Serial.println("DEMOTED!");
  87.           myLevel--;
  88.         }
  89.           playTone(2700, 50);
  90.           playTone(1800, 50);
  91.           playTone(900, 50);
  92.       } else if (ret[1] == refReset) {
  93.         Serial.println("AMMO RESET!");
  94.         myShots = maxShots;
  95.         playTone(900, 50);
  96.         playTone(450, 50);
  97.         playTone(900, 50);
  98.         playTone(450, 50);
  99.         playTone(900, 50);
  100.         playTone(450, 50);
  101.       } else if (ret[1] == refRevive) {
  102.         Serial.println("REVIVED!");
  103.         myShots = 0;
  104.         myHits = 0;
  105.         myLevel = 1;
  106.         playTone(900, 50);
  107.         playTone(1800, 50);
  108.         playTone(900, 50);
  109.         playTone(1800, 50);
  110.         playTone(900, 50);
  111.         playTone(800, 50);
  112.       }
  113.     } else {
  114.       if (ret[1] == replySucc) {
  115.         playTone(9000, 50);
  116.         playTone(450, 50);
  117.         playTone(9000, 50);
  118.         Serial.println("SUCCESS!");        
  119.       } else if (ret[1] == replyFail) {
  120.         playTone(450, 50);
  121.         playTone(9000, 50);
  122.         playTone(450, 50);
  123.         Serial.println("FAILED!");        
  124.       }
  125.       if (ret[1] <= maxLevel && ret[1] >= myLevel && myHits <= maxHits) {
  126.         Serial.println("HIT!");
  127.         myHits++;
  128.         playTone(9000, 50);
  129.         playTone(900, 50);
  130.         playTone(9000, 50);
  131.         playTone(900, 50);
  132.       }
  133.     }
  134.   }
  135. }
  136.  
  137. void senseIR() {
  138.   int who[4];
  139.   int what[4];
  140.   int end;
  141.   if (pulseIn(sensorPin, LOW, 50) < startBit) {
  142.     digitalWrite(blinkPin, LOW);
  143.     ret[0] = -1;
  144.     return;
  145.   }
  146.   digitalWrite(blinkPin, HIGH);
  147.   who[0]   = pulseIn(sensorPin, LOW);
  148.   who[1]   = pulseIn(sensorPin, LOW);
  149.   who[2]   = pulseIn(sensorPin, LOW);
  150.   who[3]   = pulseIn(sensorPin, LOW);
  151.   what[0]  = pulseIn(sensorPin, LOW);
  152.   what[1]  = pulseIn(sensorPin, LOW);
  153.   what[2]  = pulseIn(sensorPin, LOW);
  154.   what[3]  = pulseIn(sensorPin, LOW);
  155.   end      = pulseIn(sensorPin, LOW);
  156.   if (end <= endBit) {
  157.     Serial.print(end);
  158.     Serial.println(" : bad end bit");
  159.     ret[0] = -1;
  160.     return;
  161.   }
  162.   Serial.println("---who---");
  163.   for(int i=0;i<=3;i++) {
  164.     Serial.println(who[i]);
  165.     if(who[i] > one) {
  166.       who[i] = 1;
  167.     } else if (who[i] > zero) {
  168.       who[i] = 0;
  169.     } else {
  170.       // Since the data is neither zero or one, we have an error
  171.       Serial.println("unknown player");
  172.       ret[0] = -1;
  173.       return;
  174.     }
  175.   }
  176.   ret[0]=convert(who);
  177.   Serial.println(ret[0]);
  178.  
  179.   Serial.println("---what---");
  180.   for(int i=0;i<=3;i++) {
  181.     Serial.println(what[i]);
  182.     if(what[i] > one) {
  183.       what[i] = 1;
  184.     } else if (what[i] > zero) {
  185.       what[i] = 0;
  186.     } else {
  187.       // Since the data is neither zero or one, we have an error
  188.       Serial.println("unknown action");
  189.       ret[0] = -1;
  190.       return;
  191.     }
  192.   }
  193.   ret[1]=convert(what);
  194.   Serial.println(ret[1]);
  195.   return;
  196. }
  197.  
  198. void playTone(int tone, int duration) {
  199.   for (long i = 0; i < duration * 1000L; i += tone * 2) {
  200.     digitalWrite(speakerPin, HIGH);
  201.     delayMicroseconds(tone);
  202.     digitalWrite(speakerPin, LOW);
  203.     delayMicroseconds(tone);
  204.   }
  205. }
  206.  
  207. int convert(int bits[]) {
  208.   int result = 0;
  209.   int seed   = 1;
  210.   for(int i=3;i>=0;i--) {
  211.     if(bits[i] == 1) {
  212.       result += seed;
  213.     }
  214.     seed = seed * 2;
  215.   }
  216.   return result;
  217. }
  218.  
  219. void senseFire() {
  220.   trigger = digitalRead(triggerPin);
  221.   if (trigger == LOW && fired == false) {
  222.     Serial.println("Button Pressed");
  223.     fired = true;
  224.     myShots++;
  225.     if (myHits <= maxHits && myShots > maxShots && random(1,20) <= myShots) {
  226.       Serial.println("SELF DESTRUCT");
  227.       selfDestruct();
  228.     } else if (myHits <= maxHits) {
  229.       Serial.print("Firing Shot : ");
  230.       Serial.println(myShots);
  231.       fireShot(myCode, myLevel);
  232.     }
  233.   } else if (trigger == HIGH) {
  234.     if (fired == true) {
  235.       Serial.println("Button Released");
  236.     }
  237.     // reset the fired variable
  238.     fired = false;
  239.   }
  240. }
  241.  
  242. void fireShot(int player, int level) {
  243.   int encoded[8];
  244.   digitalWrite(blinkPin, HIGH);
  245.   for (int i=0; i<4; i++) {
  246.     encoded[i] = player>>i & B1;   //encode data as '1' or '0'
  247.   }
  248.   for (int i=4; i<8; i++) {
  249.     encoded[i] = level>>i & B1;
  250.   }
  251.   // send startbit
  252.   oscillationWrite(senderPin, startBit);
  253.   // send separation bit
  254.   digitalWrite(senderPin, HIGH);
  255.   delayMicroseconds(waitTime);
  256.   // send the whole string of data
  257.   for (int i=7; i>=0; i--) {
  258.     if (encoded[i] == 0) {
  259.       oscillationWrite(senderPin, zero);
  260.     } else {
  261.       oscillationWrite(senderPin, one);
  262.     }
  263.     // send separation bit
  264.     digitalWrite(senderPin, HIGH);
  265.     delayMicroseconds(waitTime);
  266.   }
  267.   oscillationWrite(senderPin, endBit);
  268.   playTone(100, 5);
  269.   digitalWrite(blinkPin, LOW);
  270. }
  271.  
  272. void oscillationWrite(int pin, int time) {
  273.   for(int i = 0; i <= time/26; i++) {
  274.     digitalWrite(pin, HIGH);
  275.     delayMicroseconds(13);
  276.     digitalWrite(pin, LOW);
  277.     delayMicroseconds(13);
  278.   }
  279. }
  280.  
  281. void selfDestruct() {
  282.   myHits  = maxHits+1;
  283.   playTone(1000, 250);
  284.   playTone(750, 250);
  285.   playTone(500, 250);
  286.   playTone(250, 250);
  287. }
Advertisement
Add Comment
Please, Sign In to add comment