Currently, I'm having trouble to locate which pixel to have the temperature, date, and time onto the OLED screen. I want to have the time at the left side of the screen and have the date and the time to be on the right side stacking on top of each other. I having trouble with the display.setCursor since I have tried multiple times to use different values and only am able to have the temperature on screen and not the date and times.
Here's the code I currently have,
#include <SPI.h> //i2c and the display libraries
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <DS3231.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 32 // OLED display height, in pixels
#define OLED_RESET 4 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); //Declaring the display name (display)
DS3231 clock;
RTCDateTime dt;
void setup() {
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); //Start the OLED display
clock.begin();
delay(1000);
display.clearDisplay();
display.display();
}
void loop() { //This code displays the Time( Hours:Minutes) as 12h format with (AM/PM) and Date(day of the week/Month/Day of the month)
//And the temperature in Fahrenheit
dt = clock.getDateTime();
display.clearDisplay();
display.setTextSize(3); //size of the text that will follow
display.setTextColor(WHITE); //its color
display.setCursor(0,0); //position from where you want to start writing
display.print(clock.dateFormat("g:i", dt)); //text todisplay
display.setTextSize(1);
display.setCursor(0,17);
display.print(clock.dateFormat("A", dt));
display.setCursor(20,0);
display.print(clock.dateFormat("D", dt));
display.setCursor(0,15);
display.print(clock.dateFormat("Mj", dt));
display.setCursor(70,15);
float f=(clock.readTemperature() * 1.8) + 32; //Temperature is read in C and we convert it to F
display.print(f,0);
display.print(" F");
display.display();
delay(1000);
}
display.setCursor(70,15);
float f=(clock.readTemperature() * 1.8) + 32; //Temperature is read in C and we convert it to F
display.print(f,0);
display.print(" F");
Once I removed the part you told me, my screen was blank. I'm not sure if it's because the setCursors I have in the other parts of the code are causing is to be off the screen.
I'm noticed that the temperature is not shown anymore, so this means that the module is not reading temperature anymore. When I was doing this project, I referenced:
I also noticed that I'm using an 128x64 screen and in the beginning of the code, I declared the the screen width is 32. I'll change that do 64 and see it it works.
There is a patched library provided by the poster Ikar4n. The patch doesn't do anything harmful (I've checked) and the bug itself seems to be a beginner's error: the functions return a pointer to a locally allocated buffer.
You could try using the patched library, with two caveats:
where there is one beginner's error, there are likely more,
the correction marks the buffer as static, thus reserving a whopping 255 bytes for this, now compile time allocated, memory; actually there are two dateFormat functions, another one for the alarm time, so if you used both that would be 255 * 2 reserved - IMO a bit wasteful, but maybe it is enough for you.
I'll check out the new patched library posted by IKar4n. I did actually used the original library that use posted and probably didn't noticed it has bugs. Thank you.
I saw the new patched, but I guess the issue I'm having is why hasn't the time has popped up, but the temperature is. I'll see if I can miss around the setCursor and try to get the time on screen because that's what is causing issues.
So far, I have found a way to have the time and temperature be on the screen. I used a few codes the creator of the project posted on his post and post into a version of a code I'm working on. The issues now I'm having is how can I keep the time from going away and keeping it still. When I upload the code, the time is displayed for 6 seconds on the left side of the screen, then the temperature pops up on the right side for a 2 seconds and goes back to the screen. I kind of want the time to be still on the life side, but keep the temperature poppoing up every 6 seconds.
Here's the code I have compiled:
#include <SPI.h> //i2c and the display libraries
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <DS3231.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET 4 // Reset pin # (or -1 if sharing Arduino reset pin)
int a;
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); //Declaring the display name (display)
DS3231 clock;
RTCDateTime dt;
void setup() {
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); //Start the OLED display
clock.begin();
delay(1000);
display.clearDisplay();
display.display();
a=millis();
}
void loop() { //This code displays the Time (Hours:Minutes) and the Date (Day/Month)
dt = clock.getDateTime();
display.clearDisplay();
display.setTextSize(2); //size of the text that will follow
display.setTextColor(WHITE); //its color
display.setCursor(1,1); //position from where you want to start writing
display.print(clock.dateFormat("H:i",dt)); //text todisplay
display.print(dt.hour); //text todisplay
display.print(":");
display.print(dt.minute);
display.setCursor(100,1);
display.setTextSize(2);
display.print(clock.dateFormat("D", dt));
display.setCursor(100,20);
display.print(clock.dateFormat("d", dt));
display.setCursor(100,25);
display.display();
if (millis()>=a+6000 && millis()<=a+7000){ //Temperature display refresh time (every 6-7 seconds)
dt = clock.getDateTime();
a=millis();
display.clearDisplay();
display.setTextSize(2); //size of the text that will follow
display.setTextColor(WHITE); //its color
display.setCursor(1,1); //position from where you want to start writing
display.print(clock.dateFormat("H:i", dt)); //text todisplay
display.setTextSize(2);
display.setCursor(100,1);
display.print(clock.readTemperature(),0);
display.setCursor(100,20);
display.setTextSize(1);
display.print("C");
display.setCursor(100,25);
display.display();
delay(2000); //delay of displaying the temperature
}
}
For now, I think I got it. I made some changes to the code and I think I got the basic layout of what I want. It's showing the time, date, and temperature. Thank for giving me advice and making sure I had the right libraries. I also gave you Karma in the previous posts.
My new code is now:
There can be improvements where it can be visually appealing but it's working.
#include <SPI.h> //i2c and the display libraries
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <DS3231.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET 4 // Reset pin # (or -1 if sharing Arduino reset pin)
int a;
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); //Declaring the display name (display)
DS3231 clock;
RTCDateTime dt;
void setup() {
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); //Start the OLED display
clock.begin();
delay(1000);
display.clearDisplay();
display.display();
a=millis();
}
void loop() { //This code displays the Time (Hours:Minutes) and the Date (Day/Month)
dt = clock.getDateTime();
display.clearDisplay();
display.setTextSize(2); //size of the text that will follow
display.setTextColor(WHITE); //its color
display.setCursor(1,1); //position from where you want to start writing
display.print(clock.dateFormat("H:i",dt)); //text to display
display.print(dt.hour); //text to display
display.print(":");
display.print(dt.minute);
display.setCursor(100,1);
display.setTextSize(2);
display.print(dt.month);
display.setCursor(100,20);
display.print(dt.day);
display.setCursor(100,25);
display.display();
if (millis()>=a+6000 && millis()<=a+7000){ //Temperature display refresh time (every 6-7 seconds)
dt = clock.getDateTime();
a=millis();
display.setTextSize(2); //size of the text that will follow
display.setTextColor(WHITE); //its color
display.setCursor(1,1); //position from where you want to start writing
display.print(clock.dateFormat("H:i", dt)); //text to display
display.setTextSize(2);
display.setCursor(100,40);
float f=(clock.readTemperature() * 1.8) + 32; //Temperature is read in C and we convert it to F
display.print(f,0);
display.setCursor(100,55);
display.setTextSize(1);
display.print(" F");
display.setCursor(100,65);
display.display();
delay(2000); //delay of displaying the temperature
}
}
Don't use single letters for global variables; it conveys no meaning and makes searching for them in larger sketches much more difficult.
Variable "a" needs to be of "unsigned long" type, otherwise the value of "millis()" will be truncated to fit inside the "int" with unexpected results.
Not critical in your sketch but:
"millis()>=a+6000" is better written as:
"millis() - a >= 6000" to avoid issues upon rollover of the unsigned numeric type.
You don't need to check if it is less than 7000ms.
"delay()" is best avoided in favour of using"millis()" for timing.
Tools->Autoformat or Ctrl-T will tidy the indentation of your sketch for you.