DS1307 DHT11 Arduino Nano DMD P10
Menampilkan display jam : menit dan Suhu serta Kelembaban (Humidity) P10 (32 X16) Arduino Nano 5 VDC 2A
Videonya :
Codingnya :
#include "DHT.h"
#define DHTPIN 2
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
// DHT Integers
int a = 7; // these determine the position of the character
int b = 14;
int c = 21;
int hundred_t = 0;
int ten_t = 0;
int unit_t = 0;
int hundred_h = 0;
int ten_h = 0;
int unit_h = 0;
int temp = 0;
int humid = 0;
int val_t = 0;
int val_h = 0;
byte dayOfWeek=4;
byte dayOfMonth=1;
#include <SPI.h> //SPI.h must be included as DMD is written by SPI (the IDE complains otherwise)
#include <DMD.h> //
#include <TimerOne.h> //
#include "SystemFont5x7.h" // This font is too small just for the time display
//#include "Arial_black_16.h" // This font is too big for the clock display, the last number just misses by two columns
#include "Arial14.h" // Arial 14. This is a nice fit
#include <Wire.h>
#define DS1307_I2C_ADDRESS 0x68
// Convert normal decimal numbers to binary coded decimal
byte decToBcd(byte val)
{
return ( (val/10*16) + (val%10) );
}
// Convert binary coded decimal to normal decimal numbers
byte bcdToDec(byte val)
{
return ( (val/16*10) + (val%16) );
}
// 1) Sets the date and time on the ds1307
// 2) Starts the clock
// 3) Sets hour mode to 24 hour clock
// Assumes you're passing in valid numbers
void setDateDs1307(byte second, // 0-59
byte minute, // 0-59
byte hour, // 1-23
byte dayOfWeek, // 1-7
byte dayOfMonth, // 1-28/29/30/31
byte month, // 1-12
byte year) // 0-99
{
Wire.beginTransmission(DS1307_I2C_ADDRESS);
Wire.write(0);
Wire.write(decToBcd(second)); // 0 to bit 7 starts the clock
Wire.write(decToBcd(minute));
Wire.write(decToBcd(hour));
Wire.write(decToBcd(dayOfWeek));
Wire.write(decToBcd(dayOfMonth));
Wire.write(decToBcd(month));
Wire.write(decToBcd(year));
Wire.write(00010000); // sends 0x10 (hex) 00010000 (binary) to control register - turns on square wave
Wire.endTransmission();
}
// Gets the date and time from the ds1307
void getDateDs1307(byte *second,
byte *minute,
byte *hour,
byte *dayOfWeek,
byte *dayOfMonth,
byte *month,
byte *year)
{
// Reset the register pointer
Wire.beginTransmission(DS1307_I2C_ADDRESS);
Wire.write(0);
Wire.endTransmission();
Wire.requestFrom(DS1307_I2C_ADDRESS, 7);
// A few of these need masks because certain bits are control bits
*second = bcdToDec(Wire.read() & 0x7f);
*minute = bcdToDec(Wire.read());
*hour = bcdToDec(Wire.read() & 0x3f); // Need to change this if 12 hour am/pm
*dayOfWeek = bcdToDec(Wire.read());
*dayOfMonth = bcdToDec(Wire.read());
*month = bcdToDec(Wire.read());
*year = bcdToDec(Wire.read());
}
//Fire up the DMD library as dmd
DMD dmd(1,1);
void ScanDMD()
{
dmd.scanDisplayBySPI();
}
void setup()
{
byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
//initialize TimerOne's interrupt/CPU usage used to scan and refresh the display
Timer1.initialize( 5000 ); //period in microseconds to call ScanDMD. Anything longer than 5000 (5ms) and you can see flicker.
Timer1.attachInterrupt( ScanDMD ); //attach the Timer1 interrupt to ScanDMD which goes to dmd.scanDisplayBySPI()
//clear/init the DMD pixels held in RAM
dmd.clearScreen( true ); //true is normal (all pixels off), false is negative (all pixels on)
dmd.selectFont(System5x7);
// dmd.selectFont(Arial_Black_16);
// dmd.selectFont(Arial_14); // Note the format of this command, dmd.selectFont(Arial14); does not compile
Wire.begin();
// Change these values to what you want to set your clock to.
// You probably only want to set your clock once and then remove
// the setDateDs1307 call.
second = 0;
minute = 50;
hour = 07; // use 24-hour time otherwise day/date calculations will be off
dayOfWeek = 7;
dayOfMonth = 17;
month = 5;
year = 12;
// setDateDs1307(second, minute, hour, dayOfWeek, dayOfMonth, month, year); // after you set the time comment this line out
}
void drawHour()
{
byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
getDateDs1307(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, &year);
// I elected to make this a 24 hour clock so I commented the following code out and added case 13 to 24 below
// if (hour>12)
// {
// hour=hour-12; // only 12 hours on analogue clocks
// }
switch(hour)
{
// I have placed this on row 0 as it only leaves one line (row15) to display the seconds without overlapping pixels
// Try it on row 1 and not all of the seconds will be displayed
case 1:
dmd.drawString( 1,0, "01", 2, GRAPHICS_NORMAL );
break;
case 2:
dmd.drawString( 1,0, "02", 2, GRAPHICS_NORMAL );
break;
case 3:
dmd.drawString( 1,0, "03", 2, GRAPHICS_NORMAL );
break;
case 4:
dmd.drawString( 1,0, "04", 2, GRAPHICS_NORMAL );
break;
case 5:
dmd.drawString( 1,0, "05", 2, GRAPHICS_NORMAL );
break;
case 6:
dmd.drawString( 1,0, "06", 2, GRAPHICS_NORMAL );
break;
case 7:
dmd.drawString( 1,0, "07", 2, GRAPHICS_NORMAL );
break;
case 8:
dmd.drawString( 1,0, "08", 2, GRAPHICS_NORMAL );
break;
case 9:
dmd.drawString( 1,0, "09", 2, GRAPHICS_NORMAL );
break;
case 10:
dmd.drawString( 1,0, "10", 2, GRAPHICS_NORMAL );
break;
case 11:
dmd.drawString( 1,0, "11", 2, GRAPHICS_NORMAL );
break;
case 12:
dmd.drawString( 1,0, "12", 2, GRAPHICS_NORMAL );
break;
// I elected to make this a 24 hour clock. If you want a 12 hour clock remove this code and uncomment the code above
case 13:
dmd.drawString( 1,0, "13", 2, GRAPHICS_NORMAL );
break;
case 14:
dmd.drawString( 1,0, "14", 2, GRAPHICS_NORMAL );
break;
case 15:
dmd.drawString( 1,0, "15", 2, GRAPHICS_NORMAL );
break;
case 16:
dmd.drawString( 1,0, "16", 2, GRAPHICS_NORMAL );
break;
case 17:
dmd.drawString( 1,0, "17", 2, GRAPHICS_NORMAL );
break;
case 18:
dmd.drawString( 1,0, "18", 2, GRAPHICS_NORMAL );
break;
case 19:
dmd.drawString( 1,0, "19", 2, GRAPHICS_NORMAL );
break;
case 20:
dmd.drawString( 1,0, "20", 2, GRAPHICS_NORMAL );
break;
case 21:
dmd.drawString( 1,0, "21", 2, GRAPHICS_NORMAL );
break;
case 22:
dmd.drawString( 1,0, "22", 2, GRAPHICS_NORMAL );
break;
case 23:
dmd.drawString( 1,0, "23", 2, GRAPHICS_NORMAL );
break;
case 24:
dmd.drawString( 1,0, "24", 2, GRAPHICS_NORMAL );
break;
}
}
void drawMinute()
{
byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
getDateDs1307(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, &year);
switch(minute)
{
case 0:
dmd.drawString( 17,0, "00", 2, GRAPHICS_NORMAL );
break;
case 1:
dmd.drawString( 17,0, "01", 2, GRAPHICS_NORMAL );
break;
case 2:
dmd.drawString( 17,0, "02", 2, GRAPHICS_NORMAL );
break;
case 3:
dmd.drawString( 17,0, "03", 2, GRAPHICS_NORMAL );
break;
case 4:
dmd.drawString( 17,0, "04", 2, GRAPHICS_NORMAL );
break;
case 5:
dmd.drawString( 17,0, "05", 2, GRAPHICS_NORMAL );
break;
case 6:
dmd.drawString( 17,0, "06", 2, GRAPHICS_NORMAL );
break;
case 7:
dmd.drawString( 17,0, "07", 2, GRAPHICS_NORMAL );
break;
case 8:
dmd.drawString( 17,0, "08", 2, GRAPHICS_NORMAL );
break;
case 9:
dmd.drawString( 17,0, "09", 2, GRAPHICS_NORMAL );
break;
case 10:
dmd.drawString( 17,0, "10", 2, GRAPHICS_NORMAL );
break;
case 11:
dmd.drawString( 17,0, "11", 2, GRAPHICS_NORMAL );
break;
case 12:
dmd.drawString( 17,0, "12", 2, GRAPHICS_NORMAL );
break;
case 13:
dmd.drawString( 17,0, "13", 2, GRAPHICS_NORMAL );
break;
case 14:
dmd.drawString( 17,0, "14", 2, GRAPHICS_NORMAL );
break;
case 15:
dmd.drawString( 17,0, "15", 2, GRAPHICS_NORMAL );
break;
case 16:
dmd.drawString( 17,0, "16", 2, GRAPHICS_NORMAL );
break;
case 17:
dmd.drawString( 17,0, "17", 2, GRAPHICS_NORMAL );
break;
case 18:
dmd.drawString( 17,0, "18", 2, GRAPHICS_NORMAL );
break;
case 19:
dmd.drawString( 17,0, "19", 2, GRAPHICS_NORMAL );
break;
case 20:
dmd.drawString( 17,0, "20", 2, GRAPHICS_NORMAL );
break;
case 21:
dmd.drawString( 17,0, "21", 2, GRAPHICS_NORMAL );
break;
case 22:
dmd.drawString( 17,0, "22", 2, GRAPHICS_NORMAL );
break;
case 23:
dmd.drawString( 17,0, "23", 2, GRAPHICS_NORMAL );
break;
case 24:
dmd.drawString( 17,0, "24", 2, GRAPHICS_NORMAL );
break;
case 25:
dmd.drawString( 17,0, "25", 2, GRAPHICS_NORMAL );
break;
case 26:
dmd.drawString( 17,0, "26", 2, GRAPHICS_NORMAL );
break;
case 27:
dmd.drawString( 17,0, "27", 2, GRAPHICS_NORMAL );
break;
case 28:
dmd.drawString( 17,0, "28", 2, GRAPHICS_NORMAL );
break;
case 29:
dmd.drawString( 17,0, "29", 2, GRAPHICS_NORMAL );
break;
case 30:
dmd.drawString( 17,0, "30", 2, GRAPHICS_NORMAL );
break;
case 31:
dmd.drawString( 17,0, "31", 2, GRAPHICS_NORMAL );
break;
case 32:
dmd.drawString( 17,0, "32", 2, GRAPHICS_NORMAL );
break;
case 33:
dmd.drawString( 17,0, "33", 2, GRAPHICS_NORMAL );
break;
case 34:
dmd.drawString( 17,0, "34", 2, GRAPHICS_NORMAL );
break;
case 35:
dmd.drawString( 17,0, "35", 2, GRAPHICS_NORMAL );
break;
case 36:
dmd.drawString( 17,0, "36", 2, GRAPHICS_NORMAL );
break;
case 37:
dmd.drawString( 17,0, "37", 2, GRAPHICS_NORMAL );
break;
case 38:
dmd.drawString( 17,0, "38", 2, GRAPHICS_NORMAL );
break;
case 39:
dmd.drawString( 17,0, "39", 2, GRAPHICS_NORMAL );
break;
case 40:
dmd.drawString( 17,0, "40", 2, GRAPHICS_NORMAL );
break;
case 41:
dmd.drawString( 17,0, "41", 2, GRAPHICS_NORMAL );
break;
case 42:
dmd.drawString( 17,0, "42", 2, GRAPHICS_NORMAL );
break;
case 43:
dmd.drawString( 17,0, "43", 2, GRAPHICS_NORMAL );
break;
case 44:
dmd.drawString( 17,0, "44", 2, GRAPHICS_NORMAL );
break;
case 45:
dmd.drawString( 17,0, "45", 2, GRAPHICS_NORMAL );
break;
case 46:
dmd.drawString( 17,0, "46", 2, GRAPHICS_NORMAL );
break;
case 47:
dmd.drawString( 17,0, "47", 2, GRAPHICS_NORMAL );
break;
case 48:
dmd.drawString( 17,0, "48", 2, GRAPHICS_NORMAL );
break;
case 49:
dmd.drawString( 17,0, "49", 2, GRAPHICS_NORMAL );
break;
case 50:
dmd.drawString( 17,0, "50", 2, GRAPHICS_NORMAL );
break;
case 51:
dmd.drawString( 17,0, "51", 2, GRAPHICS_NORMAL );
break;
case 52:
dmd.drawString( 17,0, "52", 2, GRAPHICS_NORMAL );
break;
case 53:
dmd.drawString( 17,0, "53", 2, GRAPHICS_NORMAL );
break;
case 54:
dmd.drawString( 17,0, "54", 2, GRAPHICS_NORMAL );
break;
case 55:
dmd.drawString( 17,0, "55", 2, GRAPHICS_NORMAL );
break;
case 56:
dmd.drawString( 17,0, "56", 2, GRAPHICS_NORMAL );
break;
case 57:
dmd.drawString( 17,0, "57", 2, GRAPHICS_NORMAL );
break;
case 58:
dmd.drawString( 17,0, "58", 2, GRAPHICS_NORMAL );
break;
case 59:
dmd.drawString( 17,0, "59", 2, GRAPHICS_NORMAL );
break;
delay(990);
dmd.drawFilledBox(0,0,32,32,1); // clears the display
break;
}
}
void runClock()
{
drawHour();
drawMinute();
dmd.writePixel( 15,1, GRAPHICS_NORMAL,1);
dmd.writePixel( 14,1, GRAPHICS_NORMAL,1);
dmd.writePixel( 15,2, GRAPHICS_NORMAL,1);
dmd.writePixel( 14,2, GRAPHICS_NORMAL,1);
dmd.writePixel( 15,4, GRAPHICS_NORMAL,1);
dmd.writePixel( 14,4, GRAPHICS_NORMAL,1);
dmd.writePixel( 15,5, GRAPHICS_NORMAL,1);
dmd.writePixel( 14,5, GRAPHICS_NORMAL,1);
// The following code will display a flashing colon but this sometimes results in one random led in the second display not lighting
//dmd.drawChar( 13, 0, ':', GRAPHICS_OR ); // clock colon overlay on
//delay(500);
//dmd.drawChar( 13, 0, ':', GRAPHICS_NOR ); // clock colon overlay off
//delay(500);
}
void loop()
{
runClock();
dmd.writePixel( 20,15, GRAPHICS_NORMAL,1);
float h = dht.readHumidity();
float t = dht.readTemperature();
temp = t * 10;
hundred_t = temp / 100;
val_t = hundred_t * 100;
temp = temp - val_t;
ten_t = temp / 10;
val_t = ten_t * 10;
temp = temp - val_t;
unit_t = temp / 1;
humid = h * 10;
hundred_h = humid / 100;
val_h = hundred_h * 100;
humid = humid - val_h;
ten_h = humid / 10;
val_h = ten_h * 10;
humid = humid - val_h;
unit_h = humid / 1;
dmd.drawString( 0,9, "T", 1, GRAPHICS_NORMAL );
dmd.writePixel( 5,11, GRAPHICS_NORMAL,1);
dmd.writePixel( 5,12, GRAPHICS_NORMAL,1);
dmd.drawChar( 27, 9, 'c', GRAPHICS_NORMAL );
switch (hundred_t) {
case 0:
dmd.drawChar( a, 9, '0', GRAPHICS_NORMAL );
break;
case 1:
dmd.drawChar( a, 9, '1', GRAPHICS_NORMAL );
break;
case 2:
dmd.drawChar( a, 9, '2', GRAPHICS_NORMAL );
break;
case 3:
dmd.drawChar( a, 9, '3', GRAPHICS_NORMAL );
break;
case 4:
dmd.drawChar( a, 9, '4', GRAPHICS_NORMAL );
break;
case 5:
dmd.drawChar( a, 9, '5', GRAPHICS_NORMAL );
break;
case 6:
dmd.drawChar( a, 9, '6', GRAPHICS_NORMAL );
break;
case 7:
dmd.drawChar( a, 9, '7', GRAPHICS_NORMAL );
break;
case 8:
dmd.drawChar( a, 9, '8', GRAPHICS_NORMAL );
break;
case 9:
dmd.drawChar( a, 9, '9', GRAPHICS_NORMAL );
break;
}
switch (ten_t) {
case 0:
dmd.drawChar( b, 9, '0', GRAPHICS_NORMAL );
break;
case 1:
dmd.drawChar( b, 9, '1', GRAPHICS_NORMAL );
break;
case 2:
dmd.drawChar( b, 9, '2', GRAPHICS_NORMAL );
break;
case 3:
dmd.drawChar( b, 9, '3', GRAPHICS_NORMAL );
break;
case 4:
dmd.drawChar( b, 9, '4', GRAPHICS_NORMAL );
break;
case 5:
dmd.drawChar( b, 9, '5', GRAPHICS_NORMAL );
break;
case 6:
dmd.drawChar( b, 9, '6', GRAPHICS_NORMAL );
break;
case 7:
dmd.drawChar( b, 9, '7', GRAPHICS_NORMAL );
break;
case 8:
dmd.drawChar( b, 9, '8', GRAPHICS_NORMAL );
break;
case 9:
dmd.drawChar( b, 9, '9', GRAPHICS_NORMAL );
break;
}
switch (unit_t) {
case 0:
dmd.drawChar( c, 9, '0', GRAPHICS_NORMAL );
break;
case 1:
dmd.drawChar( c, 9, '1', GRAPHICS_NORMAL );
break;
case 2:
dmd.drawChar( c, 9, '2', GRAPHICS_NORMAL );
break;
case 3:
dmd.drawChar( c, 9, '3', GRAPHICS_NORMAL );
break;
case 4:
dmd.drawChar( c, 9, '4', GRAPHICS_NORMAL );
break;
case 5:
dmd.drawChar( c, 9, '5', GRAPHICS_NORMAL );
break;
case 6:
dmd.drawChar( c, 9, '6', GRAPHICS_NORMAL );
break;
case 7:
dmd.drawChar( c, 9, '7', GRAPHICS_NORMAL );
break;
case 8:
dmd.drawChar( c, 9, '8', GRAPHICS_NORMAL );
break;
case 9:
dmd.drawChar( c, 9, '9', GRAPHICS_NORMAL );
break;
}
delay(5000);
dmd.drawString( 0,9, "H", 1, GRAPHICS_NORMAL );
dmd.writePixel( 5,11, GRAPHICS_NORMAL,1);
dmd.writePixel( 5,12, GRAPHICS_NORMAL,1);
dmd.drawChar( 27, 9, '%', GRAPHICS_NORMAL );
switch (hundred_h) {
case 1:
dmd.drawChar( a, 9, '1', GRAPHICS_NORMAL );
break;
case 2:
dmd.drawChar( a, 9, '2', GRAPHICS_NORMAL );
break;
case 3:
dmd.drawChar( a, 9, '3', GRAPHICS_NORMAL );
break;
case 4:
dmd.drawChar( a, 9, '4', GRAPHICS_NORMAL );
break;
case 5:
dmd.drawChar( a, 9, '5', GRAPHICS_NORMAL );
break;
case 6:
dmd.drawChar( a, 9, '6', GRAPHICS_NORMAL );
break;
case 7:
dmd.drawChar( a, 9, '7', GRAPHICS_NORMAL );
break;
case 8:
dmd.drawChar( a, 9, '8', GRAPHICS_NORMAL );
break;
case 9:
dmd.drawChar( a, 9, '9', GRAPHICS_NORMAL );
break;
}
switch (ten_h) {
case 1:
dmd.drawChar( b, 9, '1', GRAPHICS_NORMAL );
break;
case 2:
dmd.drawChar( b, 9, '2', GRAPHICS_NORMAL );
break;
case 3:
dmd.drawChar( b, 9, '3', GRAPHICS_NORMAL );
break;
case 4:
dmd.drawChar( b, 9, '4', GRAPHICS_NORMAL );
break;
case 5:
dmd.drawChar( b, 9, '5', GRAPHICS_NORMAL );
break;
case 6:
dmd.drawChar( b, 9, '6', GRAPHICS_NORMAL );
break;
case 7:
dmd.drawChar( b, 9, '7', GRAPHICS_NORMAL );
break;
case 8:
dmd.drawChar( b, 9, '8', GRAPHICS_NORMAL );
break;
case 9:
dmd.drawChar( b, 9, '9', GRAPHICS_NORMAL );
break;
}
switch (unit_h) {
case 1:
dmd.drawChar( c, 9, '1', GRAPHICS_NORMAL );
break;
case 2:
dmd.drawChar( c, 9, '2', GRAPHICS_NORMAL );
break;
case 3:
dmd.drawChar( c, 9, '3', GRAPHICS_NORMAL );
break;
case 4:
dmd.drawChar( c, 9, '4', GRAPHICS_NORMAL );
break;
case 5:
dmd.drawChar( c, 9, '5', GRAPHICS_NORMAL );
break;
case 6:
dmd.drawChar( c, 9, '6', GRAPHICS_NORMAL );
break;
case 7:
dmd.drawChar( c, 9, '7', GRAPHICS_NORMAL );
break;
case 8:
dmd.drawChar( c, 9, '8', GRAPHICS_NORMAL );
break;
case 9:
dmd.drawChar( c, 9, '9', GRAPHICS_NORMAL );
break;
}
delay(5000);
}
Bang kalau mau pakai arduino Uno.Bagian mana yang di rubanh
ReplyDelete