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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. uint32_t tmp = ((uint32_t)b) - ((uint32_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. lcd_init();
  39. delay(SCREEN_TIMEOUT); // show splash screen
  40. timer_init();
  41. interrupt_init();
  42. }
  43. void loop() {
  44. if ((time_a == 1) && (time_b == 1)) {
  45. // we got an event on both inputs
  46. measure();
  47. }
  48. lcd_loop();
  49. }