<!-- Contactless acoustic stopwatch from standard of phyphox.
     Project: to create specialized stopwatch for Millikan experiment.
     mIv Jan 2020, also comments -->
<phyphox version="1.7" locale="en"> 
    <title>Acoustic Millikan Stopwatch</title>
    <category>Timers</category>
    <description>
        Millikan experiment with phyphox acoustic stopwatch:

	Millikan experiment was the first clear demonstration of the electric charge quantification and the determination of the elementary charge (charge of electron). Radius and charge of the oil drop in this experiment is determined from the two times: time 1 of the drop free fall over h = 1 mm and time 2 of its rise over the same height h under vertical electric field U/d, where U is electric tension applied between two metallic plates at distance d. The equations are given in the attached document.

        This version of the phyphox acoustic stopwatch as its prototype measures every time between two loud acoustic signals. These can be clicks, beeps, claps etc. as long as they are louder than the environment. You might want to adjust the threshold, giving the level at which the stop watch is triggered (ranging from 0 to 1). 

	After starting the experiment, the clock will start measurement of time 1 on the first click exceeding the threshold and will be stopped on the second click. The third click will start measurement of time 2 and stop it on the fourth click. Two more clicks save the measured times and calculated radius and charge, and return the stopwatch to the original state. Make sure that the click is short enough as a long sound might be immediately detected as a stop.
    </description>
    <icon>Mil</icon>
    <color>green</color>
  <data-containers>
    <container>threshold</container> <!-- sound threshold to START/STOP -->
    <container>mindelay</container> <!-- max sound duration -->
    <container size="16384">recording</container> <!-- 16384 ??? -->
    <container>rate</container>
    <container>i</container>
    <container>i2</container>
    <container>t</container>
    <container>t2</container>
    <container>limit</container>
    <container>max</container>
    <container>last</container>
    <container size="0">tlist</container>
    <container size="0">tlist1</container>
    <container size="6">ones</container>
    <container size="6">tail</container>
    <container size="0">dtlist</container>
    <container size="0">tindex</container>
    <container size="1">tcount</container>
    <container size="1">tcount-1</container>

    <container>t0</container> <!-- size = 1 by default -->
    <container>t1</container>
    <container>t2</container>
    <container>t3</container>
    <container>t0effective</container>
    <container>t1effective</container>
    <container>t2effective</container>
    <container>t3effective</container>
    <container>tmax</container>
    <container>dt01</container>
    <container>dt23</container>

    <container>count</container>
    <container init="0">index</container>

    <container>radius</container>
    <container>charge</container>
    <container init="2.061">K</container> <!-- 1e-10 -->
    <container init="600">U</container>
<!-- RESULT TABLE -->
    <container size="0">DT01</container>
    <container size="0">DT23</container>
    <container size="0">R</container>
    <container size="0">Q</container>
  </data-containers>

    <input> <!-- Continuous sound recording -->
        <audio>
            <output>recording</output>
            <output component="rate">rate</output>
        </audio>
    </input>

  <views>
    <view label="Simple">
      <edit label="Threshold" unit="a.u." default="0.1" signed="false" min="0" max="1">
        <output>threshold</output>
      </edit>
      <edit label="Minimum Delay" unit="s" default="0.1" signed="false">
        <output>mindelay</output>
      </edit>
      <value label="Time1" size="2" precision="2" unit="s">
        <input>dt01</input>
      </value>
      <value label="Time2" size="2" precision="2" unit="s">
        <input>dt23</input>
      </value>

      <button label="Reset">
        <input type="empty"/>
        <output>tlist</output>
        <input type="value">0</input>
        <output>index</output>
      </button>

      <value label="Radius" size="2" precision="2" unit="µm" color="blue">
        <input>radius</input>
      </value>
      <value label="Charge/e" size="2" precision="2" color="red">
        <input>charge</input>
      </value>
      <edit label="Tension U" unit="V" default="600" signed="false">
        <output>U</output>
      </edit>
    </view>
  </views>

  <analysis>

    <count> <!-- length of the sound recording -->
      <input clear="false">recording</input>
      <output>count</output>
    </count>
    <max>   <!-- position and value of sound peak, recording is cleared -->
      <input as="y">recording</input>
      <output as="position">i</output>
      <output as="max">max</output>
    </max>
    <add>   <!-- index + i = absolute peak position, i is cleared -->
      <input>i</input>
      <input clear="false">index</input>
      <output>i2</output>
    </add>

    <divide> <!-- absolute time, i2 is cleared -->
      <input>i2</input>
      <input clear="false">rate</input>
      <output>t</output>
    </divide>

    <if greater="true"> <!-- if not high enough t to 0 -->
      <input clear="false">max</input>
      <input clear="false">threshold</input>
      <input clear="false">t</input>
      <input type="value">0</input>
      <output>t</output>
    </if>

    <const>  <!-- last=0 -->
      <output>last</output>
    </const>
    <append>
      <input clear="false">tlist</input>
      <output clear="false">last</output>
    </append>

    <add> <!-- limit = mindelay for absolute time t -->
      <input clear="false">last</input>
      <input clear="false">mindelay</input>
      <output>limit</output>
    </add>
    <if less="true" equal="true">
      <input clear="false">limit</input>
      <input clear="false">t</input>
      <input clear="false">t</input>
      <input type="value">0</input>
      <output>t</output>
    </if>
    <if greater="true"> <!-- if t greater 0, t saved to tlist -->
      <input clear="false">t</input>
      <input type="value">0</input>
      <input clear="false">t</input>
      <output clear="false">tlist</output>
    </if>

    <differentiate> <!-- calculate time intervals in tlist -->
      <input clear="false">tlist</input>
      <output>dtlist</output>
    </differentiate>
    <append>
      <input type="value">0</input>
      <output clear="false">dtlist</output>
    </append>

    <count> <!-- number of measurements in tlist -->
      <input clear="false">tlist</input>
      <output>tcount</output>
    </count>
    <subtract>
      <input clear="false">tcount</input>
      <input type="value">1</input>
      <output>tcount-1</output>
    </subtract>

    <add> <!-- upgrade index for next run, count is cleared -->
      <input clear="false">index</input>
      <input>count</input>
      <output>index</output>
    </add>
    <divide> <!-- tmax ? -->
      <input clear="false">index</input>
      <input clear="false">rate</input>
      <output>tmax</output>
    </divide>

    <const>  <!-- ? if followed by subrange -->
      <output>t0</output>
    </const>
    <const>
      <output>t1</output>
    </const>
    <const>  
      <output>t2</output>
    </const>
    <const>
      <output>t3</output>
    </const>

    <subrange> <!-- 1st element of tlist -->
      <input as="from" type="value">0</input>
      <input as="length" type="value">1</input>
      <input as="in" clear="false">tlist</input>
      <output clear="false">t0</output>
    </subrange>
    <subrange>  <!-- 2nd element of tlist -->
      <input as="from" type="value">1</input>
      <input as="length" type="value">1</input>
      <input as="in" clear="false">tlist</input>
      <output clear="false">t1</output>
    </subrange>
    <subrange> <!-- 3d element of tlist -->
      <input as="from" type="value">2</input>
      <input as="length" type="value">1</input>
      <input as="in" clear="false">tlist</input>
      <output clear="false">t2</output>
    </subrange>
    <subrange>  <!-- 4th element of tlist -->
      <input as="from" type="value">3</input>
      <input as="length" type="value">1</input>
      <input as="in" clear="false">tlist</input>
      <output clear="false">t3</output>
    </subrange>

    <if less="true" equal="true">
      <input clear="false">t3</input>
      <input type="value">0</input>
      <input clear="false">tmax</input>
      <input clear="false">t3</input>
      <output>t3effective</output>
    </if>
    <if less="true" equal="true">
      <input clear="false">t2</input>
      <input type="value">0</input>
      <input clear="false">tmax</input>
      <input clear="false">t2</input>
      <output>t2effective</output>
    </if>
    <if less="true" equal="true">
      <input clear="false">t1</input>
      <input type="value">0</input>
      <input clear="false">tmax</input>
      <input clear="false">t1</input>
      <output>t1effective</output>
    </if>
    <if less="true" equal="true">
      <input clear="false">t0</input>
      <input type="value">0</input>
      <input clear="false">tmax</input>
      <input clear="false">t0</input>
      <output>t0effective</output>
    </if>

    <subtract>
      <input>t1effective</input>
      <input>t0effective</input>
      <output>dt01</output>
    </subtract>
    <subtract>
      <input>t3effective</input>
      <input>t2effective</input>
      <output>dt23</output>
    </subtract>

    <formula formula="sqrt(9.71/[1])">
      <input clear="false">dt01</input>
      <output>radius</output>
    </formula>

    <formula formula="[3]*(1/[1]+1/[2])*sqrt(0.1/[1])/[4]/0.00001602"> <!-- 1e-10*0.0001/1e-19=1e5 -->
      <input clear="false">dt01</input>
      <input clear="false">dt23</input>
      <input clear="false">K</input>
      <input clear="false">U</input>
      <output>charge</output>
    </formula>

<!-- RESULT TABLE -->
    <if equal="true">
      <input as="a" clear="false">tcount</input>
      <input as="b" type="value">6</input>
      <input as="true" clear="false">dt01</input>
      <output clear="false">DT01</output>
    </if>
    <if equal="true">
      <input as="a" clear="false">tcount</input>
      <input as="b" type="value">6</input>
      <input as="true" clear="false">dt23</input>
      <output clear="false">DT23</output>
    </if>
    <if equal="true">
      <input as="a" clear="false">tcount</input>
      <input as="b" type="value">6</input>
      <input as="true" clear="false">radius</input>
      <output clear="false">R</output>
    </if>
    <if equal="true">
      <input as="a" clear="false">tcount</input>
      <input as="b" type="value">6</input>
      <input as="true" clear="false">charge</input>
      <output clear="false">Q</output>
    </if>
    <!-- NEW: restart by two more claps -->
    <if greater="true">
      <input as="a" clear="false">tcount</input>
      <input as="b" type="value">5</input>
      <input as="true" type="empty"/>
      <output>tlist</output>
    </if>
    <if greater="true">
      <input as="a" clear="false">tcount</input>
      <input as="b" type="value">5</input>
      <input as="true" type="value">0</input>
      <output>index</output>
    </if>

  </analysis>

  <!-- ALL peak times: for some potential usage -->
  <export>
    <set name="Result table">
      <data name="time1 (s)">DT01</data>
      <data name="time2 (s)">DT23</data>
      <data name="radius (µm)">R</data>
      <data name="charge/e">Q</data>
    </set>
  </export>

</phyphox>