osman_cyp
Published © LGPL

Kronograf

A device that theoretically measures the speed of anything in meters per second.

AdvancedProtip304
Kronograf

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
General Purpose Dual Op-Amp
Texas Instruments General Purpose Dual Op-Amp
×1
Resistor 10k ohm
Resistor 10k ohm
×2
Through Hole Resistor, 470 ohm
Through Hole Resistor, 470 ohm
×2
5 mm LED: Red
5 mm LED: Red
×2
Multi-Turn Precision Potentiometer- 10k ohms (25 Turn)
Multi-Turn Precision Potentiometer- 10k ohms (25 Turn)
×2
Photomicrosensor, Slot Transmissive
Photomicrosensor, Slot Transmissive
×2

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Schematics

Kronograf

Code

TimeMeasurement

Arduino
//
//    FILE: timeMeasurement.ino
//  AUTHOR: Rob dot Tillaart at gmail dot com
// VERSION: 0.0.1
// PURPOSE: timeMeasurement Arduino UNO between 2 interrupts on pin 2 and 3 using timer1
//     URL: http://forum.arduino.cc/index.php?topic=473669
//
// HISTORY:
// 0.0.1 - 2017-04-28 initial version
//

volatile bool inprogress = false;
volatile bool dataReady = false;
volatile uint32_t ticks = 0;
volatile uint32_t overflows = 0;

const float ticksPerSecond = 16000000.0;
const float sensorDistance = 0.018;



void setup()
{
  Serial.begin(1000000);

  Serial.println(__FILE__);
  Serial.println();

  pinMode(2, INPUT);
  pinMode(3, INPUT);

  attachInterrupt(digitalPinToInterrupt(2), startTimer, RISING);
  attachInterrupt(digitalPinToInterrupt(3), stopTimer, RISING);
}

void loop()
{
  if (dataReady && inprogress)
  {
    // MATH
    float duration = ticks / ticksPerSecond;  // seconds
    float distance = sensorDistance;          // meter
    float speed1 = distance / duration;        // meter / second

    // OUTPUT
    Serial.print("time: ");
    Serial.print(ticks / 16.0, 2);    // 2 decimals
    Serial.print(" usec ");
    Serial.print("\tspeed: ");
    Serial.print(speed1, 3);           // 2 decimals
    Serial.println(" m/s");

    // PREPARE NEXT MEASUREMENT
    inprogress = false;
  }
}

ISR (TIMER1_OVF_vect)
{
  overflows++;
}

// note startTimer code itself takes
void startTimer()
{
  // prevent start interrupt when in progress
  if (inprogress) return;
  inprogress = true;
  dataReady = false;

  TCCR1A = 0;             // 
  TCCR1B = 0;             // stop
  TCNT1 = 0;              // reset counter, overflows at 65536
  overflows = 0;          // reset overflowCount
  OCR1A = 1;              // clock pulses per increment TCNT1
  TIMSK1 = bit(TOIE1);    // set overflow interrupt
  TCCR1B = bit(CS10);     // start
}

void stopTimer()
{
  if (dataReady) return;  // prevent additional stop interrupt
  dataReady = true;

  TCCR1A = 0;
  TCCR1B = 0;
  TIMSK1 = 0;
  ticks = overflows * 65536UL + TCNT1;    // calculate clockticks.
}

// ---END OF FILE ---

Credits

robTillaart

Posted by osman_cyp

Comments