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.begin(9600);
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");
return;
}
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 = ");
Serial.print(refreshrate);
Serial.println("ms");
commandFile.close();
}
else {
Serial.println("Could not read command file.");
return;
}
}
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.println("on");
Serial.print("number of button pushes: ");
Serial.println(buttonPushCounter);
}
delay(50);
}
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();
plantCounter=plantCounter+1;
File dataFile =SD.open("log.csv",FILE_WRITE);
if(dataFile)
dataFile.print((plantCounter)); //Logging plants to SD card
dataFile.print(",");
dataFile.print((((wheelCounter)/2)*4.3)); //Logging wheel slots to SD card
dataFile.println(" Inches");
dataFile.close();
Serial.print((plantCounter)); //Serial Print plants
Serial.print(",");
Serial.print((((wheelCounter)/2)*4.3)); //Serial print wheel slots
Serial.print(",");
Serial.print(start_time); //prints start time when button pressed
Serial.print(",");
Serial.print(stop_time); //prints millis at sensed plant
Serial.print(",");
Serial.println(stop_time - start_time);
}
}
laststatePlant = statePlant;
}
//Wheel hole counting loop
int stateWheel = digitalRead(wheelSensor);
if ( stateWheel !=laststateWheel)
{
laststateWheel = stateWheel;
wheelCounter = wheelCounter+1;
}
}