Oled ssh1106 with Arduino

hey guys i will connect oled ssh1106 with oxygen and pressure sensor to show the percent and i made 6 pages to show it with delay but the PROBLEM that i want to control the pages with 3 buttons ( up , down ,and select To navigate between pages and the first page will be data and clock ) if anyone help me with or modify it and here is the far i could go

#include <Wire.h>
#include <virtuabotixRTC.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SH1106.h>

const unsigned char logo[] PROGMEM = {
   
};

virtuabotixRTC clock(3, 4, 2);
Adafruit_SH1106 display(-1);
int state = 1;
int currentPage = 1;

void setup()
{
    pinMode(5, INPUT_PULLUP);
    display.begin(SH1106_SWITCHCAPVCC, 0x3C);
    display.setTextColor(WHITE);

    display.drawBitmap(0, 0, logo, 128, 64, WHITE);
    display.display();
    delay(2000);

    drawUI();
}

void loop()
{
    if (digitalRead(5) == LOW)
    {
        if (state == 0)
        {
            delay(500);
            drawUI();
            state = 1;
        }
        else
        {
            display.drawBitmap(0, 0, logo, 128, 64, WHITE);
            display.display();
            delay(2000);
            state = 0;
            currentPage = 1;
        }
    }

    if (state == 1)
    {
        clock.updateTime();
        drawUI();
        displayData(currentPage);
        display.display();
        delay(10000);
        currentPage = (currentPage % 5) + 1;
    }
}

void drawUI()
{
    display.clearDisplay();
    display.setTextSize(1);
    display.setCursor(2, 2);
    display.println("  Clock Sleep");
    display.drawLine(0, 11, 128, 11, WHITE);
    display.drawLine(94, 0, 94, 10, WHITE);
    display.drawLine(26, 44, 102, 44, WHITE);
}

void displayData(int page)
{
    switch (page)
    {
case 1:
   
    display.setTextSize(1);
    display.setCursor(2, 20);
    display.print("O2:");
    display.setCursor(40, 20);
    display.print("70%"); 

    display.setCursor(2, 35); 
    display.print("Flow:");
    display.setCursor(40, 35);
    display.print("5 L/min"); 

    display.setCursor(2, 50); 
    display.print("Pressure:");
    display.setCursor(50, 50);
    display.print("  10 cmH2O"); 
    break;

    case 2:
       
        display.setTextSize(1);
        display.setCursor(2, 20);
        display.print("O2:");
        display.setCursor(40, 20);
        display.print("80%"); 
        display.setCursor(2, 48);
        display.print("Flow:");
        display.setCursor(40, 48);
        display.print("5 L/min"); 
        break;

    case 3:
        
        display.setTextSize(1);
        display.setCursor(2, 20);
        display.print("Flow:");
        display.setCursor(40, 20);
        display.print("8 L/min"); 
        display.setCursor(2, 48);
        display.print("O2:");
        display.setCursor(40, 48);
        display.print("70%"); 
        break;

    case 4:
        // Display pressure in cm H2O
        display.setTextSize(1);
        display.setCursor(2, 34);
        display.print("Pressure:");
        display.setCursor(80, 34);
        display.print("cmH2O");
        break;

    case 5:
        // Display pressure in mmHg
        display.setTextSize(1);
        display.setCursor(2, 34);
        display.print("Pressure:");
        display.setCursor(80, 34);
        display.print("mmHg"); 
        break;
    }
}

What is this code doing? It looks like a one button version.

byte buttonpin[] = {2, 4, 7}; // declare button pins

void setup() {
  Serial.begin(9600); // start serial monitor
  for (int i = 0; i < 3; i++)
    pinMode(buttonpin[i], INPUT_PULLUP); // configure button pins (LOW when pressed)
}

void loop() {
  for (int i = 0; i < 3; i++) {
    if (!digitalRead(buttonpin[i])) { // if a button was pressed
      Serial.print(buttonpin[i]); // print the pin number <-- This is where you move UP, DOWN, SELECT
      delay(150); // debounce delay
    }
  }
}

this is more like testing if it is currently being hold pressed rather than was pressed. you'll get a "shotgun effect" if you don't track state changes (ie previously was not pressed and now is pressed ➜ then it's a press)

This line is intended for "future enhancements"... Yes, this is a strawman. I would not use delay() for the final project either... nor green buttons. : |.. @mohameddd123 probably will discover what is best.

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