Control Stopwatch via Sensor data

Hello everyone,

i made some big progress with my arduino project but i'am stuck at one point:

I want to control a stopwatch on my arduino with a sensor.

When the Arduino reads sensor data input, the stopwatch starts, and when there is no more data flow, it should stop. I got it starting, but when the sensor does not ouput anymore data, it just continues counting.

The code i have for the stopwatch is:

digitalWrite(ledPin, LOW);            // Initiate LED and Step Pin States

  sensorValue = flowRate; 

  
  if (flagA == 0 && flowRate > 0.0 && blinking == false)
  {
    startTime = millis();                               // store the start time
    blinking = true;                                  // turn on blinking while timing

    
    flagA == 1;
  }

  else if (flagA == 1 && flowRate == 0.0 && blinking == true)
  {
    lcd.setCursor(0, 0);
    blinking = false;                                    // turn off blinking, all done timing
    
    // Routine to report elapsed time
    elapsedTime =   millis() - startTime;                // store elapsed time
    elapsedMinutes = (elapsedTime / 60000L);
    elapsedSeconds = (elapsedTime / 1000L);              // divide by 1000 to convert to seconds - then cast to an int to print
    elapsedFrames = (elapsedTime / interval);            // divide by 100 to convert to 1/100 of a second - then cast to an int to print
    fractional = (int)(elapsedFrames % frameRate);       // use modulo operator to get fractional part of 100 Seconds
    fractionalSecs = (int)(elapsedSeconds % 60L);        // use modulo operator to get fractional part of 60 Seconds
    fractionalMins = (int)(elapsedMinutes % 60L);        // use modulo operator to get fractional part of 60 Minutes

    flagA = 0;

with:

int flagA = 0;
int sensorValue = 0;

anyone knows how to control the stopwatch?
Thank you all!

Hard to help when you post partial code that does not work.

    flagA == 1;

Assignment or comparison ?

Danois90:
Hard to help when you post partial code that does not work.

Here the complete code:

#include <LiquidCrystal.h>            // initialize the library with the numbers of the interface pins
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);

int sensorInterrupt = 0;  // 0 = digital pin 2
int sensorPin       = 2;

// The hall-effect flow sensor outputs approximately 4.5 pulses per second per
// litre/minute of flow.
float calibrationFactor = 6;

volatile byte pulseCount;

float flowRate;
unsigned int flowMilliLitres;
unsigned long totalMilliLitres;

unsigned long oldTime;

int sensorValue = 0;

//Stopuhrkram

int ledPin = 13;                    // LED connected to digital pin 13
int buttonPin = 5;                  // button on pin 5 (Only needed when button is used)
int value = LOW;                    // previous value of the LED
int buttonState;                    // variable to store button state (Only needed when button is used)
int lastButtonState;                // variable to store last button state (Only needed when button is used)
int blinking;                       // condition for blinking - timer is timing
int frameRate = 10;                // the frame rate (frames per second) at which the stopwatch runs - Change to suit
long interval = (1000 / frameRate); // blink interval
long previousMillis = 0;            // variable to store last time LED was updated
long startTime ;                    // start time for stop watch
long elapsedTime ;                  // elapsed time for stop watch
int fractional;                     // variable used to store fractional part of time
int fractionalSecs;                 // variable used to store fractional part of Seconds
int fractionalMins;                 // variable used to store fractional part of Minutes
int elapsedFrames;                  // elapsed frames for stop watch
int elapsedSeconds;                 // elapsed seconds for stop watch
int elapsedMinutes;                 // elapsed Minutes for stop watch
char buf[10];                       // string buffer for itoa function
void setup()
{
  lcd.begin(16, 2);

  // Initialize a serial connection for reporting values to the host
  Serial.begin(9600);

  pinMode(ledPin, OUTPUT);         // sets the digital pin as output
  pinMode(buttonPin, INPUT);       // not really necessary, pins default to INPUT anyway
  digitalWrite(buttonPin, HIGH);   // turn on pullup resistors. Wire button so that press shorts pin to ground.


  pinMode(sensorPin, INPUT);
  digitalWrite(sensorPin, HIGH);

  pulseCount        = 0;
  flowRate          = 0.0;
  flowMilliLitres   = 0;
  totalMilliLitres  = 0;
  oldTime           = 0;

  // The Hall-effect sensor is connected to pin 2 which uses interrupt 0.
  // Configured to trigger on a FALLING state change (transition from HIGH
  // state to LOW state)
  attachInterrupt(sensorInterrupt, pulseCounter, FALLING);
}




void pulseCounter()
{
  // Increment the pulse counter
  pulseCount++;
}


void loop()
{

  if ((millis() - oldTime) > 100)   // Only process counters once per 1/10 second
  {
    // Disable the interrupt while calculating flow rate and sending the value to
    // the host
    detachInterrupt(sensorInterrupt);

    // Because this loop may not complete in exactly 1/10 second intervals we calculate
    // the number of milliseconds that have passed since the last execution and use
    // that to scale the output. We also apply the calibrationFactor to scale the output
    // based on the number of pulses per second per units of measure (litres/minute in
    // this case) coming from the sensor.
    flowRate = ((1000.0 / (millis() - oldTime)) * pulseCount) / calibrationFactor;

    // Note the time this processing pass was executed. Note that because we've
    // disabled interrupts the millis() function won't actually be incrementing right
    // at this point, but it will still return the value it was set to just before
    // interrupts went away.
    oldTime = millis();

    // Divide the flow rate in litres/minute by 600 to determine how many litres have
    // passed through the sensor in this 1 second interval, then multiply by 1000 to
    // convert to millilitres.
    flowMilliLitres = (flowRate / 600) * 1000;

    // Add the millilitres passed in this second to the cumulative total
    totalMilliLitres += flowMilliLitres;

    unsigned int frac;

    // Print the flow rate for this second in litres / minute
    lcd.setCursor(8, 0);
    lcd.print(" B:");
    lcd.print(flowRate, 1);  // Print the integer part of the variable
    lcd.print(" L/min");

    lcd.setCursor(0, 1);
    lcd.print("Gesamt: ");
    lcd.print(totalMilliLitres);
    lcd.println (" mL     ");


    // Reset the pulse counter so we can start incrementing again
    pulseCount = 0;

    // Enable the interrupt again now that we've finished sending output
    attachInterrupt(sensorInterrupt, pulseCounter, FALLING);
  }


  //Stopuhrkram
  sensorValue = flowRate;

  digitalWrite(ledPin, LOW);            // Initiate LED and Step Pin States

  buttonState = digitalRead(buttonPin); // Check for button press, read the button state and store

  // check for a high to low transition if true then found a new button press while clock is not running - start the clock
  if (flowRate > 0.0 && blinking == false)
  {
    startTime = millis();                               // store the start  time
    blinking = true;                                  // turn on blinking while timing

   
  }



  else if (flowRate == 0.0 && blinking == true)
  {
    lcd.setCursor(0, 0);
   
    // Routine to report elapsed time
    elapsedTime =   millis() - startTime;                // store    elapsed time
    elapsedMinutes = (elapsedTime / 60000L);
    elapsedSeconds = (elapsedTime / 1000L);              // divide by 1000 to convert to seconds - then cast to an int to print
    elapsedFrames = (elapsedTime / interval);            // divide by 100 to convert to 1/100 of a second - then cast to an int to print
    fractional = (int)(elapsedFrames % frameRate);       // use modulo operator to get fractional part of 100 Seconds
    fractionalSecs = (int)(elapsedSeconds % 60L);        // use modulo operator to get fractional part of 60 Seconds
    fractionalMins = (int)(elapsedMinutes % 60L);        // use modulo operator to get fractional part of 60 Minutes


    if (fractionalMins < 10) {                           // pad in leading zeros
      lcd.print("0");                                 // add a zero
    }

    lcd.print(itoa(fractionalMins, buf, 10));       // convert the int to a string and print a fractional part of 60 Minutes to the LCD
    lcd.print(":");                                 //print a colan.

    if (fractionalSecs < 10) {                           // pad in leading zeros
      lcd.print("0");                                 // add a zero
    }

    lcd.print(itoa(fractionalSecs, buf, 10));          // convert the int to a string and print a fractional part of 60 Seconds to the LCD
    lcd.print(":");                                    //print a colan.

    if (fractional < 10) {                               // pad in leading zeros
      lcd.print("0");                                 // add a zero
    }

    lcd.print(itoa(fractional, buf, 10));              // convert the int to a string and print a fractional part of 25 Frames to the LCD

    lcd.print("\t");
    blinking = false;                                    // turn off blinking, all done timing

  }

  else {
    lastButtonState = buttonState;                  // store buttonState in lastButtonState, to compare next time (only needed when button is in use )

    
  }


  if ( (millis() - previousMillis > interval) ) {

    if (blinking == true) {
      previousMillis = millis();                    // remember the last time we blinked the LED

      digitalWrite(ledPin, HIGH);                   // Pulse the LED for Visual Feedback

      elapsedTime =   millis() - startTime;         // store elapsed time
      elapsedMinutes = (elapsedTime / 60000L);      // divide by 60000 to convert to minutes - then cast to an int to print
      elapsedSeconds = (elapsedTime / 1000L);       // divide by 1000 to convert to seconds - then cast to an int to print
      elapsedFrames = (elapsedTime / interval);     // divide by 40 to convert to 1/25 of a second - then cast to an int to print
      fractional = (int)(elapsedFrames % frameRate);// use modulo operator to get fractional part of 25 Frames
      fractionalSecs = (int)(elapsedSeconds % 60L); // use modulo operator to get fractional part of 60 Seconds
      fractionalMins = (int)(elapsedMinutes % 60L); // use modulo operator to get fractional part of 60 Minutes
      lcd.clear();                                  // clear the LDC

      if (fractionalMins < 10) {                    // pad in leading zeros
        lcd.print("0");                             // add a zero
      }

      lcd.print(itoa(fractionalMins, buf, 10));   // convert the int to a string and print a fractional part of 60 Minutes to the LCD
      lcd.print(":");                             //print a colan.

      if (fractionalSecs < 10) {                    // pad in leading zeros
        lcd.print("0");                             // add a zero
      }

      lcd.print(itoa(fractionalSecs, buf, 10));   // convert the int to a string and print a fractional part of 60 Seconds to the LCD
      lcd.print(":");                             //print a colan.

      if (fractional < 10) {                        // pad in leading zeros
        lcd.print("0");                             // add a zero
      }
      lcd.print(itoa((fractional), buf, 10));  // convert the int to a string and print a fractional part of 25 Frames to the LCD
    }

    else {
      digitalWrite(ledPin, LOW);                 // turn off LED when not blinking
    }


  }
}

Thank you