Timing with millis

I am really new to arduino and I am trying to learn how to use the millis() function. I am trying to time how long the pushbutton is turned on (pressed an odd number of times). I already have the code for the button work in the manner I would like. So I would like to find a way to measure the time difference from when the button was pressed the first time to when it is pressed again. Right now my serial monitor is just showing the same millis for both startTime and stopTime. Any help is appreciated!
Thank you in advance!

//Using pushbutton to start the loop of counting plants and wheel slots and reading to SD card
#include <SD.h>

int ledLight = 5; //on/off light
int pushButton = 7; //Button input pin
int plantSensor = 2; //Plant Sensor
int wheelSensor = 3; //Wheel Sensor
int plantCounter = 0; //Plant Sensor Counter begin
int wheelCounter = 0;  //Wheel Sensor Counter begin
int buttonState = 0; //for on/off switch
int lastButtonState = 0; //for on/off switch
int buttonPushCounter = 0; //for on/off switch
int statePlant;
int laststatePlant = HIGH;
int stateWheel;
int laststateWheel = HIGH;
long lastDebounceTime = 0;  // the last time the output pin was toggled
long debounceDelay = 50;    // the debounce time; increase if the output flickers
int refreshrate = 10;
const int CS_PIN = 10; //for SD card
const int POW_PIN = 8; //for SD card
unsigned long millisStart = 0; //Store last time photoelectric sensor was updated

void setup() {
  pinMode(pushButton, INPUT);
  pinMode(ledLight, OUTPUT);
  Serial.println("Initializing Card");
  pinMode(CS_PIN, OUTPUT);
  pinMode(POW_PIN, OUTPUT);
  digitalWrite(POW_PIN, HIGH);
  if (!SD.begin(CS_PIN)) {
    Serial.println("Card Failure");
  Serial.println("Card Ready");
  File commandFile = SD.open("speed.txt");
  if (commandFile) {
    Serial.println("Reading Command File");
    while(commandFile.available()) {
      refreshrate = commandFile.parseInt();
    Serial.print("Refresh Rate = ");
  else {
    Serial.println("Could not read command file.");

void loop()
  //ON/OFF switch loop
  buttonState = digitalRead(pushButton);
  if(buttonState != lastButtonState)
  if (buttonState == HIGH){ //Check if button pressed
    delay(500); //debouncing
    buttonPushCounter++; //count button presses
    Serial.print("number of button pushes: ");
  if(buttonPushCounter%2 == 0){ //Light off if button pushed 0 or even number of times
    digitalWrite(ledLight, LOW); //Show system is off
  else{ //Light on if button pushed odd number of times
    digitalWrite(ledLight, HIGH); //Show system is on
  //Plant counting loop
  unsigned long start_time;
  unsigned long stop_time;
  if(buttonPushCounter%2 == 0){ //System off if button pushed 0 or even number of times
  else{ //System on if button pressed odd number of times
   start_time = millis();
    int statePlant = digitalRead(plantSensor);
    if ( statePlant != laststatePlant) 
    if(statePlant == HIGH) 
      stop_time = millis();
             File dataFile =SD.open("log.csv",FILE_WRITE);
        dataFile.print((plantCounter));  //Logging plants to SD card
        dataFile.print((((wheelCounter)/2)*4.3)); //Logging wheel slots to SD card
        dataFile.println("  Inches");
        Serial.print((plantCounter)); //Serial Print plants
        Serial.print((((wheelCounter)/2)*4.3)); //Serial print wheel slots
        Serial.print(start_time); //prints start time when button pressed
        Serial.print(stop_time); //prints millis at sensed plant
        Serial.println(stop_time - start_time);
     laststatePlant = statePlant;

  //Wheel hole counting loop
 int stateWheel = digitalRead(wheelSensor);
    if ( stateWheel !=laststateWheel)
        laststateWheel = stateWheel;
        wheelCounter = wheelCounter+1;

Any time you use delay() in your sketch it stops code execution for that amount of time.
The first thing you should do is get rid of the delays.

When you want to time the interval between successive pushes you need to read the value of millis() on the first push, then read the value of millis on the second push. Subtract the two then display this time.

The variables should be static or global, type, unsigned long.


You only use millis in this segment:

 else { //System on if button pressed odd number of times
    start_time = millis();
    int statePlant = digitalRead(plantSensor);
    if ( statePlant != laststatePlant)
      if (statePlant == HIGH)
        stop_time = millis();

Indeed, you always record the start time immediately before you record the stop time. So your results are not unexpected. You have to save the start time somehow, instead of always updating it.

Is there a simple way to save the millis() at a certain point in the code?

unsigned long myTime; // this line goes before 'void setup()'


When the button is pushed (HIGH or LOW, your choice)
myTime = millis(); // save the current value of millis in the myTime variable.


Have you seen and do you understand what is happening here?


Okay, that makes sense Larry D. Thanks so much! I'll test it out tomorrow morning!