Millis - Getting Elapsed Time

Hi! I have a 555 timer set up on a bread board with an led hooked up to it. I am using an arduino to get the time when the LED turns back on/off. I have tried several versions of code but am afraid that the current version is also set up for errors in the long run. I would like to get the time that the LED is on again, not for how long it is on. I should get about 6 seconds total between the on time and the off time. Any advise, suggestions, work around is welcomed. Thanks in advance. :)

int inputReading = 8; // detect whether LED is on or OFF
unsigned long time, timeOn, timeOff, offTime, onTime;

void setup(){
  Serial.begin(9600);
  pinMode(inputReading, INPUT);
  onTime = millis();
}
void loop(){
  if (digitalRead(8) == LOW){
    offTime = millis();
    timeOn = offTime - onTime;
    if(digitalRead(8) == HIGH){
      onTime = millis();
      timeOff = onTime - offTime;
    }
// get the time led is on again and print this
    time = timeOn + timeOn;
    if (time >= 1000){
      Serial.println(time/1000);
    }
    onTime = millis();
  }
}

I think this part of my code is wrong. Where time is equal to the timeOn + timeOn. It should really be the combined timeOn+timeOff.But when I do this I get crazy numbers in serial monitor.

// get the time led is on again and print this
    time = timeOn + timeOn;
    if (time >= 1000){
      Serial.println(time/1000);
    }
    onTime = millis();
  }
}

How to use this forum

If you indent your code properly, and put it inside code tags, you will have something like this:

int inputReading = 8; // detect whether LED is on or OFF
unsigned long time, timeOn, timeOff, offTime, onTime;

void setup()
  {
  Serial.begin(9600);
  pinMode(inputReading, INPUT);
  onTime = millis();
  }

void loop()
  {
  if (digitalRead( 8 ) == LOW)
    {
    offTime = millis();
    timeOn = offTime - onTime;
    
    if(digitalRead( 8 ) == HIGH)
      {
      onTime = millis();
      timeOff = onTime - offTime;
      }  // end of if HIGH
      
    // get the time led is on again and print this
    time = timeOn + timeOn;
    if (time >= 1000)
      {
      Serial.println(time/1000);
      }
      
    onTime = millis();
    }  // end of if LOW
  }  // end of loop

Your test for "if(digitalRead( 8 ) == HIGH)" is inside the test for if it is LOW. In other words, if it is LOW and HIGH at the same time, which isn't particularly likely.

const int inputReadingPin = 8; // detect whether LED is on or OFF
unsigned long time, timeOn, timeOff, offStartTime, onStartTime;

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

void loop() {
  static int previousState = LOW;
  int newState = digitalRead(inputReadingPin);
  if (newState == previousState)
     return;   // Don't do anything until the state changes

    if (newState == LOW){
      offStartTime = millis();
      timeOn = offStartTime - onStartTime;
    }

    if(newState == HIGH){
      onStartTime = millis();
      timeOff = onStartTime - offStartTime;
    }


// get the time led is on again and print this
    time = timeOn + timeOff;

      Serial.println(time/1000.0);

   previousState = newState;
}
[/quote]

Thanks Nick. I have updated the code.

Thanks John Wasser. I think that works. I may have another question but first need to try a another combination of capacitors and resistors.

Thanks a ton again!

    // get the time led is on again and print this
    time = timeOn + timeOn;

Do you mean: timeOff for one of those? That is just timeOn times two.