hey guys looking for some advice, what im trying to do it calculate a Rpm of a motor say "40" to how long a Button is pressed and times it, so i could press it for 40 it then would add 40 sec to the main val then waits intill i press it again say 10 sec it would then add it to the main val that already has 40 sec in to 50 sec, then to times the time 50sec by the rpm :--------
say you held the trigger in for a total of 46mins it would times the rpm by that so say your rpm was "840" and mins was 46 you would times 46 by 840 that = 38,640 ?
capture the value of millis() when you detect a press (when button transitions to LOW or HIGH depending if you use a pull-up or not), then do the difference with millis() when you detect the release the button --> that will be your duration
hey guys looking for some advice, what im trying to do it calculate a Rpm of a motor say "40" to how long a Button is pressed and times it, so i could press it for 40 it then would add 40 sec to the main val then waits intill i press it again say 10 sec it would then add it to the main val that already has 40 sec in to 50 sec, then to times the time 50sec by the rpm :--------
I have no idea what you are trying to do with an rpm value and a button press duration, but here is some example code using the Bounce2 button library to determine the duration of a press.
// Include the Bounce2 library found here :
// https://github.com/thomasfredericks/Bounce2
//available through Arduino ide library manager
#include <Bounce2.h>
#define buttonPin 5
// Instantiate a Bounce object called button
Bounce button = Bounce();
unsigned long buttonPressStartTimeStamp;
unsigned long buttonPressDuration;
void setup() {
Serial.begin(115200);
Serial.println("Button Press Duration Example");
// Setup the button with an internal pull-up
pinMode(buttonPin, INPUT_PULLUP);
button.attach(buttonPin);
button.interval(20);//set debounce interval
}
void loop() {
// Update the Bounce instance, does digitalRead of button
button.update();
// Button press transition from HIGH to LOW)
if (button.fell())
{
buttonPressStartTimeStamp = millis();
}
// Button release transition from LOW to HIGH) :
if (button.rose())
{
buttonPressDuration = (millis() - buttonPressStartTimeStamp);
Serial.print("Button pressed for ");
Serial.print(buttonPressDuration);
Serial.println(" milliseconds");
}
}