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