I have a project that there's a motor that its speed is being controlled by a rotary encoder and there's a button counting how many rotations the motor is making by continuously being pressed by the motor each time the motor makes a spin. the button is on a piece of metal that should be broken after a huge number of spins, so then when the piece of metal breaks I need the motor to stop automatically after 5 seconds of the button being pressed.
No matter how I try writing the code, millis() keep counting since the start of the program and it stops after 5 seconds of running. Can you please help by making millis count the time after the button has not been pressed.
In the notes there are also lines i tried that did not work.
void loop() {
if (valRotary > 0 && lastValRotary == 0) { // start at first loop lap after that val > 0
startMillis = millis();
}
Serial.print(valRotary); //print the value on the serial monitor
lcd.setCursor(0,1); // set LCD cursor to coloumn 0 row 1
if(valRotary>lastValRotary)
{
Serial.print(" CW"); // print clockwise direction on serial monitor if the current encoder value is bigger than the last encoder value
}
if(valRotary<lastValRotary) {
Serial.print(" CCW"); // print counter clockwise direction on serial monitor if current encoder value is less than the last encoder value
}
lastValRotary = valRotary;
Serial.println(" "); // print space if the last encoder value is the same as the current encoder value
lcd.print(valRotary); // print on LCD
lcd.print(" ");
lcd.setCursor(7,1);
delay(50);
analogWrite(motorPin1,valRotary); // control motor by value in the rotary encoder
buttonState = digitalRead(switch1); //read the value of the button
if (buttonState != lastButtonState) {
if (buttonState == LOW) {
// if the current state is LOW then the button went from off to on and count should increase:
count = count + 1;
Serial.println(count);
lcd.print(count);
} else {
// if the current state is HIGH then the button went from on to off:
currentMillis = millis();
Serial.println("TEST");
}
// Delay a little bit to avoid bouncing
delay(50);
}
// save the current state as the last state, for next time through the loop
lastButtonState = buttonState;
/*if (currentMillis - startMillis >= period) //test whether the period has elapsed and button is not pressed
{
digitalWrite(motorPin1, LOW);
Serial.println("done");
}*/
if (count % 5 == 0 && count > 4) {
digitalWrite(motorPin1, LOW);
}
/* if (val == LOW) //BUTTON GOT PRESSED
{
count = count + 1;
Serial.println(count);
lcd.print(count);
}
if (val == HIGH){
currentMillis = millis() ; //get the current "time" (actually the number of milliseconds since the program started)
Serial.println("TEST");
}
if (currentMillis - startMillis >= period) //test whether the period has elapsed and button is not pressed
{
digitalWrite(motorPin1, LOW);
Serial.println("done");
} */
//
//
//
/* if (val == LOW){
logic = true; //button pressed
}
if (logic == true){
currentMillis = millis(); //Last Pressed time stamp
logic = false;
} else (logic == false); {
if (currentMillis - startMillis >= period ){ //Last pressed time stamp vs. acual time stamp of system if more than period brake up
digitalWrite(motorPin1, LOW);
}
} */
}