If statement triggers with no input

  arduino, arduino-nano, c++

(sorry in advance as I’m very new to arduino and have no experience what so ever in c++).
anyways, I’m trying to display a number on a led matrix that iterates every time a button is pressed, however this number is iterating when the button is not pressed, and is not iterating when pressed. here’ the code

// Program to demonstrate the MD_Parola library
//
// Uses the Arduino Print Class extension with various output types
//
// MD_MAX72XX library can be found at https://github.com/MajicDesigns/MD_MAX72XX
//

#include <MD_Parola.h>
#include <MD_MAX72xx.h>
#include <SPI.h>

const uint16_t WAIT_TIME = 1000;

// Define the number of devices we have in the chain and the hardware interface
// NOTE: These pin numbers will probably not work with your hardware and may
// need to be adapted
#define HARDWARE_TYPE MD_MAX72XX::FC16_HW
#define MAX_DEVICES 4

#define CLK_PIN   13
#define DATA_PIN  11
#define CS_PIN    7

// Hardware SPI connection
//MD_Parola P = MD_Parola(HARDWARE_TYPE, CS_PIN, MAX_DEVICES);
// Arbitrary output pins
 MD_Parola P = MD_Parola(HARDWARE_TYPE, DATA_PIN, CLK_PIN, CS_PIN, MAX_DEVICES);

const int pinButton  3;
int x = 0;
void setup(void)
{
  P.begin();
  pinMode(pinButton, INPUT);
}

void loop(void)
{
  if(digitalRead(pinButton) == HIGH){
   x = x + 1 ;
  }
  else{
    x = x;
  }
  
  P.print(x);

}

Have any ideas why ?
(I’ve checked wiring)

Source: Windows Questions C++

LEAVE A COMMENT