Speed measurement via button

Hello Arduino friends. I'm new to the forum, but not new to developing applications. So far I have always used Java to develop small applications for the Arduino (UNO).
Now I would like to use a touch pulse to determine the time between the individual pulses (and then the speed from that).
I query the position of the button in the loop, when it is high I note the time until the next time. I can then calculate the time from this (I thought).
But the results are not at all correct. The calculation of v is also incorrect, although Mils and speeTime are correct. here ist my Code in Loop:

tasterstatus=digitalRead(taster);
if(tasterstatus==HIGH){
  delay(200);
  mils = millis();
  float v=mils-speedTime;            
  speedTime = mils;
  lcd.setCursor(0, 0);             
  lcd.print(mils);   
  lcd.setCursor(10, 0);             
  lcd.print(speedTime);  
  lcd.setCursor(0, 1); 
  lcd.print(v);    


       
}

Can someone help me, I don't know these problems from Java.
greeting
Pfeiffy

Welcome to the forum

Please post your full sketch

you need

  • reliable detection of event, a change in state, possibly needing debouncing
  • capture a timestamp on each event
  • if more than one event has occurred, calculate time between current and previous event
  • calculate rate of events (1 / t)

Hi @pfeiffyy ,

measuring the time between two button presses can be done like this:

/*
  Forum: https://forum.arduino.cc/t/speed-measurement-via-button/1213924
  Wokwi: https://wokwi.com/projects/387533742096207873

  2024/01/21
  ec2021

*/

const byte buttonPin = 4;
unsigned long lastPressedTime = 0;

void setup() {
  pinMode(buttonPin, INPUT_PULLUP);
  Serial.begin(115200);
  Serial.println("Start ");
}

void loop() {
  if (buttonPressed()){
    if (lastPressedTime > 0){
      unsigned long difference = millis()-lastPressedTime;
      Serial.print("Time difference: ");
      Serial.println(difference);
    }
    lastPressedTime = millis();
  }
}

boolean buttonPressed(){
static unsigned long lastChange = 0;
static byte lastState = LOW;
static byte state;
   byte actState = digitalRead(buttonPin);
   if (actState != lastState) {
      lastState = actState;
      lastChange = millis();
   }
   if (state != actState && millis()-lastChange > 20){
     state = actState;
     return !state; // Returns true only when state changes from HIGH to LOW
   }
  return false;
}

To be tested on Wokwi: https://wokwi.com/projects/387533742096207873

Some additional words:

  • Buttons usually do "bounce" (create a number of spikes when pressed or released that are read by the controller as very quick on/off events). So a method of debouncing is required to avoid reading the spikes.
  • We have to distinguish "event" from "state":
    • A usual button can be in the state "open" or "closed".
    • The moment when the button is pressed or released can be seen as almost taking no time, it is an event.
  • Therefore the function buttonPressed() in this sketch does not return the state of the button, but only "true in the event when the button state changes from HIGH to LOW.
  • The sketch prints the time between the previous time the button was and pressed and the actual time when it was pressed again.
  • You wrote that you want to calculate a "speed". What speed do you want to calculate? Speed is (something)/time, what is your (something) ?

Hello,
I'm sorry I haven't been in touch for so long, but I've had a lot of work. I'll test it now and report back. Thanks in advance for the code and the link.
Pfeiffy

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.