Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- Vibration tracking target trainer
- 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
- Each collection will be on a bounded random timer. Light the red LED, wait a set amount of time and turn green again.
- If there is no shot in the set amount of time, set off an alert that you messed a target.
- */
- //System variables section
- int entropy_pin = A0; //This is used to make the random number actually random. Probably not needed here but good practice
- unsigned long time_of_last_change; //Might not be needed here;
- //User variables section
- int wait_time = 2; //This is the amount of time you have to hit the target measured in seconds
- int flash_light_time = 6; //Amount of time to flash the lights on a booboo
- short int number_of_targets = 1; //How many targets you got, max 4.
- int min_wait = 1; //These are the minimum and max amount of time in seconds it will wait to go from green to red.
- int max_wait = 10;
- unsigned long debounce = 250; //Debounce timeout - 250 (quarter second) is plenty for BBs I think, 500 for playing whack-a-mole
- bool analog = false;
- //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.
- int red_light = 9;
- int green_light = 10;
- int blue_light = 11;
- int vibration_pin_digital = 2;
- //int vibration_pin_analog[] = {A0, A1, A2, A3};
- class TargetCollection
- {
- public:
- int green_pin;
- int red_pin;
- int blue_pin;
- int vibration_pin;
- bool shootable = false;
- bool need_flash = false;
- int flash_speed = 100;
- bool green_on = true;
- bool two_targets = false;
- unsigned long time_flashing_done;
- unsigned long time_flash_start;
- unsigned long time_target_on;
- unsigned long time_target_hit;
- // unsigned long time_of_last_change;
- unsigned long time_of_next_flip;
- unsigned long debounce_time;
- TargetCollection()
- {
- //Nothing to see here.
- }
- void lightFlip()
- {
- if (green_on)
- {
- digitalWrite(green_pin, HIGH);
- digitalWrite(red_pin, LOW);
- green_on = false;
- }
- else
- {
- digitalWrite(green_pin, LOW);
- digitalWrite(red_pin, HIGH);
- green_on = true;
- }
- }
- //Rename this for whatever... It's the "you messed up" function.
- void flashLights()
- {
- if (millis() > time_flash_start && millis() < time_flashing_done)
- {
- time_flash_start = millis()+ flash_speed;
- lightFlip();
- }
- if (millis() > time_flashing_done)
- {
- need_flash = false;
- shootable = false;
- digitalWrite(green_pin, LOW);
- digitalWrite(red_pin, LOW);
- time_of_next_flip = time_of_next_flip + (random(min_wait, max_wait) * 1000);
- }
- }
- void setup(int red_pin_in, int green_pin_in, int blue_pin_in, int vibration_pin_in)
- {
- green_pin = green_pin_in;
- red_pin = red_pin_in;
- blue_pin = blue_pin_in;
- vibration_pin = vibration_pin_in;
- time_of_last_change = millis();
- pinMode(green_pin, OUTPUT);
- pinMode(red_pin, OUTPUT);
- if (analog)
- pinMode(vibration_pin, INPUT);
- else
- pinMode(vibration_pin, INPUT_PULLUP);
- digitalWrite(green_pin, HIGH);
- digitalWrite(vibration_pin, HIGH);
- randomSeed(analogRead(entropy_pin));
- }
- void loop()
- {
- /*if (millis() > time_target_hit + 1000 && !shootable)
- {
- digitalWrite(green_pin, LOW);
- }*/
- if (need_flash)
- {
- flashLights();
- }
- else if (millis() > time_of_next_flip && !shootable)
- {
- Serial.print("SHOOT ME!!! time: ");
- Serial.println(millis());
- two_targets = random(0,2);
- Serial.print("two targets: ");
- Serial.println(two_targets);
- if (two_targets)
- {
- digitalWrite(blue_pin, HIGH);
- digitalWrite(green_pin, LOW);
- digitalWrite(red_pin, LOW);
- }
- else
- {
- digitalWrite(green_pin, LOW);
- digitalWrite(red_pin, HIGH);
- digitalWrite(blue_pin, LOW);
- }
- shootable = true;
- time_target_on = millis();
- }
- else if (!shootable && ( (!analog && digitalRead(vibration_pin) == LOW) || (analog && analogRead(vibration_pin) > 100) ) && millis() > debounce_time)
- {
- //Some type of alarm would be awesome.
- Serial.println("You didn't wait!");
- time_flashing_done = millis() + (flash_light_time * 1000);
- time_flash_start = millis();
- need_flash = true;
- flash_speed = 100;
- flashLights();
- }
- else if (shootable && (time_target_on+(wait_time*1000)) > millis() && millis() > debounce_time)
- {
- if (((!analog && digitalRead(vibration_pin) == LOW ) || (analog && analogRead(vibration_pin) > 100)) ) // Target got hit in time.
- {
- Serial.println("We've got a hit!!!");
- if (two_targets)
- {
- digitalWrite(red_pin, HIGH);
- digitalWrite(green_pin, LOW);
- digitalWrite(blue_pin, LOW);
- two_targets = 0;
- }
- else
- {
- digitalWrite(blue_pin, LOW);
- digitalWrite(red_pin, LOW);
- digitalWrite(green_pin, HIGH);
- time_target_hit = millis();
- shootable = false;
- }
- time_of_next_flip = (random(min_wait, max_wait) * 1000)+millis();
- debounce_time = millis() + debounce;
- }
- }
- else if (shootable && (time_target_on+(wait_time*1000)) < millis())
- {
- //You done mussed up!!! Set off the alarm. For now. Blink lights.
- Serial.println("Too slow!!");
- time_flashing_done = millis() + (flash_light_time * 1000);
- time_flash_start = millis();
- need_flash = true;
- flash_speed = 500;
- flashLights();
- shootable = false;
- time_of_next_flip = (random(min_wait, max_wait) * 1000) + millis();
- }
- }
- };
- TargetCollection target1;
- TargetCollection target2;
- TargetCollection target3;
- TargetCollection target4;
- TargetCollection target_list[] = {target1, target2, target3, target4};
- void setup() {
- Serial.begin(9600); //Serial monitor connection for debugging.
- randomSeed(analogRead(entropy_pin));
- /*if (analog)
- {
- for (int i = 0; i < number_of_targets; i++)
- {
- target_list[i].setup(red_light[i], green_light[i], vibration_pin_analog[i]);
- }
- }
- else
- {
- for (int i = 0; i < number_of_targets; i++)
- {
- target_list[i].setup(red_light[i], green_light[i], vibration_pin_digital[i]);
- }
- }
- */
- target1.setup(red_light, green_light, blue_light, vibration_pin_digital);
- }
- void loop() {
- /*
- for (int i=0; i < number_of_targets; i++)
- {
- target_list[i].loop();
- }
- */
- target1.loop();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement