I have read it, but I am still so confused about the millis(), I have literally searched everywhere, but it doesn't seem to solve my issue, the maximum i am getting is finding the elapsed time.
I followed what you said about the Bounce2 library, but instead of using that I wrote a button state detection code.
I am very new to millis() function, and that is why having problem integrating it into the program I mentioned above.
Basically what I have to do is: I have three doors, which I am simulating with 3 red LEDs, when any LED is on means that the door is closed, and when any LED is off means that the door is open. I can let in 3 people in through the door, and I am simulating this with the button press.
I let in 3 people (so after 3 button presses) from the first door, and then close all the doors, and after a certain delay I open the second door to move the people in the next area, and again close the door. I want to display on the Serial monitor, how much time people waited in each area. My teacher suggested me to use the millis() function but he never taught that. and that is why i wanted help understanding this function, I am not getting much help from internet or youtube videos.
this is my whole programme so far:
const int buttonPin = 2;
const byte ledPin = 3;
const byte ledPinTwo = 4;
const byte ledPinThree = 5;
int currentButtonState = 0;
int lastButtonState = 0;
int buttonPressCount = 0;
int switchCase = -1;
enum switchCase{
stateOne = 0,
stateTwo = 1,
stateThree = 2,
stateFour = 3
};
void setup()
{
//initialising serial monitor
Serial.begin(9600);
//declaring input
pinMode(buttonPin, INPUT_PULLUP); //INPUT_PULLUP enables the internal Pullup resistor of the microcontroller
//declaring outputs
pinMode(ledPin, OUTPUT);
pinMode(ledPinTwo, OUTPUT);
pinMode(ledPinThree, OUTPUT);
//initial message
Serial.println("Press button to enter 6 people");
//switchCase begins from state one
switchCase = stateOne;
}
void loop()
{
//buttonPressCounterF();
//limitingPress();
StateMachine();
}
void buttonPressCounterF()
{
//reading button pin
currentButtonState = digitalRead(buttonPin);
//comparison of current state with last state of the button
if(currentButtonState != lastButtonState)
//If button is pressed
if(currentButtonState == LOW)
{
//increment button press counter
buttonPressCount++;
//print the press number on lcd screen
Serial.print("Press Number: ");
Serial.println(buttonPressCount);
}
//delay to avoid bouncing
delay(50);
lastButtonState = currentButtonState;
}
void limitingPress()
{
//limiting the number of presses
if(buttonPressCount == 6)
{
Serial.print("6 People are in");
buttonPressCount = 0; //reset counter
switchCase = stateTwo;
}
}
void StateMachine()
{
switch(switchCase)
{
case stateOne:
firstGateOpenStateF();
break;
case stateTwo:
Serial.println("State Two");
allGatesClosedStateF();
switchCase = stateThree;
break;
case stateThree:
Serial.println("State Three");
middleGateOpenStateF();
switchCase = stateFour;
break;
case stateFour:
Serial.println("State Four");
allGatesClosedStateF();
setup();
break;
default:
Serial.println("Error, do something");
break;
}
}
void allGatesClosedStateF()
{
digitalWrite(ledPin, HIGH);
digitalWrite(ledPinTwo, HIGH);
digitalWrite(ledPinThree, HIGH);
delay(2000);
}
void firstGateOpenStateF()
{
digitalWrite(ledPin, LOW);
digitalWrite(ledPinTwo, HIGH);
digitalWrite(ledPinThree, HIGH);
buttonPressCounterF();
limitingPress();
}
void middleGateOpenStateF()
{
digitalWrite(ledPin, HIGH);
digitalWrite(ledPinTwo, LOW);
digitalWrite(ledPinThree, HIGH);
delay(2000);
}