3D printed Arduino Airsoft Chronograph
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

OpenChrono.ino 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. * OpenChrono.ino
  3. *
  4. * OpenChrono BB speed measurement device.
  5. *
  6. * Copyright (c) 2022 Thomas Buck <thomas@xythobuz.de>
  7. *
  8. * OpenChrono is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * OpenChrono is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with OpenChrono. If not, see <https://www.gnu.org/licenses/>.
  20. *
  21. * The goal is to measure the speed of a BB moving through the device.
  22. */
  23. #include "timing.h"
  24. #include "ticks.h"
  25. #include "lcd.h"
  26. #include "config.h"
  27. static void calculate(uint16_t a, uint16_t b) {
  28. uint16_t ticks = 0;
  29. if (b >= a) {
  30. // simple case - just return difference
  31. ticks = b - a;
  32. } else {
  33. // the timer overflowed between measurements!
  34. int32_t tmp = ((int32_t)b) - ((int32_t)a);
  35. tmp += 0x10000;
  36. ticks = (uint16_t)tmp;
  37. }
  38. double speed = tick_to_metric(ticks);
  39. if ((speed >= MIN_SPEED) && (speed <= MAX_SPEED)) {
  40. // only register realistic velocities
  41. tick_new_value(ticks);
  42. lcd_new_value();
  43. }
  44. }
  45. static void measure() {
  46. cli(); // disable interrupts before interacting with values
  47. // reset interrupts
  48. trigger_a = 0;
  49. trigger_b = 0;
  50. uint16_t a = time_a, b = time_b;
  51. sei(); // enable interrupts immediately afterwards
  52. calculate(a, b);
  53. }
  54. void setup() {
  55. // we simply turn on the IR LEDs all the time
  56. pinMode(IR_LED_PIN, OUTPUT);
  57. digitalWrite(IR_LED_PIN, HIGH);
  58. // but the UV LEDs will only be pulsed on firing!
  59. pinMode(UV_LED_PIN, OUTPUT);
  60. digitalWrite(UV_LED_PIN, LOW);
  61. lcd_init();
  62. delay(SCREEN_TIMEOUT); // show splash screen
  63. timer_init();
  64. interrupt_init();
  65. }
  66. void loop() {
  67. if (trigger_b) {
  68. if (trigger_a) {
  69. // we got an event on both inputs
  70. measure();
  71. } else {
  72. // we got a false second trigger!
  73. // clear so next calculation will be correct
  74. trigger_b = 0;
  75. }
  76. }
  77. lcd_loop();
  78. }