Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Simple chronograph using break wires. The wires connect 5V to
- a digital input wired with a 10 kohm pulldown resistor.
- Additional portions of the circuit are outlined in comments
- above different sections. */
- // Calculating the velocity is a FLOP.
- float t;
- float v;
- int v2;
- void setup()
- {
- /* Breakwires are connected to pins 2 &3. And LED is wired
- from pins 9 and 10 to ground. AnalogWrite is used instead
- of using the approriate current limiting resistors */
- pinMode(2, INPUT);
- pinMode(3, INPUT);
- pinMode(9, OUTPUT);
- pinMode(11, OUTPUT);
- }
- // Simple function to flash the pin 9 (green) LED.
- void flash(int a)
- {
- // If flash(0) is called, the LED flashes on and off once.
- if(a<1)
- {
- analogWrite(9, 101);
- delay(100);
- analogWrite(9, 0);
- delay(100);
- }
- /* If flash(int i>0) is called, the LED flashes three times
- and pauses for half a second. */
- else
- {
- for(int z=0;z<3;z++)
- {
- flash(0);
- }
- delay(500);
- }
- }
- /* This function flashes the LED on pin 11 to communicate the
- recorded velocity. Here, int a is a power of 10 where a-1 is
- the maximum velocity the chronograph can record, and the first
- digit this function attempts to communicate. No flashes equals a
- zero. */
- void inflash(int a)
- {
- /* While i is less than the rounded down value of the result
- of the velocity as an integer taken modulo the next highest
- power of ten and divided by the current power of ten the
- function is trying to communicate, the LED will flash to
- communicate that digit. */
- for(int i=0;i<(floor((v2%a)/(a/10)));i++)
- {
- analogWrite(11, 101);
- delay(200);
- analogWrite(11, 0);
- delay(200);
- }
- }
- void loop()
- {
- // Wait for both wires to be connected. Flash pin 9.
- while(digitalRead(2)==LOW||digitalRead(3)==LOW)
- {
- flash(0);
- }
- // Wait for the first wire to break.
- while(digitalRead(2)==HIGH)
- {
- }
- /* Assign floating point number t the current time in micro-
- seconds since the sketch first started. */
- t=micros();
- // Wait for the second wire to break.
- while(digitalRead(3)==HIGH)
- {
- }
- /* Calculate the velocity, comparing the current time to t. A
- wire distance of 2 feet is used for this calculation. */
- v=(2/((micros()-t)/1000000));
- /* A jumper is included to change the unit of velocity
- communicated to the user. It uses a simple voltage divider
- with R2 = 10 kohm and R1 varying based on the pin jumpered.
- For the first case, it is 15 kohm, and the second, it is 6.8
- kohm. Some wiggle room is given for sloppy resistors. */
- if(analogRead(A5)<420&&analogRead(A5)>400)
- {
- // Give velocity in m/s if R1 = 15 kohm.
- v=v*0.3048;
- }
- if(analogRead(A5)<620&&analogRead(A5)>600)
- {
- // Give velocity in mi/hr if R2 = 6.8 kohm.
- v=v*0.6818;
- }
- /* We need to convert the floating point number v to an
- integer v2 for communication. */
- v2=(int)round(v);
- /* Here, i's initial value will be the maximum value the
- chronograph can communicate plus one. So this chronograph
- can read up to 9999 velocity units. */
- for(int i=10000;i>=10;i=(i/10))
- {
- /* Starting from the highest power of ten, the function
- inflash(i) (where i is the next highest power of ten
- being worked on) will flash the digit being
- communicated on pin 11. */
- inflash(i);
- // Flash for clarity.
- flash(1);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement