Advertisement
Guest User

multi-target with double tap.

a guest
Apr 5th, 2020
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.25 KB | None | 0 0
  1. /**
  2.    Vibration tracking target trainer
  3.  
  4.    Device to track up to 4 vibration sensors. Each sensor will have a two LEDs attached to it. Red and greed. This is a collection
  5.  
  6.    Each collection will be on a bounded random timer. Light the red LED, wait a set amount of time and turn green again.
  7.    If there is no shot in the set amount of time, set off an alert that you messed a target.
  8.  
  9. */
  10.  
  11.  
  12.  
  13. //System variables section
  14. int entropy_pin = A0;              //This is used to make the random number actually random. Probably not needed here but good practice
  15. unsigned long time_of_last_change; //Might not be needed here;
  16.  
  17.  
  18.  
  19.  
  20. //User variables section
  21. int wait_time = 2;                //This is the amount of time you have to hit the target measured in seconds
  22. int flash_light_time = 6;         //Amount of time to flash the lights on a booboo
  23. short int number_of_targets = 2;  //How many targets you got, max 4.
  24. int min_wait = 1;                 //These are the minimum and max amount of time in seconds it will wait to go from green to red.
  25. int max_wait = 10;
  26. unsigned long debounce = 150;     //Debounce timeout - 250 (quarter second) is plenty for BBs I think, 500 for playing whack-a-mole
  27. bool analog = false;
  28.  
  29.  
  30. //It is important that these variables are in sets. First element of each should all be pins of one target, second elements for second target, etc.
  31. int red_light[] =     {2, 5, 8, 11};
  32. int green_light[] =   {3, 6, 9, 12};
  33. int blue_light[] =    {4, 7, 10, 13};
  34. int vibration_pin_digital[] = {A0, A1, A2, A3};
  35. int vibration_pin_analog[] = {};
  36.  
  37.  
  38.  
  39.  
  40. class TargetCollection
  41. {
  42.   public:
  43.  
  44.     int green_pin;
  45.     int red_pin;
  46.     int blue_pin;
  47.     int vibration_pin;
  48.     bool shootable = false;
  49.     bool need_flash = false;
  50.     bool two_targets = false;
  51.     int flash_speed = 100;
  52.     bool green_on = true;
  53.     unsigned long time_flashing_done;
  54.     unsigned long time_flash_start;
  55.     unsigned long time_target_on;
  56.     unsigned long time_target_hit;
  57. //    unsigned long time_of_last_change;
  58.     unsigned long time_of_next_flip;
  59.     unsigned long debounce_time;
  60.  
  61.     TargetCollection()
  62.     {
  63.       //Nothing to see here.
  64.     }
  65.  
  66.     void lightFlip()
  67.     {
  68.       if (green_on)
  69.       {
  70.         digitalWrite(blue_pin, LOW);
  71.         digitalWrite(green_pin, HIGH);
  72.         digitalWrite(red_pin, LOW);
  73.         green_on = false;
  74.       }
  75.       else
  76.       {
  77.         digitalWrite(blue_pin, LOW);
  78.         digitalWrite(green_pin, LOW);
  79.         digitalWrite(red_pin, HIGH);
  80.         green_on = true;
  81.       }
  82.      
  83.     }
  84.     //Rename this for whatever... It's the "you messed up" function.
  85.     void flashLights()
  86.     {
  87.       if (millis() > time_flash_start && millis() < time_flashing_done)
  88.       {
  89.         time_flash_start = millis()+ flash_speed;
  90.         lightFlip();
  91.       }
  92.      
  93.       if (millis() > time_flashing_done)
  94.       {
  95.         need_flash = false;
  96.         shootable = false;
  97.         digitalWrite(green_pin, LOW);
  98.         digitalWrite(red_pin, LOW);
  99.         digitalWrite(blue_pin, LOW);
  100.         time_of_next_flip = time_of_next_flip + (random(min_wait, max_wait) * 1000);
  101.       }
  102.  
  103.      
  104.      
  105.      
  106.     }
  107.     void setup(int red_pin_in, int green_pin_in, int blue_pin_in, int vibration_pin_in)
  108.     {
  109.       green_pin = green_pin_in;
  110.       red_pin = red_pin_in;
  111.       blue_pin = blue_pin_in;
  112.       vibration_pin = vibration_pin_in;
  113.       time_of_last_change = millis();
  114.  
  115.       pinMode(green_pin, OUTPUT);
  116.       pinMode(red_pin, OUTPUT);
  117.       pinMode(blue_pin, OUTPUT);
  118.       if (analog)
  119.         pinMode(vibration_pin, INPUT);
  120.       else
  121.         pinMode(vibration_pin, INPUT_PULLUP);
  122.      
  123.       digitalWrite(green_pin, HIGH);
  124.       digitalWrite(vibration_pin, HIGH);
  125.  
  126.     }
  127.  
  128.     void loop()
  129.     {
  130.      
  131.       /*if (millis() > time_target_hit + 1000 && !shootable)
  132.       {
  133.         digitalWrite(green_pin, LOW);
  134.       }*/
  135.      
  136.       if (need_flash)
  137.       {
  138.         flashLights();
  139.       }
  140.      
  141.       else if (millis() > time_of_next_flip && !shootable)
  142.       {
  143.         Serial.print("SHOOT ME!!! time: ");
  144.         Serial.println(millis());
  145.         two_targets = random(0,2);
  146.         Serial.print("two targets: ");
  147.         Serial.println(two_targets);
  148.         if (two_targets)
  149.         {
  150.           digitalWrite(blue_pin, HIGH);
  151.           digitalWrite(green_pin, LOW);
  152.           digitalWrite(red_pin, LOW);
  153.         }
  154.         else
  155.         {
  156.           digitalWrite(green_pin, LOW);
  157.           digitalWrite(red_pin, HIGH);
  158.           digitalWrite(blue_pin, LOW);
  159.         }
  160.         shootable = true;    
  161.         time_target_on = millis();
  162.        
  163.       }
  164.       else if (!shootable && ( (!analog  && digitalRead(vibration_pin) == LOW) || (analog && analogRead(vibration_pin) > 100) ) && millis() > debounce_time)
  165.       {
  166.         //Some type of alarm would be awesome.
  167.         Serial.println("You didn't wait!");
  168.         time_flashing_done = millis() + (flash_light_time * 1000);
  169.         time_flash_start = millis();
  170.         need_flash = true;
  171.         flash_speed = 100;
  172.         flashLights();
  173.       }
  174.       else if (shootable && (time_target_on+(wait_time*1000)) > millis() && millis() > debounce_time)
  175.       {      
  176.         if (((!analog && digitalRead(vibration_pin) == LOW ) || (analog && analogRead(vibration_pin) > 100)) ) // Target got hit in time.
  177.         {
  178.           Serial.println("We've got a hit!!!");
  179.           if (two_targets)
  180.           {
  181.             digitalWrite(red_pin, HIGH);
  182.             digitalWrite(green_pin, LOW);
  183.             digitalWrite(blue_pin, LOW);
  184.             two_targets = 0;
  185.           }
  186.           else
  187.           {
  188.             digitalWrite(blue_pin,LOW);
  189.             digitalWrite(red_pin, LOW);
  190.             digitalWrite(green_pin, LOW);
  191.             time_target_hit = millis();
  192.             shootable = false;
  193.           }
  194.           time_of_next_flip = (random(min_wait, max_wait) * 1000)+millis();
  195.           debounce_time = millis() + debounce;
  196.         }
  197.       }
  198.      
  199.       else if (shootable && (time_target_on+(wait_time*1000)) < millis())
  200.       {
  201.         //You done mussed up!!! Set off the alarm. For now. Blink lights.
  202.         Serial.println("Too slow!!");
  203.         time_flashing_done = millis() + (flash_light_time * 1000);
  204.         time_flash_start = millis();
  205.         need_flash = true;
  206.         flash_speed = 500;
  207.         flashLights();
  208.         shootable = false;
  209.         time_of_next_flip = (random(min_wait, max_wait) * 1000) + millis();
  210.       }
  211.      
  212.     }
  213. };
  214.  
  215.  
  216. TargetCollection target1;
  217. TargetCollection target2;
  218. TargetCollection target3;
  219. TargetCollection target4;
  220.  
  221. TargetCollection target_list[] = {target1, target2, target3, target4};
  222.  
  223.  
  224.  
  225.  
  226. void setup() {
  227.   Serial.begin(9600);  //Serial monitor connection for debugging.
  228.  
  229.   randomSeed(analogRead(entropy_pin));
  230.  
  231.   if (analog)
  232.   {
  233.  
  234.     for (int i = 0; i < number_of_targets; i++)
  235.     {
  236.       target_list[i].setup(red_light[i], green_light[i], blue_light[i], vibration_pin_analog[i]);
  237.     }
  238.   }
  239.   else
  240.   {
  241.     for (int i = 0; i < number_of_targets; i++)
  242.     {
  243.       target_list[i].setup(red_light[i], green_light[i], blue_light[i], vibration_pin_digital[i]);
  244.     }
  245.   }
  246. }
  247.  
  248. void loop() {
  249.  
  250.   for (int i=0; i < number_of_targets; i++)
  251.   {
  252.     target_list[i].loop();
  253.   }
  254.  
  255.  
  256.  
  257. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement