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.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. tick_new_value(ticks);
  26. lcd_new_value();
  27. }
  28. static void measure() {
  29. cli(); // disable interrupts before interacting with values
  30. // reset interrupts
  31. trigger_a = 0;
  32. trigger_b = 0;
  33. uint16_t a = time_a, b = time_b;
  34. sei(); // enable interrupts immediately afterwards
  35. calculate(a, b);
  36. }
  37. void setup() {
  38. // we simply turn on the IR LEDs all the time
  39. pinMode(IR_LED_PIN, OUTPUT);
  40. digitalWrite(IR_LED_PIN, HIGH);
  41. // but the UV LEDs will only be pulsed on firing!
  42. pinMode(UV_LED_PIN, OUTPUT);
  43. digitalWrite(UV_LED_PIN, LOW);
  44. lcd_init();
  45. delay(SCREEN_TIMEOUT); // show splash screen
  46. timer_init();
  47. interrupt_init();
  48. }
  49. void loop() {
  50. if (trigger_b) {
  51. if (trigger_a) {
  52. // we got an event on both inputs
  53. measure();
  54. } else {
  55. // we got a false second trigger!
  56. // clear so next calculation will be correct
  57. trigger_b = 0;
  58. }
  59. }
  60. lcd_loop();
  61. }