Advertisement
Guest User

Untitled

a guest
Jul 14th, 2013
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.81 KB | None | 0 0
  1. /* Simple chronograph using break wires. The wires connect 5V to
  2. a digital input wired with a 10 kohm pulldown resistor.
  3. Additional portions of the circuit are outlined in comments
  4. above different sections. */
  5.  
  6. // Calculating the velocity is a FLOP.
  7. float t;
  8. float v;
  9. int v2;
  10.  
  11. void setup()
  12. {
  13.  
  14. /* Breakwires are connected to pins 2 &3. And LED is wired
  15. from pins 9 and 10 to ground. AnalogWrite is used instead
  16. of using the approriate current limiting resistors */
  17. pinMode(2, INPUT);
  18. pinMode(3, INPUT);
  19. pinMode(9, OUTPUT);
  20. pinMode(11, OUTPUT);
  21. }
  22.  
  23. // Simple function to flash the pin 9 (green) LED.
  24. void flash(int a)
  25. {
  26.  
  27. // If flash(0) is called, the LED flashes on and off once.
  28. if(a<1)
  29. {
  30. analogWrite(9, 101);
  31. delay(100);
  32. analogWrite(9, 0);
  33. delay(100);
  34. }
  35.  
  36. /* If flash(int i>0) is called, the LED flashes three times
  37. and pauses for half a second. */
  38. else
  39. {
  40. for(int z=0;z<3;z++)
  41. {
  42. flash(0);
  43. }
  44. delay(500);
  45. }
  46. }
  47.  
  48. /* This function flashes the LED on pin 11 to communicate the
  49. recorded velocity. Here, int a is a power of 10 where a-1 is
  50. the maximum velocity the chronograph can record, and the first
  51. digit this function attempts to communicate. No flashes equals a
  52. zero. */
  53. void inflash(int a)
  54. {
  55.  
  56. /* While i is less than the rounded down value of the result
  57. of the velocity as an integer taken modulo the next highest
  58. power of ten and divided by the current power of ten the
  59. function is trying to communicate, the LED will flash to
  60. communicate that digit. */
  61. for(int i=0;i<(floor((v2%a)/(a/10)));i++)
  62. {
  63. analogWrite(11, 101);
  64. delay(200);
  65. analogWrite(11, 0);
  66. delay(200);
  67. }
  68. }
  69. void loop()
  70. {
  71.  
  72. // Wait for both wires to be connected. Flash pin 9.
  73. while(digitalRead(2)==LOW||digitalRead(3)==LOW)
  74. {
  75. flash(0);
  76. }
  77.  
  78. // Wait for the first wire to break.
  79. while(digitalRead(2)==HIGH)
  80. {
  81. }
  82.  
  83. /* Assign floating point number t the current time in micro-
  84. seconds since the sketch first started. */
  85. t=micros();
  86.  
  87. // Wait for the second wire to break.
  88. while(digitalRead(3)==HIGH)
  89. {
  90. }
  91.  
  92. /* Calculate the velocity, comparing the current time to t. A
  93. wire distance of 2 feet is used for this calculation. */
  94. v=(2/((micros()-t)/1000000));
  95.  
  96. /* A jumper is included to change the unit of velocity
  97. communicated to the user. It uses a simple voltage divider
  98. with R2 = 10 kohm and R1 varying based on the pin jumpered.
  99. For the first case, it is 15 kohm, and the second, it is 6.8
  100. kohm. Some wiggle room is given for sloppy resistors. */
  101. if(analogRead(A5)<420&&analogRead(A5)>400)
  102. {
  103.  
  104. // Give velocity in m/s if R1 = 15 kohm.
  105. v=v*0.3048;
  106. }
  107. if(analogRead(A5)<620&&analogRead(A5)>600)
  108. {
  109.  
  110. // Give velocity in mi/hr if R2 = 6.8 kohm.
  111. v=v*0.6818;
  112. }
  113.  
  114. /* We need to convert the floating point number v to an
  115. integer v2 for communication. */
  116. v2=(int)round(v);
  117.  
  118. /* Here, i's initial value will be the maximum value the
  119. chronograph can communicate plus one. So this chronograph
  120. can read up to 9999 velocity units. */
  121. for(int i=10000;i>=10;i=(i/10))
  122. {
  123.  
  124. /* Starting from the highest power of ten, the function
  125. inflash(i) (where i is the next highest power of ten
  126. being worked on) will flash the digit being
  127. communicated on pin 11. */
  128. inflash(i);
  129.  
  130. // Flash for clarity.
  131. flash(1);
  132. }
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement