Go Down

Topic: PROBLEM WITH OLED AND TWO BUTTONS  (Read 376 times) previous topic - next topic

marioval97

Hello, here i bring to you my problem ( first at all, i am beginner and i have no a large knowledge of arduino):


i tried to use a OLED 128x64 with some buttons. I can use both perfectly when i code the sketch for one button ( some text in the OLED and at the same time switch on/off a LED).

The problem comes when i try to code a sketch with two button cause the OLED doesn't switch on!

i don't know how to code it. If someone could help me, i would be glad!

GrooveFlotilla

The way it works here is, you post your code (in code tags) and people help you correct it.
Some people are like Slinkies.

Not really good for anything, but they bring a smile to your face when pushed down the stairs.

marioval97

sorry, this is the first time i post in a forum:

i use the Adafruit libraries ( GFX and SSD1306)

Here is the code i used in order to set the first button and OLED/led at the same time ----->

//--------------------------------------------------------------------------//

void loop() {
if ( digitalRead (BUTTON_1==LOW){
  display.clearDisplay();
  display.setTextSize(2);
  display.setTextColor(WHITE);
  display.setCursor(0,0);
  display.print("BLUE LEDs SET");
  display.display();
  digitalWrite (BLUE, HIGH);
  delay(5000);
 }
else {
  digitalWrite (BLUE, LOW);
     display.clearDisplay();
  display.setTextSize(2;
  display.setTextColor(WHITE);
  display.setCursor(0,0);
  display.print("PRESS 1 SET BLUE");
 
  display.display();
}

}
//--------------------------------------------------------------------------//



this is the code i used in order to set the both buttons and the OLED doesn't work :----->



//--------------------------------------------------------------------------//


void loop() {
if (digitalRead(BUTTON_1)==LOW||digitalRead(BUTTON_2)==LOW){
if ( digitalRead (BUTTON_1)==LOW && digitalRead(BUTTON_2)==HIGH){
  display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(20,31);
  display.print("BLUE LEDs SET");
  display.display();
  digitalWrite (BLUE, HIGH);
  delay(5000);
}
if ( digitalRead (BUTTON_2)==LOW && digitalRead (BUTTON_1)==HIGH) {
     display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(20,31);
  display.print("WHITE LEDs SET");
    display.display();
    digitalWrite(WHITE,HIGH);
    delay(5000);
}
else{
   digitalWrite (BLUE, LOW);
   digitalWrite (WHITE,LOW);
    display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(0,15);
  display.print("PRESS 1 SET BLUE");
  display.setCursor(0,35);
   display.print("PRESS 2 SET WHITE");
display.display();
 
}}

//--------------------------------------------------------------------------//

if anyone could help me to optimize the sketch it would be welcome!

GrooveFlotilla

#3
Mar 13, 2018, 08:08 pm Last Edit: Mar 13, 2018, 08:08 pm by GrooveFlotilla
The way it works here is, you post ALL your code (in code tags) and people help you correct it.
Some people are like Slinkies.

Not really good for anything, but they bring a smile to your face when pushed down the stairs.

marioval97

Code: [Select]
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Wire.h>

 #define OLED 0x3C
 Adafruit_SSD1306 display(-1);
 #define BUTTON_1 0
 #define BLUE 1
 #define BUTTON_2 13
 #define WHITE 12
void setup() {
  //SET DISPLAY
  display.begin(SSD1306_SWITCHCAPVCC, OLED);

  //VARIABLES I/O
  pinMode (BUTTON_1,INPUT);
  pinMode (BLUE, OUTPUT);
  pinMode (BUTTON_2, INPUT);
  pinMode (WHITE, OUTPUT);

 digitalWrite(BLUE, LOW);
 digitalWrite (WHITE, LOW);

   display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(0,15);
  display.print("PRESS 1 SET BLUE");
  display.setCursor(0,35);
   display.print("PRESS 2 SET WHITE");
   display.setCursor(0,40);
 display.print("PRESS 3 SET RED");
display.display();
}
void loop() {
if (digitalRead(BUTTON_1)==LOW||digitalRead(BUTTON_2)==LOW){
if ( digitalRead (BUTTON_1)==LOW && digitalRead(BUTTON_2)==HIGH){
  display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(20,31);
  display.print("BLUE LEDs SET");
  display.display();
  digitalWrite (BLUE, HIGH);
  delay(5000);
}

if ( digitalRead (BUTTON_2)==LOW && digitalRead (BUTTON_1)==HIGH) {
     display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(20,31);
  display.print("WHITE LEDs SET");
    display.display();
    digitalWrite(WHITE,HIGH);
    delay(5000);
}


else{
   digitalWrite (BLUE, LOW);
   digitalWrite (WHITE,LOW);
    display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(0,15);
  display.print("PRESS 1 SET BLUE");
  display.setCursor(0,35);
   display.print("PRESS 2 SET WHITE");
   display.setCursor(0,40);
 display.print("PRESS 3 SET RED");
display.display();
 
}}

dave-in-nj

comment out all those IF statements and only post your display code.  make sure loop() works with just the display.

Add only one IF() back at a time and pay very close attention to ending brackets
looks like your first line bracket does not close till the end.


I would recommend that you auto-format to get the IF() statements in line.

marioval97

I tried it, comment out all the IF(), but the OLED still doesn't work!

When i try to define only one button it Works perfectly, but when i define the second one, the OLED switch off ....

dave-in-nj

post your code after you commented out the other bits.

Did you fix your mistake that button 2 has to be  both high and low ?
and did you fix the missing end bracket in loop ?

try this : 
use your above code
remove loop()
replace it with below code
it should just blink back and forth between colors.

then add one button at a time.


Code: [Select]


void loop {
    display.clearDisplay();
    display.setTextSize(1);
    display.setTextColor(WHITE);
    display.setCursor(20,31);
    display.print("WHITE LEDs SET");
    display.display();
    digitalWrite(WHITE,HIGH);
delay(2000);
    digitalWrite (BLUE, LOW);
    digitalWrite (WHITE,LOW);
    display.clearDisplay();
    display.setTextSize(1);
    display.setTextColor(WHITE);
    display.setCursor(0,15);
    display.print("PRESS 1 SET BLUE");
    display.setCursor(0,35);
    display.print("PRESS 2 SET WHITE");
    display.setCursor(0,40);
    display.print("PRESS 3 SET RED");
    display.display();
 delay(2000);
} // end of loop




marioval97

yes, i used what you told to me and now it Works! Thank you bro!!!!


Go Up