Measuring time within two events help with code

I have an photdiode wich reads 0-130 (always zero with little light, 130 with max light) with this code :

int sensorPin = A3;
int sensorValue = 0;

void setup(void) {
Serial.begin(9600);
pinMode(sensorPin, INPUT);
}

void loop(void) {
sensorValue = analogRead(sensorPin);
Serial.println(sensorValue);

Now im trying to measure the time.
starting when the value is above 5 and stopping when value is below 5.
Then i would like to print in the monitor how long this duration was.
When again light hits the diode and the value goes again above 5 it should start measure again and print again but as long the diode doesnt get hit by light again it shouldnt make a new measurement
so i wrote this code but i couldnt get it to work, is there someone who can help me please ?

int sensorPin = A3;
int sensorValue = 0;
boolean timingFlag;
unsigned long timeOff;
unsigned long timingMillis;

void setup(void) {
Serial.begin(9600);
pinMode(sensorPin, INPUT);
timingFlag = false;
}

void loop(void) {
sensorValue = analogRead(sensorPin);
if(timingFlag == false && sensorValue >=5){
timingFlag = true;
timingMillis = millis(); //set the variable Start to current microseconds

if(timingFlag == true && sensorValue <=5){
Serial.print("Time = ");
timeOff = millis() - timingMillis;
Serial.println( timeOff );
timingFlag = false;
}} }

Your second if statement is contained inside the first. Try moving it out.

If You use the Autoformat function, Ctrl T, in the IDE, You will see errors like this.
Also, please use code tags when posting code, upper left symbol in this window

const byte SensorPin = A3;


const int Threshold = 5;
boolean SensorWasAboveThreshold = false;


void setup(void)
{
  Serial.begin(115200);
  // pinMode(sensorPin, INPUT); // DON'T USE ON ANALOG INPUTS
}


void loop(void)
{
  static unsigned long startTime = 0;
  static unsigned long stopTime = 0;


  boolean sensorIsAboveThreshold =
    analogRead(SensorPin) > Threshold;


  // Detect state change
  if (sensorIsAboveThreshold != SensorWasAboveThreshold)
  {
    SensorWasAboveThreshold = sensorIsAboveThreshold;
    
    if (sensorIsAboveThreshold)
    {
      // Just went from below Threshold to above Threshold
      startTime = micros();
    }
    else
    {
      // Just went from above Threshold to below Threshold
      stopTime = micros();


      // Display the number of microseconds above threshold
      Serial.println(stopTime - startTime);
    }
  }
}

Thank you all for the answers, both codes work now.
Your codes is much more elegant @johnwasser.
I put the timing in the other code also in micros for the precision and put the baud up as well.
Is there a way to get the micro seconds printed as millis with comma ?
i would like to get big numbers printed 1000,700 instead of 1000700
and if possible get small numbers printed 0,490 instead of 490
Your wrote dont use analog inputs, why ? @johnwasser
Im trying to measure times down to 490 micros so 0,49 millis.

When i got this precise running, I would like to use two photodiodes and measure the time between first diode getting light and second diode getting light as well as the time between first diode doesnt get light any more and second diode doesnt get light any more plus the times each diode is lit.

thanks
Benjamin

benni95:
i would like to get big numbers printed 1000,700 instead of 1000700 and if possible get small numbers printed 0,490 instead of 490

      unsigned long elapsedMicroseconds = stopTime - startTime;
    // Convert to float, divide by 1000, and print with 3 decimal places
    Serial.println(elspasedMicroseconds / 1000.0, 3);

benni95:
Your wrote dont use analog inputs, why ? @johnwasser

I said to not use pinMode() on analog input pins. The pinMode() function should only be used on pins you are using for digitalRead() or digitalWrite().

Analog reading is slow. If you want to time very short intervals, use an analog comparator to do the thresholding and turn your analog signal into a digital signal. Then you can measure down to a fraction of a microsecond using the Input Capture feature of Timer1. For multiple signals, you would need some external switching hardware or an Arduino with more Input Capture pins than the one on an UNO/Nano.

I gathered the diode from a broken camera its a silicon blue cell and the repair manual says it has a response time from 10^-6 sec.
right now i got one leg from the diode connected to a3 and the other to ground, i put a germanium diode as a resistor between a3 and ground this way the reading keeps zero when there is no light on the diode.
which parts do i need for analog comparator and how do i hook it up with the diode?
So its not possible to have one diode on d pin 1 and the other on d pin2 and start timing when pin 1 change and stop when pin 2 change ?
goal is to measure the time a curtain of a camera needs to travel 35mm so i want to put two diodes 35mm apart
You have been really helpful already !

The code looks right now like this

const byte SensorPin = A3;


const int Threshold = 1;
boolean SensorWasAboveThreshold = false;


void setup(void)
{
  Serial.begin(115200);
  // pinMode(sensorPin, INPUT); // DON'T USE ON ANALOG INPUTS
}


void loop(void)
{
  static unsigned long startTime = 0;
  static unsigned long stopTime = 0;
  unsigned long elapsedMicroseconds = stopTime - startTime;

  boolean sensorIsAboveThreshold =
    analogRead(SensorPin) > Threshold;


  // Detect state change
  if (sensorIsAboveThreshold != SensorWasAboveThreshold)
  {
    SensorWasAboveThreshold = sensorIsAboveThreshold;

    if (sensorIsAboveThreshold)
    {
      // Just went from below Threshold to above Threshold
      startTime = micros();
    }
    else
    {
      // Just went from above Threshold to below Threshold
      stopTime = micros();


      // Display the number of microseconds above threshold
      Serial.print("Verschlusszeit = ");
      Serial.println(stopTime - startTime);
      Serial.println(elapsedMicroseconds / 1000.0, 3);
    }
  }
}

somehow it wont keep the numbers thats why i put the old print behind so you can see the difference, now the print looks like this.

05:12:12.061 -> Verschlusszeit = 224
05:12:12.061 -> 4287574.000
05:12:14.617 -> Verschlusszeit = 969136
05:12:14.617 -> 4293358.500
05:12:18.218 -> Verschlusszeit = 1208592
05:12:18.218 -> 4292602.500
05:12:21.984 -> Verschlusszeit = 2679600
05:12:21.984 -> 4293881.000
05:12:23.402 -> Verschlusszeit = 413392
05:12:23.402 -> 4293939.000
05:12:26.488 -> Verschlusszeit = 1941184
05:12:26.488 -> 4293840.500
Any idea ?
@johnwasser
thanks upfront

You are calculating'elapsedMicroseconds' at the top of loop(), BEFORE you set 'stopTime'. You should do that calculation AFTER you set 'stopTime'.

Works now like a charme thanks again ! @johnwasser
here is the updated Code

const byte SensorPin = A3;


const int Threshold = 1;
boolean SensorWasAboveThreshold = false;


void setup(void)
{
  Serial.begin(115200);
  // pinMode(sensorPin, INPUT); // DON'T USE ON ANALOG INPUTS
}


void loop(void)
{
  static unsigned long startTime = 0;
  static unsigned long stopTime = 0;
  
  boolean sensorIsAboveThreshold =
    analogRead(SensorPin) > Threshold;


  // Detect state change
  if (sensorIsAboveThreshold != SensorWasAboveThreshold)
  {
    SensorWasAboveThreshold = sensorIsAboveThreshold;

    if (sensorIsAboveThreshold)
    {
      // Just went from below Threshold to above Threshold
      startTime = micros();
    }
    else
    {
      // Just went from above Threshold to below Threshold
      stopTime = micros();
      unsigned long elapsedMicroseconds = stopTime - startTime;

      // Display the number of microseconds above threshold
      Serial.print("Verschlusszeit = ");
      Serial.println(elapsedMicroseconds / 1000.0, 3);
    }
  }
}

benni95:
So its not possible to have one diode on d pin 1 and the other on d pin2 and start timing when pin 1 change and stop when pin 2 change ?

Any Answers on that Question ? @johnwasser
And is there an other way to tip you then bitcoin ?
best regards

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.