Button not read when pressed

Hi guys

simle question here regarding a button.
i am using a button in a project and when its pressed a if and a for loop start.
the button state is being read as high but when i do press it seems to miss it.
i think the code is possible running to fast for it to be read when i press it or something do i need to add a debounce feature or a delay to make it read.

code attached

#include <Wire.h>
#include <Adafruit_MMA8451.h>
#include <Adafruit_Sensor.h>

Adafruit_MMA8451 mma = Adafruit_MMA8451();

Adafruit_MMA8451 mma2 = Adafruit_MMA8451();



int addr = 0;  
const int buttonPin = 2; 
int buttonState = 0;         
int measurementCount =1;



void setup(void) {
  Serial.begin(9600);
   pinMode(buttonPin, INPUT_PULLUP);
   
     
  Serial.println("Adafruit MMA8451 test!");
   if (! mma.begin(0x1C)) {                            // First sensor in the address of 0x1C
    Serial.println("Couldnt start first sensor");
    while (1);
  }

  if (! mma2.begin(0x1D)) {                           // Second sensor in the address of 0x1D
    Serial.println("Couldnt start second sensor");
    while (1);
  }
}


void loop() {
  /* Get a new sensor event */ 
  sensors_event_t event; 
  sensors_event_t event2;
  mma.getEvent(&event);
   mma2.getEvent(&event2); 

  /* Display the results (acceleration is measured in m/s^2) */
 
  Serial.print("X: \t"); Serial.print(event.acceleration.x); Serial.print("\t");
  Serial.print("Y: \t"); Serial.print(event.acceleration.y); Serial.print("\t");
  Serial.print("Z: \t"); Serial.print(event.acceleration.z); Serial.print("\t");    //sensor one data print
  Serial.println("m/s^2 ");

   Serial.print("X: \t"); Serial.print(event2.acceleration.x); Serial.print("\t");
 Serial.print("Y: \t"); Serial.print(event2.acceleration.y); Serial.print("\t");
 Serial.print("Z: \t"); Serial.print(event2.acceleration.z); Serial.print("\t");  //sensor two data print
 Serial.println("m/s^2 ");

    buttonState = digitalRead(buttonPin);
    Serial.println(buttonState); 
    if(buttonState == LOW){  

          
     float valx = event.acceleration.x;
     // EEPROM.put(addr, valx);
      Serial.print("valx    ");
      Serial.print(valx);

           float valx2 = event2.acceleration.x;
     // EEPROM.put(addr, valx2);
      Serial.print("valx2    ");
      Serial.print(valx2);
           
     float valy = event.acceleration.y;
     // EEPROM.put(addr, valy);
      Serial.print("   valy    ");
      Serial.print(valy);      

               float valy2 = event2.acceleration.y;
     // EEPROM.put(addr, valy2);
      Serial.print("valy2    ");
      Serial.print(valy2);

     float valz = event.acceleration.z;
     // EEPROM.put(addr, valz);
      Serial.print("   valz    ");
      Serial.print(valz);
      Serial.println();

      float valz2 = event2.acceleration.z;
     // EEPROM.put(addr, valz2);
      Serial.print("valz2    ");
      Serial.print(valz2);


     
     for(measurementCount; measurementCount<100; measurementCount++) {

     sensors_event_t event; 
  sensors_event_t event2;
  mma.getEvent(&event);
   mma2.getEvent(&event2);

     float NewX;    
     NewX = event.acceleration.x - valx;
     Serial.print("Newx    ");
     Serial.print(NewX);
   
     float NewY;   
     NewY = event.acceleration.y - valy;
     Serial.print("   NewY    ");
     Serial.print(NewY);     
     
     float NewZ;    //NEW Z 
     NewZ = event.acceleration.z - valz;
     Serial.print("   NewZ    ");
     Serial.print(NewZ);
     Serial.println();

          float NewX2;    
     NewX2 = event2.acceleration.x - valx2;
     Serial.print("Newx2    ");
     Serial.print(NewX2);
   
     float NewY2;   
     NewY2 = event2.acceleration.y - valy2;
     Serial.print("   NewY2    ");
     Serial.print(NewY2);     
     
     float NewZ2;    //NEW Z 
     NewZ2 = event2.acceleration.z - valz2;
     Serial.print("   NewZ2    ");
     Serial.print(NewZ2);
     Serial.println();

     
     
  
      Serial.print("measurementcout");
     Serial.println(measurementCount);
      }

    }
  delay(500);
  
}

Your topic was MOVED to its current forum category as it is more suitable than the original

you say HIGH, but the pin is configured as INPUT_PULLUP which means the button needs to be connected between the pin and ground and pulls the pin LOW

the half second delay is unnecessary and suggests the code is not monitoring the button very often

the delay(500) is inside the loop after the button is pressed so this should not matter for sensing the button.

yes the idea is when the button is pressed it goes low but only when pressed hence the pulup.

the code for works on a simpler version of this code this is why i think its missing it or running to fast

I suspect the exact opposite.
How long are you holding the button?

no, the delay is at the very end of loop() which means the button is never checked more often that every half second

ive tried holding it pressing quick and slow none seem to work

is the button connected to ground?

it's outside the for (measuremetnCount loop and outside the closing brace for if (buttonState

its connected true pin 2 and 5 volts not ground but i dont think ground is needed?

the internal pullup pulls the pin HIGH. what pulls the pin LOW?

so connected to ground will fix this? i didnt have it connected t ground on the other code and it worked

don't know what you did on the other code.

if the internal pullup is used the pin needs to connect to ground -- your code checks for buttonState == LOW

how often does your "m/s^2" print occur?

for pullup its should not be connected to ground i taught and orther it needs

INPUT_CONFIGURE enables an internal pull-up resistor. you have no control over how it's connected other than enabling it

the external button switch pulls down the pin to ground

a "pull-down" resistor would connect to ground, as it's name implies

apologies to everyone it was a wiring mistake. when i re wired it i connected to 5V instead of ground gcjr was correct.

i got confused with something else thanks very much

If it worked with a connection to 5V that's what one may call bad luck ... :wink:

INPUT_PULLUP as already mentioned by @gcjr switches the pin to HIGH level using an internal pullup resistor which limits the current when you press the button and shortcut the pin to GND. It goes LOW when and while pressed ...

So moving to GND should fix this, however for proper function you should remove the delay(500) as also mentioned before, because if you press the button shortly you may not do this synchronized with your sketch ...

In addition you may find it helpful not to react on press but on a release of the button, That ensures that the action will only take place once and not repeatedly because loop() can be "damned quick" ...

thank you.
you are 100% correct

You are welcome. Actually there is a simple but helpful library called ezButton on github that does work with millis() (no timer or hardware interrupts) and provides debouncing.

That may ease a lot of things ...

thanks ill take a look at this. millis() would be alot better than the delay() fuction