Push Button in constant state of depression and won't work

I have GND and D2 wire hooked up and the button hasnt been working. It was working this morning but idk what happened or remember how I had it. I think the code is fine? but idk. I know the loop part is okay. I'm just having trouble getting it to read HIGH. Thank you in advanced.

#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels

// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
// The pins for I2C are defined by the Wire-library. 
// On an arduino UNO:       A4(SDA), A5(SCL)
// On an arduino MEGA 2560: 20(SDA), 21(SCL)
// On an arduino LEONARDO:   2(SDA),  3(SCL), ...
#define OLED_RESET     -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

long randNumber;

//BUTTON SET UP
 const int button = 2;
 int var = 0;
void setup() {
  Serial.begin(9600);
pinMode(button, INPUT_PULLUP);digitalWrite(button, HIGH);
  // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
  if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
    Serial.println(F("SSD1306 allocation failed"));
    for(;;); // Don't proceed, loop forever
  }

  
   randomSeed(analogRead(A0));

  
}

void loop(){
 int var = digitalRead(button);
  if (var == LOW)
  {
        Serial.print("PRESS ME!");
       
  display.clearDisplay();

  display.setTextSize(3);
  display.setTextColor(WHITE);
  display.setCursor(20, 20);
  // Display static text
  display.println("PRESS ME!");
  display.display(); 
  display.display();

 return 0;
  }

  else {
 randNumber = random(1,4);

  // Show initial display buffer contents on the screen --
  // the library initializes this with an Adafruit splash screen.
  if (randNumber == 2){

   Serial.print("YES");
    delay(1000);
  display.clearDisplay();

  display.setTextSize(5);
  display.setTextColor(WHITE);
  display.setCursor(23, 17);
  // Display static text
  display.println("YES");
  display.display(); 
  display.display();
  delay(2000); // Pause for 2 seconds
return 0;

  }
  if (randNumber == 3) {

    Serial.print("NO");
        delay(1000);
  display.clearDisplay();

  display.setTextSize(5);
  display.setTextColor(WHITE);
  display.setCursor(40, 17);
  // Display static text
  display.println("NO");
  display.display(); 
  display.display();
  delay(2000); // Pause for 2 seconds

  
  }
  else {
 randNumber = random(1,3);

 if (randNumber == 3){
      Serial.print("YES");
        delay(2000);
  display.clearDisplay();

  display.setTextSize(5);
  display.setTextColor(WHITE);
  display.setCursor(23, 17);
  // Display static text
  display.println("YES");
  display.display(); 
  display.display();
  delay(2000); // Pause for 2 seconds
 return 0;
  }
 }
    if (randNumber == 1){
          Serial.print("NO");
        delay(1000);
  display.clearDisplay();

  display.setTextSize(5);
  display.setTextColor(WHITE);
  display.setCursor(40, 17);
  // Display static text
  display.println("NO");
  display.display(); 
  display.display();
  delay(2000); // Pause for 2 seconds

  }

  else {
        Serial.print("YES");
        delay(1000);
  display.clearDisplay();

  display.setTextSize(5);
  display.setTextColor(WHITE);
  display.setCursor(23, 17);
  // Display static text
  display.println("YES");
  display.display(); 
  display.display();
  delay(2000); // Pause for 2 seconds
 return 0;
  }
    
    Serial.print("LMAO");
        delay(2000);
  display.clearDisplay();

  display.setTextSize(5);
  display.setTextColor(WHITE);
  display.setCursor(8, 17);
  // Display static text
  display.println("LMAO");
  display.display(); 
  display.display();
  delay(2000); // Pause for 2 seconds
 return 0;
  }
}

Explain.

Pull the hookup wire from D2.  What happens?

If you wire to diagonal terminals you will avoid a short to ground. My guess without seeing a schematic or photo that shows the switch.

Never mind. I miswired it, lol. I'm back to the original problem with the float (I think?) where it just spits out answers if I'm not touching the button. Do you know how to help that?

Thanks for the help! I indeed did miswire it, haha.

I think you need to reverse the logic of your sketch. With INPUT_PULLUP mode, digitalRead() of the button will be LOW when pressed and HIGH when not pressed.

int var = digitalRead(button);
 // if (var == LOW)
if(var == HIGH)
  {
        Serial.print("PRESS ME!");
       
  display.clearDisplay();

  display.setTextSize(3);
  display.setTextColor(WHITE);
  display.setCursor(20, 20);
  // Display static text
  display.println("PRESS ME!");
  display.display(); 
  display.display();

 return 0;
  }

  else {  // what to do when button is pressed

In some cases, you can benefit from a custom definition:

const int PRESSED = LOW;
const int RELEASED = HIGH;
...
  if(var == PRESSED)

Hello
What´s the name of the game?
Have a nice day and enjoy coding in C++.

Kind of "belt and braces".

In earlier (ancient?) IDE, you actually used

  pinMode(button, INPUT);  digitalWrite(button, HIGH);

To implement INPUT_PULLUP.

We're not stuck on AVR architecture any more. That isn't portable.

Thanks! I just got started and it's so much fun. I started with an easier project and it's gonna be a snarky decision cube, haha.

Thank you so much! It worked!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.