Using Analogue input to halt timing sequence

Hi there,

I am using arduino for a project, and despite progressing fairly well to start with, I’ve hit a bit of a brick wall with a problem.

Essentially, I want to stop a timer when the value on an analogue input reaches a certain point. I am using a Sharp IR sensor as my analogue input.

I’ve tried various “if” and “else if” commands with limited success, would anyone be able to help me out?

 #define ledPin  13                  // LED connected to digital pin 13
#define buttonPin 7                 // button on pin 7
#define analoguePin A0

int value = LOW;                    // previous value of the LED
int buttonState;                    // variable to store button state
int lastButtonState;                // variable to store last button state
int blinking;                       // condition for blinking - timer is timing
long interval = 100;                // blink interval - change to suit
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 analogueIn = A0;
int sensorValue = 0;                // value read from the pot
int outputValue = 0;                // value output to the PWM (analog out)
int analogueState;


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

   pinMode(ledPin, OUTPUT);         // sets the digital pin as output

   pinMode(buttonPin, INPUT);      // not really necessary, pins default to INPUT anyway
   pinMode(analoguePin, INPUT);
   digitalWrite(buttonPin, HIGH);   // turn on pullup resistors. Wire button so that press shorts pin to ground.
   
}

void loop()
{
    // check for button press
   buttonState = digitalRead(buttonPin);                   // read the button state and store
   
   sensorValue = analogRead (analogueIn);
  
 if (sensorValue <=300) analogueState = LOW; 
  else if (sensorValue >=300)analogueState = HIGH;
 
  
   if (buttonState == LOW && lastButtonState == HIGH  &&  blinking == false){     // check for a high to low transition
      // if true then found a new button press while clock is not running - start the clock

      startTime = millis();                                   // store the start time
      blinking = true;                                     // turn on blinking while timing
      delay(5);                                               // short delay to debounce switch
      lastButtonState = buttonState;                          // store buttonState in lastButtonState, to compare next time

   }

   else if (analogueState == HIGH && lastButtonState == HIGH && blinking == true){     // check for a high to low transition
      // if true then found a new button press while clock is running - stop the clock and report

        elapsedTime =   millis() - startTime;              // store elapsed time
        blinking = false;                                  // turn off blinking, all done timing
        lastButtonState = buttonState;                     // store buttonState in lastButtonState, to compare next time

       // routine to report elapsed time 
        Serial.print( (int)(elapsedTime / 1000L));         // divide by 1000 to convert to seconds - then cast to an int to print

        Serial.print(".");                             // print decimal point

        // use modulo operator to get fractional part of time 
       fractional = (int)(elapsedTime % 1000L);

       // pad in leading zeros - wouldn't it be nice if 
       // Arduino language had a flag for this? :)
       if (fractional == 0)
          Serial.print("000");      // add three zero's
       else if (fractional < 10)    // if fractional < 10 the 0 is ignored giving a wrong time, so add the zeros
          Serial.print("00");       // add two zeros
       else if (fractional < 100)
          Serial.print("0");        // add one zero

       Serial.println(fractional);  // print fractional part of time 

   }

   else{
      lastButtonState = buttonState;                         // store buttonState in lastButtonState, to compare next time
   }

   // blink routine - blink the LED while timing
   // check to see if it's time to blink the LED; that is, the difference
   // between the current time and last time we blinked the LED is larger than
   // the interval at which we want to blink the LED.

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

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

         // if the LED is off turn it on and vice-versa.
         if (value == LOW)
            value = HIGH;
         else
            value = LOW;
         digitalWrite(ledPin, value);
      }
      else{
         digitalWrite(ledPin, LOW);                         // turn off LED when not blinking
      }
   }

}

I’m sure to someone more experienced, this will be absolute childs play. Could someone point me in the right direction?

Much appreciated

Andrew

Could someone point me in the right direction?

The code you posted does something, but you didn't explain what it does.

You want it to do something, but you were rather vague about what that is.

So, no, I'm afraid not.

Edit:

 if (sensorValue = 300) analogueState = LOW; 
  else if (sensorValue = 500)analogueState = HIGH;

On the other hand, this is wrong. Use == for comparison.

Sorry, I realise now that it's a bit vague.

The idea is: I want a button to start a timer, then an IR sensor to stop the timer when it reaches a set value. In this case it's to detect the passing of a bicycle. The bicycle stops the timer and then the time is displayed on the serial monitor.

Thank you for the == information

Andrew

       // routine to report elapsed time 
        Serial.print( (int)(elapsedTime / 1000L));         // divide by 1000 to convert to seconds - then cast to an int to print

        Serial.print(".");                             // print decimal point

        // use modulo operator to get fractional part of time 
       fractional = (int)(elapsedTime % 1000L);

       // pad in leading zeros - wouldn't it be nice if 
       // Arduino language had a flag for this? :)
       if (fractional == 0)
          Serial.print("000");      // add three zero's
       else if (fractional < 10)    // if fractional < 10 the 0 is ignored giving a wrong time, so add the zeros
          Serial.print("00");       // add two zeros
       else if (fractional < 100)
          Serial.print("0");        // add one zero

       Serial.println(fractional);  // print fractional part of time

That’s a lot of code to accomplish this:

        Serial.print((float)elapsedTime / 1000.0, 3);         // convert to seconds; print to 3 places

Thank you, that does seem to make more sense.

Have you any idea how I can get that timer to stop with the analog input? I’ve tried various methods but can’t seem to make it work?

Cheers again

Have you any idea how I can get that timer to stop with the analog input? I’ve tried various methods but can’t seem to make it work?

Yes, but before we get into that, you need to make some changes to your code. One statement per line.

 if (sensorValue <=300) analogueState = LOW; 
  else if (sensorValue >=300)analogueState = HIGH;

That’s two statements on each line.

Put each { on a new line. Use Tools + Auto Format to properly indent the code. Then, repost your code. Explain what it ACTUALLY does, and how that differs from what you want. If you have any serial output, share that. If not, why not?

You sir, are a genius. Followed your advice, changed a few things around and all working now and it looks a lot tidier.

Many virtual beers being sent your way, and thanks again.