how to check whenever a button is hold more than a certain time

Hi,

How do you check whenever a button is hold more than a few miliseconds and change the state of the button to off , while using interrupts? Also if the button is released the state also needs to be off

I need this for my little dino game on a matrix display otherwise the game is bit too easy.

Here is my code that i have now that is used to check the button:

void move_player(){
    
    buttonState = digitalRead(buttonPin);
    
  if(buttonState == LOW){
    draw_player(playerGroundRow,playerGroundColumn);
    player_posistion = true;
  }
  if(buttonState == HIGH){
    draw_player(playerGroundRow, playerGroundColumn >>2);
    draw_ground();
    lc1.setLed(0,1,2,false);
    player_posistion = false;
  }
}

Also here is my void setup:

void setup() {
   pinMode(buttonPin, INPUT);
   Serial.begin(9600);
   lc1.shutdown(0,false);
   lc1.setIntensity(0,15);
   attachInterrupt(digitalPinToInterrupt(buttonPin), move_player, CHANGE);
   draw_ground();
   draw_player(playerGroundRow,playerGroundColumn);
}

You shouldn't be doing drawing inside of an interrupt. Interrupts should be as small as possible.

Also, you shouldn't need interrupts for a button. Just check the button transition in your loop code, save the millis() count, and then check the millis() count again on the next button transition.

How is your button wired?

Hi Ardion,

two things

post the whole sketch from the very first line to the very last line.

if you write

How do you check whenever a button is hold more than a few miliseconds and change the state of the button to off , Also if the button is released the state also needs to be off

With "the state of the button" do you mean the value of variable "buttonState" or do you mean the mechanical situation of the button? describe in normal words the different possabilities what the player can do and what should be seen on the screen if player pressed button short - on screen you see .... if player pressed button long - on the screen you see ..... if player does not press the button - on the screen you see ..... best regards Stefan

The code in this link shows how to program for different button clicks

...R

StefanL38:
Hi Ardion,

two things

post the whole sketch from the very first line to the very last line.

if you write
With “the state of the button” do you mean the value of variable “buttonState” or do you mean the mechanical situation of the button?
describe in normal words the different possabilities what the player can do and what should be seen on the screen
if player pressed button short - on screen you see …
if player pressed button long - on the screen you see …
if player does not press the button - on the screen you see …
best regards Stefan

My whole code

#include <LedControl.h>
const int buttonPin = 2;
LedControl lc1=LedControl(12,11,10,1);
int ms = 5;
int obstacle_delay = 100;
int playerGroundColumn = B11110000;
int playerGroundRow = 1; 
bool player_posistion;
bool obstacle_posistion;
int playerScore;
int buttonState = 0;

void draw_player(int row,int column){

  lc1.setRow(0,row,column);

}

void move_player(){
    
    buttonState = digitalRead(buttonPin);
    
  if(buttonState == LOW){
    draw_player(playerGroundRow,playerGroundColumn);
    player_posistion = true;
    
  
  }
  if(buttonState == HIGH){
    
    draw_player(playerGroundRow, playerGroundColumn >>2);
    draw_ground();
    lc1.setLed(0,1,2,false);
    player_posistion = false;
  }
  
 
}
void draw_ground(){
      lc1.setColumn(0,0,B11111111); 
      
}
int move_obstacle(){
  
 
  for(int i = 8; i > -1; i--){
      if(i == 1){
        obstacle_posistion = true;
      }
      else{
        obstacle_posistion = false;
      }
      collide_check();
      if(i == 0 && player_posistion){
        lc1.setLed(0,1,1,true);
      }
      lc1.setLed(0,i,1,true);
      delay(obstacle_delay);
      lc1.setLed(0,i,1,false);
      
     
      
  }
}
void collide_check(){
  if(obstacle_posistion && player_posistion){
    Serial.println("We collided");
    
  }
  else if(obstacle_posistion && !player_posistion){
    playerScore++;
    Serial.println(playerScore);
    obstacle_delay--;
    if(obstacle_delay <=30){
      obstacle_delay = 30;
    }
  }
}

void setup() {
   pinMode(buttonPin, INPUT);
   Serial.begin(9600);
   lc1.shutdown(0,false);
   lc1.setIntensity(0,15);
   attachInterrupt(digitalPinToInterrupt(buttonPin), move_player, CHANGE);
   draw_ground();
   draw_player(playerGroundRow,playerGroundColumn);

}


void loop() {
 move_obstacle();
}

Yeah with the state of the button i mean the mechinacal state.
The player must jump up 2 leds if the button is pressed, if the button is released the player should be on the ground, if the button is hold more than a few milliseconds, I want that the state is low and that the player is forced to be on the ground, otherwise you could just hold the button and the game will be too easy.

Yeah with the state of the button i mean the mechinacal state.

That is impossible. How should a program change the mechanical state of a button???

There are really no commands like

Create_Force_On_The_Finger_Of_The_Player_Taking_His_Finger_physically_From_The_Button

What is so hard about finishing the following sentences?

If player presses button short playersymbol on the screen is drawn at ........

If player presses button longer playersymbol on the screen is drawn at ........

If player presses NO button playersymbol on the screen is drawn at ........

best regards Stefan

StefanL38: That is impossible. How should a program change the mechanical state of a button???

There are really no commands like

Create_Force_On_The_Finger_Of_The_Player_Taking_His_Finger_physically_From_The_Button

What is so hard about finishing the following sentences?

If player presses button short playersymbol on the screen is drawn at ........

If player presses button longer playersymbol on the screen is drawn at ........

If player presses NO button playersymbol on the screen is drawn at ........

best regards Stefan

Oh uhm I meant the buttonState in the program self, I misunderstood your question sorry. If the player presses the button short, the playersymbol on the screen is drawn 2 leds above the ground if the player presses the button long, the playersymbol on the screen is drawn at the ground ift the player presses no button, the playersymbol on the screen will also be drawn at the ground

best regards Adrion

OK so the basic principle is: obstacles (lighted up LEDs move over the "screen" which is a LED-matrix and player must press the button short before the obstacle hits him to "jump over" the obstacle. Holding the button pressed down should result in jump up and land on ground again.

What the logic of code does is: as long as the button is pressed the player is "up in the air" and all obstacle are no problem because the obstacles move on the ground.

A description like this would have helped a lot for other forum-members to understand what you want to do.

Your code must detect the change of the button-state from unpressed to pressed. It is not enough to detect the state. You must detect the transistion from unpressed to pressed. if transition has happened draw player jumped up for a short period of time then player falls back to ground.

For detecting a transition you have to compare the buttons state from last time the button state was determined with the actual button state. This means you need another variable to store the last button state. Whenever last button state was low and actual button state is high a transition low-to-high has happend.

@the community: is there a beginners guide to detect IO-pin transitions ?

Second thing: For measuring periods of time you have to use the millis()-function. To understand the millis()-function will take some time. This is like learning to ride a bike. It needs some time until you can do it. The amount of time is more than five minutes. Once you have learned it you will never forget.

Read this guide to learn to use the function millis(): https://forum.arduino.cc/index.php?topic=503368.0

best regards Stefan

StefanL38: OK so the basic principle is: obstacles (lighted up LEDs move over the "screen" which is a LED-matrix and player must press the button short before the obstacle hits him to "jump over" the obstacle. Holding the button pressed down should result in jump up and land on ground again.

What the logic of code does is: as long as the button is pressed the player is "up in the air" and all obstacle are no problem because the obstacles move on the ground.

A description like this would have helped a lot for other forum-members to understand what you want to do.

Your code must detect the change of the button-state from unpressed to pressed. It is not enough to detect the state. You must detect the transistion from unpressed to pressed. if transition has happened draw player jumped up for a short period of time then player falls back to ground.

For detecting a transition you have to compare the buttons state from last time the button state was determined with the actual button state. This means you need another variable to store the last button state. Whenever last button state was low and actual button state is high a transition low-to-high has happend.

@the community: is there a beginners guide to detect IO-pin transitions ?

Second thing: For measuring periods of time you have to use the millis()-function. To understand the millis()-function will take some time. This is like learning to ride a bike. It needs some time until you can do it. The amount of time is more than five minutes. Once you have learned it you will never forget.

Read this guide to learn to use the function millis(): https://forum.arduino.cc/index.php?topic=503368.0

best regards Stefan

Okay thank you for your help and thank you for the tips for posting on the forum(this was my first time posting on any kind of programming forum). I will try it tomorrow and then I going to let you know how it went