Help for showing multi text via botton in ssd1306 display

hi guys i need help for some edit to my sketch
I need show word (HOW)on display with the first click on the button , with the next click on the key of the word (ARE)with the next click (YOU)will be displayed with the next click (TODAY) and with the next click this cycle will start again from (HOW)

any one can help me for add this 4 word Separately in my sketch?

this is my sketch
thanks

#include <Adafruit_GFX.h>
#include <gfxfont.h>


#include <Adafruit_SSD1306.h>

const byte buttonpin = 8;

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("HOW ARE YOU TODAY");
     }
     else
     {
       display.clearDisplay();
     }
       
     display.display();
}


Take a look at the StateChangeDetection example in the IDE. It will show you how to detect when a button becomes pressed which allows a variable to be incremented each time

Use the value of the variable to control what is printed. For bonus points put the text in an array and use the button press counter as an index to the array of words thus making the code much shorter

thanks.Unfortunately I did not understand the StateChangeDetection example
Can you look at my code and edit it?

I could, but I am not going to. You will learn more by doing it yourself

read the button input
has it changed since last time ?
if it has changed is the button now pressed ?
if yes then it has become pressed so increment a counter and take action based on its value
save the current state as the previous state ready for the next test
keep going round this loop

This is very simple and understandable for you. :grinning:

I searched a lot on the net but did not find the right result
If anyone can please take a look at my sketch and modify it
So I can use it as an example
thanks

Start with the example I pointed you to. The value of buttonPushCounter is printed each time it is incremented.
If ithe value is 1 output word 1, if it is 2 then output word 2 etc

Come on. You can do it

2 Likes

Unfortunately I tried but could not use this example :sleepy:

I am not very familiar with arduino coding

The output for the StateChangeDetection example looks like this

on
number of button pushes: 1
off
on
number of button pushes: 2
off
on
number of button pushes: 3
off
on
number of button pushes: 4

Write some code along the lines of

if (buttonPushCounter == 1)
{
  //code here to print word HOW
}

and so on

1 Like

i put full my effort but i cant be successfull
i have 4 kinds of mode in my project
i want showing the word mode1 at the first on display without press any button
by pressing buttonPin = 8; mode 1 replace to mode 2 and by clicking again change to mode 3 and 4 and by clicking again turn back to mode 1
please edit my sKetch I am not very familiar with arduino coding
thanks
this is my scetch

#include <Adafruit_GFX.h>
#include <gfxfont.h>
#include <Adafruit_SH1106.h>



const int  buttonPin = 8;    // the pin that the pushbutton is attached to
const int ledPin = 13;       // the pin that the LED is attached to

// Variables will change:
int buttonPushCounter = 0;   // counter for the number of button presses
int buttonState = 0;         // current state of the button
int lastButtonState = 4;     // previous state of the button

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

void setup() {

  pinMode(buttonPin, INPUT);
  pinMode(ledPin, OUTPUT);
  
  display.begin(SH1106_SWITCHCAPVCC, 0x3c);
  Serial.begin(9600);
  display.clearDisplay();
  display.display();
  delay(2000);
  display.clearDisplay();
  display.setTextColor(INVERSE);
}


void loop() {
  // read the pushbutton input pin:
  buttonState = digitalRead(buttonPin);

  // compare the buttonState to its previous state
  if (buttonState != lastButtonState) {
    // if the state has changed, increment the counter
    
if (buttonPushCounter == 1) {
      buttonPushCounter++;
      {
       display.clearDisplay();
       display.setCursor(32,20);
       display.setTextSize(0);
       display.print("MODE1");
       }
    } else {
      

    }
    // Delay a little bit to avoid bouncing
    delay(50);
  }
  // save the current state as the last state, for next time through the loop
  lastButtonState = buttonState;


  // turns on the LED every four button pushes by checking the modulo of the
  // button push counter. the modulo function gives you the remainder of the
  // division of two numbers:
  if (buttonPushCounter % 4 == 0) {
    digitalWrite(ledPin, HIGH);
  } else {
    digitalWrite(ledPin, LOW);
  }

}

Does your code print "MODE1" when buttonPushCounter equals 1 ?

1 Like

No .Mode 1 is displayed on the screen SH1106 without hitting any key. With the first click, Mode 1 is deleted and Mode 2 is displayed. With the next click, Mode 2 is deleted and Mode 3 is displayed. With the next click, Mode 3 is deleted and Mode 4 is displayed. with the next click,again Mode 1 is displayed in sh1106. And this cycle continues

Your code in post #11 does not do what you describe. For instance it never prints anything except "MODE1"

Did you post the correct code ?

Yes this code is wrong

Please post the code that produces the results that you described in reply #13

Unfortunately, I do not know much about Ardiono coding
And I could only write code here

#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 32 // OLED display height, in pixels



// this constant won't change:
const int  buttonPin = 2;    // the pin that the pushbutton is attached to
const int ledPin = 13;       // the pin that the LED is attached to

const byte OLED_RESET = 4; // not used / nicht genutzt bei diesem Display

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);


// Variables will change:
int buttonPushCounter = 0;   // counter for the number of button presses
int buttonState = 0;         // current state of the button
int lastButtonState = 0;     // previous state of the button

void setup() {
  // initialize the button pin as a input:
  pinMode(buttonPin, INPUT);
  // initialize the LED as an output:
  pinMode(ledPin, OUTPUT);
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C); //Start the OLED display
 
  display.clearDisplay(); 
  display.display();
  display.setTextSize(2);
  display.setTextColor(WHITE);
  display.setCursor(30,0);
  display.println("WELCOME");
  display.display();
}


void loop() {
 
}

We seem to be getting nowhere.

You have a sketch that prints the value of the button press counter and you have a section of code that tests the value and if it equals 1 then it prints "MODE1"

Add more tests and for each value of the button press counter print what you want for that value

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