Advertisement
Guest User

vibration target v3

a guest
Apr 4th, 2020
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.18 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 = 1;  //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 = 250;     //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 = 9;
  32. int green_light = 10;
  33. int blue_light = 11;
  34. int vibration_pin_digital = 2;
  35. //int vibration_pin_analog[] = {A0, A1, A2, A3};
  36.  
  37.  
  38.  
  39. class TargetCollection
  40. {
  41.   public:
  42.  
  43.     int green_pin;
  44.     int red_pin;
  45.     int blue_pin;
  46.     int vibration_pin;
  47.     bool shootable = false;
  48.     bool need_flash = false;
  49.     int flash_speed = 100;
  50.     bool green_on = true;
  51.     bool two_targets = false;
  52.     unsigned long time_flashing_done;
  53.     unsigned long time_flash_start;
  54.     unsigned long time_target_on;
  55.     unsigned long time_target_hit;
  56. //    unsigned long time_of_last_change;
  57.     unsigned long time_of_next_flip;
  58.     unsigned long debounce_time;
  59.  
  60.     TargetCollection()
  61.     {
  62.       //Nothing to see here.
  63.     }
  64.  
  65.     void lightFlip()
  66.     {
  67.       if (green_on)
  68.       {
  69.         digitalWrite(green_pin, HIGH);
  70.         digitalWrite(red_pin, LOW);
  71.         green_on = false;
  72.       }
  73.       else
  74.       {
  75.         digitalWrite(green_pin, LOW);
  76.         digitalWrite(red_pin, HIGH);
  77.         green_on = true;
  78.       }
  79.      
  80.     }
  81.     //Rename this for whatever... It's the "you messed up" function.
  82.     void flashLights()
  83.     {
  84.       if (millis() > time_flash_start && millis() < time_flashing_done)
  85.       {
  86.         time_flash_start = millis()+ flash_speed;
  87.         lightFlip();
  88.       }
  89.      
  90.       if (millis() > time_flashing_done)
  91.       {
  92.         need_flash = false;
  93.         shootable = false;
  94.         digitalWrite(green_pin, LOW);
  95.         digitalWrite(red_pin, LOW);
  96.         time_of_next_flip = time_of_next_flip + (random(min_wait, max_wait) * 1000);
  97.       }
  98.  
  99.      
  100.      
  101.      
  102.     }
  103.     void setup(int red_pin_in, int green_pin_in, int blue_pin_in, int vibration_pin_in)
  104.     {
  105.       green_pin = green_pin_in;
  106.       red_pin = red_pin_in;
  107.       blue_pin = blue_pin_in;
  108.       vibration_pin = vibration_pin_in;
  109.       time_of_last_change = millis();
  110.  
  111.       pinMode(green_pin, OUTPUT);
  112.       pinMode(red_pin, OUTPUT);
  113.       if (analog)
  114.         pinMode(vibration_pin, INPUT);
  115.       else
  116.         pinMode(vibration_pin, INPUT_PULLUP);
  117.      
  118.       digitalWrite(green_pin, HIGH);
  119.       digitalWrite(vibration_pin, HIGH);
  120.       randomSeed(analogRead(entropy_pin));
  121.  
  122.     }
  123.  
  124.     void loop()
  125.     {
  126.      
  127.       /*if (millis() > time_target_hit + 1000 && !shootable)
  128.       {
  129.         digitalWrite(green_pin, LOW);
  130.       }*/
  131.      
  132.       if (need_flash)
  133.       {
  134.         flashLights();
  135.       }
  136.      
  137.       else if (millis() > time_of_next_flip && !shootable)
  138.       {
  139.         Serial.print("SHOOT ME!!! time: ");
  140.         Serial.println(millis());
  141.         two_targets = random(0,2);
  142.         Serial.print("two targets: ");
  143.         Serial.println(two_targets);
  144.         if (two_targets)
  145.         {
  146.           digitalWrite(blue_pin, HIGH);
  147.           digitalWrite(green_pin, LOW);
  148.           digitalWrite(red_pin, LOW);
  149.         }
  150.         else
  151.         {
  152.           digitalWrite(green_pin, LOW);
  153.           digitalWrite(red_pin, HIGH);
  154.           digitalWrite(blue_pin, LOW);
  155.         }
  156.         shootable = true;    
  157.         time_target_on = millis();
  158.        
  159.       }
  160.       else if (!shootable && ( (!analog  && digitalRead(vibration_pin) == LOW) || (analog && analogRead(vibration_pin) > 100) ) && millis() > debounce_time)
  161.       {
  162.         //Some type of alarm would be awesome.
  163.         Serial.println("You didn't wait!");
  164.         time_flashing_done = millis() + (flash_light_time * 1000);
  165.         time_flash_start = millis();
  166.         need_flash = true;
  167.         flash_speed = 100;
  168.         flashLights();
  169.       }
  170.       else if (shootable && (time_target_on+(wait_time*1000)) > millis() && millis() > debounce_time)
  171.       {      
  172.         if (((!analog && digitalRead(vibration_pin) == LOW ) || (analog && analogRead(vibration_pin) > 100)) ) // Target got hit in time.
  173.         {
  174.           Serial.println("We've got a hit!!!");
  175.           if (two_targets)
  176.           {
  177.             digitalWrite(red_pin, HIGH);
  178.             digitalWrite(green_pin, LOW);
  179.             digitalWrite(blue_pin, LOW);
  180.             two_targets = 0;
  181.           }
  182.           else
  183.           {
  184.             digitalWrite(blue_pin, LOW);
  185.             digitalWrite(red_pin, LOW);
  186.             digitalWrite(green_pin, HIGH);
  187.             time_target_hit = millis();
  188.             shootable = false;
  189.           }
  190.           time_of_next_flip = (random(min_wait, max_wait) * 1000)+millis();
  191.           debounce_time = millis() + debounce;
  192.         }
  193.       }
  194.      
  195.       else if (shootable && (time_target_on+(wait_time*1000)) < millis())
  196.       {
  197.         //You done mussed up!!! Set off the alarm. For now. Blink lights.
  198.         Serial.println("Too slow!!");
  199.         time_flashing_done = millis() + (flash_light_time * 1000);
  200.         time_flash_start = millis();
  201.         need_flash = true;
  202.         flash_speed = 500;
  203.         flashLights();
  204.         shootable = false;
  205.         time_of_next_flip = (random(min_wait, max_wait) * 1000) + millis();
  206.       }
  207.      
  208.     }
  209. };
  210.  
  211.  
  212. TargetCollection target1;
  213. TargetCollection target2;
  214. TargetCollection target3;
  215. TargetCollection target4;
  216.  
  217. TargetCollection target_list[] = {target1, target2, target3, target4};
  218.  
  219.  
  220.  
  221.  
  222. void setup() {
  223.   Serial.begin(9600);  //Serial monitor connection for debugging.
  224.  
  225.   randomSeed(analogRead(entropy_pin));
  226.  
  227.   /*if (analog)
  228.   {
  229.  
  230.     for (int i = 0; i < number_of_targets; i++)
  231.     {
  232.       target_list[i].setup(red_light[i], green_light[i], vibration_pin_analog[i]);
  233.     }
  234.   }
  235.   else
  236.   {
  237.     for (int i = 0; i < number_of_targets; i++)
  238.     {
  239.       target_list[i].setup(red_light[i], green_light[i], vibration_pin_digital[i]);
  240.     }
  241.   }
  242.   */
  243.   target1.setup(red_light, green_light, blue_light, vibration_pin_digital);
  244. }
  245.  
  246. void loop() {
  247.  
  248.   /*
  249.   for (int i=0; i < number_of_targets; i++)
  250.   {
  251.     target_list[i].loop();
  252.   }
  253.   */
  254.   target1.loop();
  255.  
  256.  
  257.  
  258. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement