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

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

You should post your code according to Read this before posting a programming question ....

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

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!