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;
}
}