problem during counting number of botton pressed in given time

I am trying to write an arduino code which will save the number of times the switch/button has pressed in 1.5 seconds by the user. Here after 1.5 seconds the number of button pressed again becomes zero. And according to number of times the user has pressed the button inside the 1.5 seconds the code will be executing certain task. please help me in this problem. i tried every method and searched every page in google but didn't found the proper answer. My aruino program is:

const int buttonPin = 2;
const int ledPin = 8;
int buttonState = 0;
int count = 0;
int low = 1; 
int t0 = 0; 
bool startTime_on = false;
bool from_loop = false;

int pin1 = 3;
int pin2 = 5;
int pin3 = 6;
int t = 100;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  startTime = millis();
  pinMode(ledPin,OUTPUT);
  pinMode(buttonPin,INPUT);

  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

}

void loop() {
  // put your main code here, to run repeatedly:
  currentTime = millis();
  buttonState = digitalRead(buttonPin);

  if(buttonState == HIGH && currentTime-t0 > 100) {
    digitalWrite(ledPin, HIGH);
    if(low == 1) {
      count++; 
      t0 = millis();
      low = 0;
    }
    if(startTime_on == false) {
      startTime = millis();
      startTime_on = true;
    }
  }
  else {
    digitalWrite(ledPin, LOW);
    low = 1;
  }

  if(currentTime-startTime > 1500) {
    if(count==1) { chapter++;
      file(count); 
    }
    else if(count==2) { unit++;
      file(count);
    }
      else if(count==3) {
        file(count);
      }
      else if(count!=0) {
        Serial.println(count);
      }
  count = 0;
  startTime_on = false;
  }
}

Here i have used currentTime-t0 > 100 line because when i don't use this the number of button state comes very large, even if i push it just for once. So how can i finish this?

dineshlama:
Here i have used currentTime-t0 > 100 line because when i don't use this the number of button state comes very large, even if i push it just for once. So how can i finish this?

You don't get a clean signal when you press a button. It will alternate between open and closed a few times. To filter out that you need to 'debounce' the button. It can be done in hardware or software. How to do it in software is described in tutorial https://www.arduino.cc/en/Tutorial/Debounce
edit:One of several ways to do it in software.

dineshlama:
I am trying to write an arduino code which will save the number of times the switch/button has pressed in 1.5 seconds by the user. Here after 1.5 seconds the number of button pressed again becomes zero. And according to number of times the user has pressed the button inside the 1.5 seconds the code will be executing certain task. please help me in this problem. i tried every method and searched every page in google but didn't found the proper answer. My aruino program is:

const int buttonPin = 2;

const int ledPin = 8;
int buttonState = 0;
int count = 0;
int low = 1;
int t0 = 0;
bool startTime_on = false;
bool from_loop = false;

int pin1 = 3;
int pin2 = 5;
int pin3 = 6;
int t = 100;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  startTime = millis();
  pinMode(ledPin,OUTPUT);
  pinMode(buttonPin,INPUT);

while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

}

void loop() {
  // put your main code here, to run repeatedly:
  currentTime = millis();
  buttonState = digitalRead(buttonPin);

if(buttonState == HIGH && currentTime-t0 > 100) {
    digitalWrite(ledPin, HIGH);
    if(low == 1) {
      count++;
      t0 = millis();
      low = 0;
    }
    if(startTime_on == false) {
      startTime = millis();
      startTime_on = true;
    }
  }
  else {
    digitalWrite(ledPin, LOW);
    low = 1;
  }

if(currentTime-startTime > 1500) {
    if(count==1) { chapter++;
      file(count);
    }
    else if(count==2) { unit++;
      file(count);
    }
      else if(count==3) {
        file(count);
      }
      else if(count!=0) {
        Serial.println(count);
      }
  count = 0;
  startTime_on = false;
  }
}



Here i have used currentTime-t0 > 100 line because when i don't use this the number of button state comes very large, even if i push it just for once. So how can i finish this?

I wasn't able to compile your code easily to shift it to interrupts method of detecting button presses. but I adapted my code to work with your sequence. Add you code to this and you should have what you need. Lots of notes in the code :slight_smile:

// While software debounce can be a solution, a small capacitor like 1uf can provide huge advantage over software solutions.

#define DebounceDelay 100 // This replaces "DebounceDelay" with the interger 100 before compiling
const int buttonPin = 2; // with attachInterrupts this can only be pin 2 or 3 using an uno!!!! it can be other pins with other microcontrollers
volatile int count = 0; // Changed count to volatile so it can be used in a interrupt properly

// Put your code here. Keep DebouncedFunction code short no delay() or Serial.print() or other time code that could consume processor time for a long period
void DebouncedFunction() {
  count++;
}

// Contains the code that debounces your button based on debounce delay
void DebounceCode() {
  static unsigned long T;
  if ( (unsigned long)(millis() - T) >= (DebounceDelay)) {
    T = millis();
    DebouncedFunction();
  }
}

// this function is for describing what is going to happen when we attach interrupt
void DebounceInterruptSetup(int PIN) {
  attachInterrupt(digitalPinToInterrupt(PIN), DebounceCode, FALLING);
  // Because we are only looking for state chage to LOW this simplifies the code
  // DebounceCode function will only trigger each time the PIN is shorted to ground.
  // we don't need to keep track of its state between button press and release.
}

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode(buttonPin, INPUT_PULLUP); // Short button pin to ground for easy wiring
  DebounceInterruptSetup(buttonPin); // only works on pin 2 and 3 on the UNO
  // An interrupt occurs when the pin changes state and stopps everything else to process the request. in our case we are incrementing the counter by 1 if 100 ms has passed since the last trigger.

  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }
}

void loop() {
  // put your main code here, to run repeatedly:
  // We do not need to watch pin 2 because it will automatically see the putton press even if other things are happening
  static unsigned long SpamTimer; // static keyword makes SpamTimer keep its last value when the loop starts over.
  static unsigned long ButtonTimer; // static keyword makes ButtonTimer keep its last value when the loop starts over.
  if (count > 0) {
    if ((millis() - ButtonTimer) >= (1500)) {// waits 1.5 seconds after first press to count the times button was pressed.
      
      Serial.print("Button Pressed ");
      Serial.print(count);
      Serial.print(" times.");
      Serial.println();
      count = 0;
    }
  } else {
    ButtonTimer = millis(); // resets the timer if count is still zero
    if ((millis() - SpamTimer) >= (1000)) {// waits 1 Second before spamming the serial port agan.
      SpamTimer = millis(); 
      Serial.println("Waiting for Button Press...");
    }
  }
}

Z

thanks for informing me about debounced function in arduino, it solved my problem.

hi i did something same yesterday.

take a look at my code mabye you can learn something from it.

#define button 2
uint8_t prevButtonState;
uint8_t buttonCount;
uint32_t timer;
uint8_t flag = 1;

void setup() {
  Serial.begin(9600);
  pinMode(button, INPUT_PULLUP);
}

void loop() {
  uint8_t buttonState = digitalRead(button);
  uint32_t currentTime = millis();

  if (buttonState == LOW) {
    if (buttonState != prevButtonState) {
      buttonCount++;
      timer = currentTime;
      flag = 0;
    }
  }
  prevButtonState = buttonState;

  if (currentTime >= timer + 300) { //Reset time!
    if (flag == 0) {
      Serial.println("test");
      buttonCount = 0;
      flag = 1;
    }
  }
  Serial.println(buttonCount);
}

for my purpose it works great! also i have reset time on 300 to you can change it to 1500 for 1.5 sec.