VIDEO: http://youtu.be/Vj2M880gH6ojigsawnz wrote:I made some changes for you. Refresh rate of the screen is now half a second and I removed some code that you don't want because it was for the humidity.
Thank you very much!
I have modified your code. I added these two lines of old code and eliminated another sketch this one.
Now it seems the second run to perfection.
I said. Thank you very much for your code and help!
ADDED: long start_time = millis();
and
delay(1000 - (millis() - start_time));
DELETED: if ( millis() - refreshTimer > 500 )
THE MODIFIED CODE:
Code: Select all
/*********************************************************************
This is a simple clock written for 128x64 OLED display.
Hardware used:
Arduino Nano V3
64x128 OLED display
DS18B20 Tem sensor
RTC I2C Module
Code written by George Timmermans
Libraries provided by other sources.
Special thanks to:
Adafruit (for there hardware, tutorials and libraries)
Stephanie (for writing the chronodot library)
I could not have done it without them. Thank you.
*********************************************************************/
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <RTClib.h>
#include <OneWire.h>
#include <DallasTemperature.h>
//********************************************************************
#define OLED_DC 11
#define OLED_CS 12
#define OLED_CLK 10
#define OLED_MOSI 9
#define OLED_RESET 8
#define ONE_WIRE_BUS 7
Adafruit_SSD1306 display( OLED_MOSI, OLED_CLK, OLED_DC, OLED_RESET,
OLED_CS );
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
#if ( SSD1306_LCDHEIGHT != 64 )
#error( "Height incorrect, please fix Adafruit_SSD1306.h!" );
#endif
RTC_DS1307 RTC;
const int MAX = 60;
int tempArray[ MAX ];
//********************************************************************
uint32_t storeDataTimer = millis();
uint32_t refreshTimer = millis();
uint32_t printTimer = millis();
//********************************************************************
void setup()
{
display.begin( SSD1306_SWITCHCAPVCC );
Wire.begin();
RTC.begin();
sensors.begin();
RTC.adjust(DateTime(__DATE__, __TIME__));
display.setTextSize( 1 );
display.setTextColor( WHITE );
display.clearDisplay();
for ( int i = 0; i < MAX; i++ )
{
tempArray[ i ] = 0;
}
}
float temp = 0;
//********************************************************************
void loop()
{
long start_time = millis();
sensors.requestTemperatures();
DateTime now = RTC.now();
if ( storeDataTimer > millis() )
storeDataTimer = millis();
if ( refreshTimer > millis() )
refreshTimer = millis();
if ( printTimer > millis() )
printTimer = millis();
if ( millis() - storeDataTimer > 2000 )
{
storeDataTimer = millis();
storeTemp();
}
if ( millis() - printTimer <= 20000 )
{
{
refreshTimer = millis();
display.clearDisplay();
printClock( RTC.now() );
display.display();
}
}
if ( millis() - printTimer > 20000 && millis() - printTimer <= 40000 )
{
if ( millis() - refreshTimer > 500 )
{
refreshTimer = millis();
display.clearDisplay();
drawTempGraph();
display.display();
}
}
if ( millis() - printTimer > 40000 )
printTimer = millis();
delay(1000 - (millis() - start_time));
}
//********************************************************************
void printClock( DateTime now )
{
display.drawCircle( 31,32,31,WHITE );
display.drawFastVLine( 30, 3, 4, WHITE );
display.drawFastVLine( 32, 3, 4, WHITE );
display.drawFastHLine( 57, 32, 4, WHITE );
display.drawFastVLine( 31, 58, 4, WHITE );
display.drawFastHLine( 2, 32, 4, WHITE );
display.drawPixel( 45, 7, WHITE );
display.drawPixel( 55, 18, WHITE);
display.drawPixel( 55, 46, WHITE );
display.drawPixel( 45, 56, WHITE );
display.drawPixel( 17, 56, WHITE );
display.drawPixel( 6, 46, WHITE );
display.drawPixel( 6, 18, WHITE );
display.drawPixel( 17, 7, WHITE );
if ( ! RTC.isrunning() )
{
display.setCursor( 67, 2 );
display.println( "RTC NOT" );
display.setCursor( 67, 10 );
display.println( "running!" );
}
else
{
drawSeconds( now.second() );
drawMinutes( now.minute() );
drawHours( now.hour(), now.minute() );
printDateTime( RTC.now() );
}
}
//********************************************************************
void printDateTime( DateTime now )
{
float temp = sensors.getTempCByIndex(0) ;
display.setCursor( 67, 2 );
if( now.day() < 10 )
display.print( "0" );
display.print( now.day(), DEC );
display.print( '/' );
if( now.month() < 10 )
display.print( "0" );
display.print( now.month(), DEC );
display.print( '/' );
display.println( now.year(), DEC );
display.setCursor( 67, 10 );
if ( now.hour() < 10 )
display.print( '0' );
display.print( now.hour(), DEC );
display.print( ':' );
if ( now.minute() < 10 )
display.print('0');
display.print( now.minute(), DEC );
display.print( ':' );
if ( now.second() < 10 )
display.print( '0' );
display.println( now.second(), DEC );
if ( isnan( ( float ) temp ) )
{
display.setCursor( 67, 18 );
display.println( "No" );
display.setCursor( 67, 26 );
display.println( "Temp!" );
}
else
{
display.setCursor( 60, 56 );
display.print( "Temp: " );
display.print( ( float ) temp, 1 );
display.println( "C" );
}
}
//********************************************************************
void drawSeconds( int seconds )
{
display.drawLine( 31, 32, 31 + 25 * cos( ( 270 + seconds * 6 ) * 3.14159 / 180 ),
32 + 25 * sin( ( 270 + seconds * 6 ) * 3.14159 / 180 ), WHITE );
}
//********************************************************************
void drawMinutes( int minutes )
{
display.drawLine( 31, 32, 31 + 20 * cos( ( 270 + minutes * 6 ) * 3.14159 / 180 ),
32 + 20 * sin( ( 270 + minutes * 6 ) * 3.14159 / 180 ), WHITE );
}
//********************************************************************
void drawHours( int hours, int minutes )
{
display.drawLine( 31, 32, 31 + 15 * cos( ( 270 + hours % 12 * 30 + minutes * 0.5 ) * 3.14159 / 180 ),
32 + 15 * sin( ( 270 + hours % 12 * 30 + minutes * 0.5 ) * 3.14159 / 180 ), WHITE );
}
//********************************************************************
void storeTemp()
{
int temp = sensors.getTempCByIndex(0) ;
static int i = 0;
if ( isnan( ( int ) temp ) )
if ( i < MAX )
{
tempArray[ i ] = 0;
i++;
}
else
{
for ( int j = 0; j < MAX - 1; j++ )
{
tempArray[ j ] = tempArray[ j + 1 ];
tempArray[ MAX - 1 ] = 0;
}
}
else
{
if ( i < MAX )
{
tempArray[ i ] = temp;
i++;
}
else
{
for ( int j = 0; j < MAX - 1; j++ )
{
tempArray[ j ] = tempArray[ j + 1 ];
tempArray[ MAX - 1 ] = temp;
}
}
}
}
//********************************************************************
void drawTempGraph()
{
display.drawPixel( 6, 13, WHITE );
display.drawPixel( 6, 23, WHITE );
display.drawPixel( 6, 33, WHITE );
display.drawPixel( 6, 43, WHITE );
display.drawPixel( 6, 53, WHITE );
display.drawPixel( 27, 62, WHITE );
display.drawPixel( 47, 62, WHITE );
display.drawPixel( 67, 62, WHITE );
display.drawPixel( 87, 62, WHITE );
display.drawPixel( 107, 62, WHITE );
display.drawFastVLine( 7, 0, 100, WHITE );
display.drawFastHLine( 7, 63, 120, WHITE );
display.setCursor( 9, 54 );
display.print( "Temp:" );
display.print( ( float ) sensors.getTempCByIndex(0),1
);
display.println( "C" );
display.setCursor( 0, 0 );
display.write( 24 );
display.setCursor( 0, 8 );
display.print( 'T' );
for (int i = 0; i < MAX; i++ )
display.drawFastHLine( 128 - MAX * 2 + i * 2, 64 - tempArray[ i ] * 2, 2, WHITE );
}
//********************************************************************