I am trying to use millis to determine how long a sensor is on or off for that matter. I am not experienced with using millis and am having a tough time figuring out how to see the timing difference from when I press the pushbutton in the else loop and when the sensor senses something. I am really new to programming so any help is appreciated!
//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 startTime; //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
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
startTime = millis();
int statePlant = digitalRead(plantSensor);
if ( statePlant != laststatePlant)
{
if(statePlant == HIGH)
{
plantCounter=plantCounter+1;
unsigned long currentTime = millis(); //checks current time
unsigned long elapsedTime = (currentTime - startTime); //measures elapsed time
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(startTime); //prints start time when button pressed
Serial.print(",");
Serial.print(currentTime); //prints millis at sensed plant
Serial.print(",");
Serial.println(elapsedTime); //prints change in time
}
}
laststatePlant = statePlant;
}
//Wheel hole counting loop
int stateWheel = digitalRead(wheelSensor);
if ( stateWheel !=laststateWheel)
{
laststateWheel = stateWheel;
wheelCounter = wheelCounter+1;
}
}