Please help Simple code for Push to text for OLED i2c

Hey if anyone can help me that would be super! I just want to know if anybody can help me can rearrange this code so it will NOT toggle but show the word “Danger” on the OLED when pinbutton 2 has been connected to ground and have the OLED go blank once pinbutton 2 has no connection to ground. But if you cant rearrange this code to work can you just give me a simple code that will make words appear momentarily on a oled i2c when a push button has been pressed?

heres the code:

// Adafruit-GFX-Library - Version: Latest
#include <Adafruit_GFX.h>
#include <Adafruit_SPITFT.h>
#include <Adafruit_SPITFT_Macros.h>
#include <gfxfont.h>

// Adafruit_SSD1306 - Version: Latest
#include <Adafruit_SSD1306.h>

int sensorPin = A0; // select the input pin for ldr
int sensorValue = 0;
bool toggle = false;
int buttonpin;

#define OLED_RESET 4 // not used / nicht genutzt bei diesem Display
Adafruit_SSD1306 display(OLED_RESET);

char inChar;
String string;

void setup() {

pinMode(13, OUTPUT);

buttonpin = 2;
pinMode(buttonpin, INPUT_PULLUP);
// initialize with the I2C addr 0x3C / mit I2C-Adresse 0x3c initialisieren
display.begin(SSD1306_SWITCHCAPVCC, 0x3c);

Serial.begin(9600);
display.clearDisplay();
display.display();
delay(2000);
display.clearDisplay();

display.setTextColor(INVERSE);
}

void loop()
{
if (digitalRead(buttonpin) == LOW)
{
toggle = !toggle;
while(digitalRead(buttonpin) == LOW);
}
switch( toggle )
{

case 1:
display.clearDisplay();
sensorValue = analogRead(sensorPin);
Serial.println(sensorValue);
display.setCursor(41,20);
display.setTextSize(1);
display.print("");
display.setCursor(32,20);
display.setTextSize(0);
display.print(“D a n g e r”);
delay(500);

break;

case 0:
display.clearDisplay();

break;
}

display.display();

}

// Adafruit-GFX-Library - Version: Latest
#include <Adafruit_GFX.h>
#include <Adafruit_SPITFT.h>
#include <Adafruit_SPITFT_Macros.h>
#include <gfxfont.h>

// Adafruit_SSD1306 - Version: Latest
#include <Adafruit_SSD1306.h>

const byte buttonpin = 2;

const byte OLED_RESET = 4; // not used / nicht genutzt bei diesem Display
Adafruit_SSD1306 display(OLED_RESET);

void setup()   
{
  pinMode(13, OUTPUT);

  pinMode(buttonpin, INPUT_PULLUP);
  // initialize with the I2C addr 0x3C / mit I2C-Adresse 0x3c initialisieren
  display.begin(SSD1306_SWITCHCAPVCC, 0x3c);

  Serial.begin(9600);
  display.clearDisplay();
  display.display();
  delay(2000);
  display.clearDisplay();

  display.setTextColor(INVERSE);
}

void loop()
{
     if (digitalRead(buttonpin) == LOW)
     {
       display.clearDisplay();
       display.setCursor(41,20);
       display.setTextSize(1);
       display.print("");
       display.setCursor(32,20);
       display.setTextSize(0);
       display.print("D a n g e r");
     }
     else
     {
       display.clearDisplay();
     }
       
     display.display();
}

I haven’t compiled or tested this code, but I did put it in code tags.

IT WORKED THANKS A BUNCH!! :smiley:

i have a similar problem,

i have a setup with 3 buttons 4 outputs

i have an Oled display to display text when button is pressed

when i enter code for just 1 button text displays fine untill i release the button

when i enter the rest of the code for to other buttons the text starts to flash on the display

what am i dooing wrong or missing ??

void setup() {
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for (;;);
}
delay(500); // Pause for 2 seconds

// Clear the buffer.
display.clearDisplay();

// Draw bitmap on the screen
display.drawBitmap(0, 0, sikistein, 128, 64, 1);
display.display();

delay(2000);
display.clearDisplay();
display.display();

display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(0, 0);
display.println("Welcome SikiStein");
display.display();

delay(2000);
display.clearDisplay();
display.display();

//configure pin 2 , 4 , 7 as an input and enable the internal pull-up resistor
pinMode(2, INPUT_PULLUP);
pinMode(3, OUTPUT);
pinMode(4, INPUT_PULLUP);
pinMode(5, OUTPUT);
pinMode(7, INPUT_PULLUP);
pinMode(6, OUTPUT);
pinMode(9, OUTPUT);

display.setTextSize(3);
display.setTextColor(WHITE);
}

void loop() {

//read the pushbutton value into a variable
int sensorVal = digitalRead(2);
int sensorVal2 = digitalRead(4);
int sensorVal3 = digitalRead(7);

if (sensorVal == HIGH) { // clutch
digitalWrite(3, HIGH );
} else {
digitalWrite(3, LOW);
}
if (sensorVal2 == HIGH) { //e-brake
digitalWrite(5, HIGH );
} else {
digitalWrite(5, LOW);
}
if (sensorVal3 == HIGH) { //front brake
digitalWrite(6, HIGH );
} else {
digitalWrite(6, LOW);
}
if (sensorVal == HIGH && sensorVal2 == HIGH && sensorVal3 == HIGH) { //pomp motor
digitalWrite(9, HIGH );
} else {
digitalWrite(9, LOW);
}
if (sensorVal == LOW) {
display.setCursor(0, 0);
display.println("Clutch ");
display.display();
} else {
display.clearDisplay();
}
if (sensorVal2 == LOW) {
display.setCursor(0, 0);
display.println("E-Brake ");
display.display();
} else {
display.clearDisplay();
}
if (sensorVal3 == LOW) {
display.setCursor(0, 0);
display.println("Front Brake ");
display.display();
} else {
display.clearDisplay();
}
display.display();
}

If one of your sensors is LOW, you display the message but your other two if() statements to test the other inputs for LOW are false so the else() portion executes, which clears the display. If you only want one of those messages, you should make it a series of if/else if/ statements rather than independent if() statements.

You should also post your code using code tags as it tells you in the sticky post at the top of the forum.

thnx for the fast reply,

i got it done and working

just a small thing can keep you up all night !!

just made it a long string of code by removing all the } inbetween code and move them

to the end of the code

display.display();
}
}
}