//Termometer y clock
#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 64 // OLED display height, in pixels
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET 4 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
int tempRead;
float temp, temp_F;
int tiempo = 1234;
int seg = 0;
int mins = 0;
int hours = 0;
unsigned long prevmillis;
String unit;
String leido = "05:58,C";
void setup() {
Serial.begin(9600);
// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3D)) { // Address 0x3D for 128x64
Serial.println(F("SSD1306 allocation failed"));
for(;;); // Don't proceed, loop forever
}
unit = leido.substring(6, 7);
Serial.println(unit);
hours = leido.toInt();
Serial.println(hours);
leido.remove(0, 3);
mins = leido.toInt();
Serial.println(mins);
}
void loop() {
tempRead = analogRead(A0);
temp = tempRead * 30 / 1023.0; //Mapear valores leidos de 0 a 15
temp_F = (temp * 1.8) + 32;
//Serial.println(temp);
display.display();
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(WHITE);
if (unit == "C")
{
//Mostrar °C
display.setCursor(25, 36);
display.println(temp, 2);
display.drawChar(92, 36, 0xF7, WHITE, BLACK, 1);
display.setCursor(100, 36);
display.println("C");
}
else if (unit == "F")
{
//Mostrar °F
display.setCursor(25,36);
display.println(temp_F, 2);
display.drawChar(92, 36, 0xF7, WHITE, BLACK, 1);
display.setCursor(100, 36);
display.println("F");
}
if (millis() - prevmillis >= 1000)
{
prevmillis = millis();
seg++;
if (seg == 60) {seg = 0; mins++;}
if (mins == 60) {mins = 0; hours++;}
if (hours == 24) {hours = 0;}
}
display.setTextSize(2);
display.setCursor(15,10);
if (hours < 10) display.print("0");
display.println(hours);
display.setCursor(36,10);
display.print(":");
display.setCursor(45,10);
if (mins < 10) display.print("0");
display.println(mins);
display.setCursor(66,10);
display.print(":");
display.setCursor(75,10);
if (seg < 10) display.print("0");
display.println(seg);
/*display.setCursor(100,10);
display.println("AM");*/
}
/*//Termometer y clock v1.0
#include <SoftwareSerial.h>
SoftwareSerial BT(6, 7); //RX, TX
#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 64 // OLED display height, in pixels
// Declaration for SSD1306 display connected using software SPI (default case):
#define OLED_MOSI 9
#define OLED_CLK 10
#define OLED_DC 11
#define OLED_CS 12
#define OLED_RESET 13
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT,
OLED_MOSI, OLED_CLK, OLED_DC, OLED_RESET, OLED_CS);
int read;
int hours = 0;
int mins = 0;
int seg = 0;
float temp, temp_F, mV;
String leido;
String unit;
unsigned long prevmillis;
void setup() {
Serial.begin(9600);
// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
if(!display.begin(SSD1306_SWITCHCAPVCC))
{
Serial.println(F("SSD1306 allocation failed"));
for(;;); // Don't proceed, loop forever
}
BT.begin(9600);
display.display();
display.clearDisplay();
}
void loop() {
read = analogRead(A0);
mV = read * 5000.0 / 1023;
temp = mV / 10;
temp_F = (temp * 1.8) + 32;
display.display();
display.setTextSize(2);
display.setTextColor(WHITE);
if (BT.available())
{
leido = BT.readString();
unit = leido.substring(6, 7);
hours = leido.toInt();
leido.remove(0, 3);
mins = leido.toInt();
}
if (millis() - prevmillis >= 1000)
{
prevmillis = millis();
display.clearDisplay();
//Clock
seg++;
if (seg == 60) {seg = 0; mins++;}
if (mins == 60) {mins = 0; hours++;}
if (hours == 24) {hours = 0;}
if (unit == "C")
{
//Mostrar °C
display.setCursor(25, 36);
display.println(temp, 2);
display.drawChar(92, 36, 0xF7, WHITE, BLACK, 1);
display.setCursor(100, 36);
display.println("C");
}
else if (unit == "F")
{
//Mostrar °F
display.setCursor(25,36);
display.println(temp_F, 2);
display.drawChar(92, 36, 0xF7, WHITE, BLACK, 1);
display.setCursor(100, 36);
display.println("F");
}
}
//Mostrar Clock
display.setTextSize(2);
//Horas
display.setCursor(15,10);
if (hours < 10) display.print("0");
display.println(hours);
display.setCursor(36,10);
display.print(":");
//Minutos
display.setCursor(45,10);
if (mins < 10) display.print("0");
display.println(mins);
display.setCursor(66,10);
display.print(":");
//Segundos
display.setCursor(75,10);
if (seg < 10) display.print("0");
display.println(seg);
}*/