Code problem button

Hello people, i have a problem with my code, all it`s working ok, with one exception:
on the last section, last lines:

      Serial.print("sometext");
      if (digitalRead(go) == LOW){     
      display.clearDisplay();
      display.display();
      delay(200);

It`s print on serial, but when i push button (go), nothing happend. This is my all code:

#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

#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);

int menu = 17;
int ok = 12;
int sel = 16;
int go = 21;

int q = 0;
int vok = 0;
int menulock = 0;

String inputString;

int i = 0;
int v = 0;
int j = 0;

void setup() {
  Serial.begin(9600);
  pinMode(menu, INPUT_PULLUP);
  pinMode(ok, INPUT_PULLUP);
  pinMode(sel, INPUT_PULLUP);
  pinMode(go, INPUT_PULLUP);
  if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
    Serial.println(F("SSD1306 allocation failed"));
    for(;;); // Don't proceed, loop forever
  }
  display.setTextSize(2);
  display.setTextColor(WHITE);
  display.clearDisplay();
  display.display();
  display.setCursor(10, 12);
  display.print("PASSWORD");
  display.display();
}

   void loop(){
   if (menulock == 0 && digitalRead(menu) == LOW){
    i++;
    if (i > 9){
      i = 0;
    }
   display.clearDisplay();
   display.setCursor(10, 12);
   display.print(i);
   display.display();
   delay(200);
 }
  if (vok < 4 && digitalRead(ok) == LOW){
      vok++;
      inputString += i;               // append new character to input string
      delay(400);
      display.setCursor(0, 10);
      display.clearDisplay();
      display.print(inputString);
      display.display();
      if (vok > 3){
        vok=4;
        menulock = 1;
      }
  }

  if (q == 0 && vok == 4 && menulock == 1 && digitalRead(sel) == LOW){
      q++;
      if (inputString != "1234"){      
      display.setTextSize(2);
      display.setCursor(25, 0);
      display.clearDisplay();
      display.print("invalid");
      display.display();
      delay(200);
      } else {
      Serial.print("sometext");
      if (digitalRead(go) == LOW){     
      display.clearDisplay();
      display.display();
      delay(200);
      }
     }
  }
   }

I use a raspberry pi pico.

Thank you very much for you`re help! :heartbeat:

It only does that Serial.print() if a group of conditions is met. Another condition (the go button) is required to do more. One of the intial conditions is q==0. The very fist time through, if it is met, q gets incremented so that if() will never be true again.

What exactly are you trying to do with this code?

Solved!

 if (vok == 4 && menulock == 1 && digitalRead(sel) == LOW){
      if (inputString != "1234"){      
      display.setTextSize(2);
      display.setCursor(25, 0);
      display.clearDisplay();
      display.print("invalid");
      display.display();
      delay(200);
      }
      } else {
      if (vok == 4 && menulock == 1 && digitalRead(sel) == LOW){  
      display.clearDisplay();
      display.display();
      delay(200);
      }
  }

How can this solve a problem? The conditions for your first if() are identical to the conditions of the if() inside the else clause... Granted, you do have delays and you are reading a pin state which could change, but this logic is very difficult to understand

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