can someone me in the right direction.

My goal is to make an led bar graph tachometer for my car, this is kind of over my head at the moment so I'm going to start smaller. I figured to start i would right a program that would give me a readout of button presses per minute, but I cant figure out how to get the Arduino to tell me the rate that the button its switching on and off. if someone would point me in the correct direction I would appreciate it.

If the button wasn't pressed before and is pressed now, count up. If not, do nothing. Every now and then write out the result. The counting looks roughly like this:

void loop() {
static int laststate = LOW;
static int count = 0;

int currrentstate = digitalRead (button);
if (currentstate == HIGH && laststate == LOW) {
    count++;
}
laststate = currentstate;

.... Do something from time to time with count
}

For the "Do something from time to time"-part check the Blink without delay function for the proper use of the millis() function. Have fun wit it.

Korman

thanks for the help. something kinda like this? I think I need to divide by 60000 somewhere, i'm confusing myself, and I think I'm doing the serial wrong.

int currentstate= 0;
int button = 2;
int time = 0;
int bpm = 0; // button per min


void setup() {
  
   Serial.begin(9600);
}

void loop() {
  
  static int laststate = LOW;
static int count = 0;

int currrentstate = digitalRead (button);
if (currentstate == HIGH && laststate == LOW) {
    count++;
     
     time = millis();
     bpm= time/count;
     Serial.print(bpm);
}
  
}

You need to use interrupts and to make sure the signal that you will read is in the 0 to 5v dc range or you will fry your arduino, and search about arduino rpm, there is plenty of code to read/look ;)

Your bpm fraction was upside down, the count should be over time, count/time. You can count seconds or minutes. if (now - then > 60000){ minute++; then=now; Serial.println(count) //print out the counts that occurred that minute count=0; //send count back to 0 } Use unsigned long for your time variables like this: unsigned long time;

const int button = 2;
const unsigned long reporttime = 5000; // Report every 5 seconds


void setup() {
  
   Serial.begin(9600);
}

void loop() {
  
  static int laststate = LOW;
  static long count = 0;
  static unsigned long lasttime;

  // Check if button has been pressed
  int currentstate = digitalRead (button);
  if (currentstate == HIGH && laststate == LOW) {
      count++;
  }
  laststate = currentstate;
    
  // check if it's time to do something
  if (millis() - lasttime > reporttime) {
     long bpm = 60000 * count / (millis() - lasttime);
     Serial.print(bpm);

     // Reset counter and time for next measure
     lasttime = millis();
     count = 0;
  }
}

A few fixes in your code. And yes, there was a 60000 missing somewhere.

Korman

I would like to have it update a lot faster than every 5 seconds, say every 50 to 100 milliseconds. but I get like 594 for every click I do when i have the report time set that low.

say every 50 to 100 milliseconds.

Too fast.

but I get like 594 for every click I do when i have the report time set that low.

You are probably running into contact bounce issues.

so is there any way to get it to update faster? my car could rev all the way up and back down in 5 seconds.

Yes, just change the measurement time.

The example I posted was assuming you're pressing buttons and if you do it the way you proposed, you need to wait for a reasonable amount of cycles to get a reading. I just assumed you're going to have troubles to press buttons at a frequency of 500 times per minute. I just assumed you have enough understanding of what you're doing to modify the 5000 to something more appropriate for your case. An update rate of 100-250ms is usually considered instantaneous for human operators.

An alternative measuring method would be to measure the signal time. Depending on your set-up, one or the other might give you more accurate readings. Which system to chose is a design decision based on what work best for the given requirements.

Korman