# Program with "switch case" function, with a switch, and displayed on a OLED

**URL:** <https://forum.arduino.cc/t/program-with-switch-case-function-with-a-switch-and-displayed-on-a-oled/671606>\
**Category:** Programming\
**Created:** [August 24, 2020, 6:00pm UTC](https://forum.arduino.cc/t/program-with-switch-case-function-with-a-switch-and-displayed-on-a-oled/671606 "2020-08-24T18:00:27Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![albbert123](https://avatars.discourse-cdn.com/v4/letter/a/6de8d8/32.png) [@albbert123](https://forum.arduino.cc/u/albbert123)\
**Post date:** [August 24, 2020, 6:00pm UTC](https://forum.arduino.cc/t/program-with-switch-case-function-with-a-switch-and-displayed-on-a-oled/671606/1 "2020-08-24T18:00:27Z")

</div>

Hello everyone! I'm spanish so sorry for my english :-/.

I'm doing a Smartwatch for my school project, and I want to change the things displayed on the screen each time I press the switch.

I used the function "switch case" but I have done something wrong. The program compilated and upload it correctly, but when I press the switch the screen doesn't change, it stops.

I'm using the arduino internal resistor (pull-up), a sensor board GY-87 and a OLED 128x64 I2C.

The code:

#include \<SPI.h\>  
#include \<Adafruit\_SSD1306.h\>  
#include \<Adafruit\_GFX.h\>  
#include \<Wire.h\> // librerias  
#include \<SPI.h\>  
#include \<TimeLib.h\>  
#include \<SFE\_BMP180.h\>

#define SCREEN\_WIDTH 128 // OLED display width, in pixels  
#define SCREEN\_HEIGHT 64 // OLED display height, in pixels

// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins) // screen  
#define OLED\_RESET 4 // Reset pin #  
Adafruit\_SSD1306 display(SCREEN\_WIDTH, SCREEN\_HEIGHT, &Wire, OLED\_RESET);

int pulsador = 0; // switch  
int estadoMenu = 0; // status menu

SFE\_BMP180 pressure; //sensor

#define ALTITUDE 20.0

void setup() {  
Serial.begin(9600);  
//Check if the dipslay starts correctly  
if (display.begin(SSD1306\_SWITCHCAPVCC, 0x3C))  
Serial.println("SSD1306 allocation done!");  
else  
{  
Serial.println("init fail\n\n");  
}  
//Check if sensor starts correctly  
if (pressure.begin())  
Serial.println("BMP180 init success");  
else  
{  
Serial.println("BMP180 init fail\n\n");  
}  
delay(2000);

setTime(19, 28, 50, 20, 10, 2015);

pulsador = 2; //switch pin 2  
pinMode(2, INPUT\_PULLUP); //internal resistor pull up

}

void loop()  
{  
//loop data definition  
String screenText;  
char status;  
double T,P,p0,a;

//display data definition  
display.clearDisplay();  
display.setTextSize(1); // Normal 1:1 pixel scale  
display.setTextColor(WHITE); // Draw white text

if (digitalRead(2)== 1) {  
estadoMenu++;  
if (estadoMenu == 4) {  
estadoMenu = 0; // from 0 to 3  
}  
switch (estadoMenu){  
case 0:  
display.setCursor(10, 32);

time\_t t = now();//Declaramos la variable time\_t t

//Imprimimos la fecha y lahora  
display.print(day(t));  
display.print(+ "/") ;  
display.print(month(t));  
display.print(+ "/") ;  
display.print(year(t));  
display.print( " ") ;  
display.print(hour(t));  
display.print(+ ":") ;  
display.print(minute(t));  
display.print(":") ;  
display.println(second(t));  
delay(1000);//Esperamos 1 segundo

display.print("\n");  
display.display();

break;

case 1:  
display.setCursor(10, 32);

//start temperature data retriving  
status = pressure.startTemperature();

if (status != 0)  
{  
delay(status);  
status = pressure.getTemperature(T);  
display.print("temp: ");  
display.print(T,2);  
display.println(" deg C");  
delay (1000);

display.display();  
}

break;

case 2:  
display.setCursor(10, 32);

if (status != 0)  
{  
//get pressure data  
delay(status);  
status = pressure.getPressure(P,T);  
display.print("abs p.: ");  
display.print(P);  
display.print(" mb ");  
display.print("\n");

//get sea level pressure data  
p0 = pressure.sealevel(P,ALTITUDE); // we're at 1655 meters (Boulder, CO)  
display.print("rel p.: ");  
display.print(p0,2);  
display.print(" mb ");  
display.print("\n");

display.display();  
}  
break;

case 3:  
display.setCursor(10, 32);

if (status != 0)  
{  
//get altitude data  
a = pressure.altitude(P,p0);  
display.print("alt: ");  
display.print(a,0);  
display.print(" meters ");  
display.print("\n");

display.display();  
}  
}  
}

//read the pushbutton value into a variable  
int sensorVal = digitalRead(2);  
//print out the value of the pushbutton  
Serial.println(sensorVal);  
}

---

<div class="post-metadata">

**Author:** ![ToddL1962](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/toddl1962/32/225060_2.png) [@ToddL1962](https://forum.arduino.cc/u/ToddL1962)\
**Post date:** [August 24, 2020, 6:35pm UTC](https://forum.arduino.cc/t/program-with-switch-case-function-with-a-switch-and-displayed-on-a-oled/671606/2 "2020-08-24T18:35:02Z")

</div>

You should post your code according to [Read this before posting a programming question ...](https://forum.arduino.cc/index.php?topic=97455.0).

I see a few issues:

1. You should really use LOW and HIGH when comparing to a digitalRead
2. If you are using the internal pullup resistor then your button should be wired to GND
3. The normal state of a button that is not depressed is HIGH if you are using a pullup. Therefore you should be checking for a HIGH to LOW transition on the button

This link might be helpful: [StateChangeDetection](https://www.arduino.cc/en/Tutorial/StateChangeDetection)

---

<div class="post-metadata">

**Author:** ![albbert123](https://avatars.discourse-cdn.com/v4/letter/a/6de8d8/32.png) [@albbert123](https://forum.arduino.cc/u/albbert123)\
**Post date:** [August 24, 2020, 11:43pm UTC](https://forum.arduino.cc/t/program-with-switch-case-function-with-a-switch-and-displayed-on-a-oled/671606/3 "2020-08-24T23:43:37Z")

</div>

Ups! Sorry, I will read it!

1. When I use "_digitalRead_" I must use HIGH/LOW? If not it is incorrect 0/1? So this line " _if (digitalRead(2)== 1) {_" it has to change to " _if (digitalRead(2)== HIGH) {_ " ?

2. Yes! I connected one cable to GND and the other to the pin 2 (D2 in Arduino NANO).

3. I am checking the transition in the monitor serie of Arduino IDE; when the button is not depressed prints 1 and displayed in the OLED the case 0, the time. But when I press the botton the serial prints 0, and time stops.

In the code I put when the button was pressed, add one (++) to the menu status, I don't understant why it doesn't work.

Thanks! I will read this page too!

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [May 6, 2021, 12:43am UTC](https://forum.arduino.cc/t/program-with-switch-case-function-with-a-switch-and-displayed-on-a-oled/671606/4 "2021-05-06T00:43:11Z")

</div>

Closed on May 6, 2021, 12:43am UTC


