3D printed Arduino Airsoft Chronograph
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

OpenChrono.ino 1.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /*
  2. * OpenChrono.ino
  3. *
  4. * OpenChrono BB speed measurement device.
  5. *
  6. * Copyright (c) 2022 Thomas Buck <thomas@xythobuz.de>
  7. *
  8. * The goal is to measure the speed of a BB moving through the device.
  9. */
  10. #include "timing.h"
  11. #include "ticks.h"
  12. #include "lcd.h"
  13. #include "config.h"
  14. static void calculate(uint16_t a, uint16_t b) {
  15. uint16_t ticks = 0;
  16. if (b >= a) {
  17. // simple case - just return difference
  18. ticks = b - a;
  19. } else {
  20. // the timer overflowed between measurements!
  21. int32_t tmp = ((int32_t)b) - ((int32_t)a);
  22. tmp += 0x10000;
  23. ticks = (uint16_t)tmp;
  24. }
  25. double speed = tick_to_metric(ticks);
  26. if ((speed >= MIN_SPEED) && (speed <= MAX_SPEED)) {
  27. // only register realistic velocities
  28. tick_new_value(ticks);
  29. lcd_new_value();
  30. }
  31. }
  32. static void measure() {
  33. cli(); // disable interrupts before interacting with values
  34. // reset interrupts
  35. trigger_a = 0;
  36. trigger_b = 0;
  37. uint16_t a = time_a, b = time_b;
  38. sei(); // enable interrupts immediately afterwards
  39. calculate(a, b);
  40. }
  41. void setup() {
  42. // we simply turn on the IR LEDs all the time
  43. pinMode(IR_LED_PIN, OUTPUT);
  44. digitalWrite(IR_LED_PIN, HIGH);
  45. // but the UV LEDs will only be pulsed on firing!
  46. pinMode(UV_LED_PIN, OUTPUT);
  47. digitalWrite(UV_LED_PIN, LOW);
  48. lcd_init();
  49. delay(SCREEN_TIMEOUT); // show splash screen
  50. timer_init();
  51. interrupt_init();
  52. }
  53. void loop() {
  54. if (trigger_b) {
  55. if (trigger_a) {
  56. // we got an event on both inputs
  57. measure();
  58. } else {
  59. // we got a false second trigger!
  60. // clear so next calculation will be correct
  61. trigger_b = 0;
  62. }
  63. }
  64. lcd_loop();
  65. }