Pada kesempatan kali ini saya akan menjelaskan mengenai bagaimana cara mengakses OLED LCD dan RTC DS1302 secara bersamaan, aplikasi dari alat ini yaitu untuk menampilkan jam dan tanggal pada lcd oled display dan juga bisa setting alarm sesuai yang diinginkan, jadi jika jam telah masuk pada zona alarm maka led atau buzzer akan menyala sehingga akan menandakan bahwa ada peringatan. untuk lebih jelasnya berikut adalah skema dan programnya.
a. Arduino Mega
b. LCD OLED
c. RTC DS1302
d. Program Arduino IDE ( Menampilkan Jam dan Tanggal )
// DS1302: RST pin -> Arduino Digital 2
// DAT pin -> Arduino Digital 3
// CLK pin -> Arduino Digital 4
#include <Wire.h>
#include <DS1302.h>
#include "U8glib.h"
U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NO_ACK);
int x;
char tmp_string1[8];
char tmp_string2[8];
int dataadc;
int z;
int setting = 0;
const int bt1 = 8;
const int bt2 = 9;
const int bt3 = 10;
const int bt4 = 11;
int bt1x;
int bt2x;
int bt3x;
int bt4x;
int led1 = 46;
int led2 = 44;
int led3 = 48;
int led4 = 50;
int waktu = 0;
int waktusimpan = 0;
DS1302 rtc(2, 3, 4);
Time t;
void draw(void) {
if(setting == 1){
bt2x = digitalRead(bt2);
bt3x = digitalRead(bt3);
bt4x = digitalRead(bt4);
itoa(waktu, tmp_string1, 8);
u8g.setFont(u8g_font_unifont);
u8g.drawStr(0, 22, "SET JAM");
u8g.drawStr(0, 40, tmp_string1);
if(bt2x == 0){
delay(200);
waktu++;
}
if(bt3x == 0){
delay(200);
waktu--;
}
if(bt4x == 0){
delay(200);
waktusimpan = waktu;
setting = 0;
}
}
if(setting == 0){
//float dua angka dibelakang koma
// itoa(z, tmp_string1, 8);
// itoa(dataadc, tmp_string2, 8);
u8g.setFont(u8g_font_unifont);
u8g.drawStr(0, 22, rtc.getDateStr(FORMAT_LONG,FORMAT_LITTLEENDIAN, '/'));
u8g.drawStr(0, 40, rtc.getTimeStr());
//Serial.println(t.hour);
if(t.hour == 7){
digitalWrite(led1,LOW);
digitalWrite(led2,HIGH);
digitalWrite(led3,HIGH);
digitalWrite(led4,HIGH);
}
else if(t.hour == 13){
digitalWrite(led1,HIGH);
digitalWrite(led2,LOW);
digitalWrite(led3,HIGH);
digitalWrite(led4,HIGH);
}
else if(t.hour == 20){
digitalWrite(led1,HIGH);
digitalWrite(led2,HIGH);
digitalWrite(led3,LOW);
digitalWrite(led4,HIGH);
}
else if(t.hour == waktusimpan){
digitalWrite(led1,HIGH);
digitalWrite(led2,HIGH);
digitalWrite(led3,HIGH);
digitalWrite(led4,LOW);
}
else{
digitalWrite(led1,HIGH);
digitalWrite(led2,HIGH);
digitalWrite(led3,HIGH);
digitalWrite(led4,HIGH);
}
}
}
void setup(void) {
pinMode(led1,OUTPUT);
pinMode(led2,OUTPUT);
pinMode(led3,OUTPUT);
pinMode(led4,OUTPUT);
pinMode(bt1,INPUT);
pinMode(bt2,INPUT);
pinMode(bt3,INPUT);
pinMode(bt4,INPUT);
digitalWrite(bt1,HIGH);
digitalWrite(bt2,HIGH);
digitalWrite(bt3,HIGH);
digitalWrite(bt4,HIGH);
Serial.begin(9600);
rtc.halt(false);
rtc.writeProtect(false);
// rtc.setDOW(MONDAY);
// rtc.setTime(03, 06,00);
// rtc.setDate(13, 3, 2017);
rtc.writeProtect(true);
if ( u8g.getMode() == U8G_MODE_R3G3B2 ) {
u8g.setColorIndex(255); // white
}
else if ( u8g.getMode() == U8G_MODE_GRAY2BIT ) {
u8g.setColorIndex(3); // max intensity
}
else if ( u8g.getMode() == U8G_MODE_BW ) {
u8g.setColorIndex(1); // pixel on
}
else if ( u8g.getMode() == U8G_MODE_HICOLOR ) {
u8g.setHiColorByRGB(255,255,255);
}
}
void loop(void) {
bt1x = digitalRead(bt1);
if(bt1x == 0){
delay(200);
setting = 1;
}
t = rtc.getTime();
u8g.firstPage();
do {
draw();
} while( u8g.nextPage() );
delay(1000);
}
e. Program Arduino IDE ( Menampilkan Jam dan Tanggal + Fitur Setting Alarm )
// DS1302: RST pin -> Arduino Digital 2
// DAT pin -> Arduino Digital 3
// CLK pin -> Arduino Digital 4
#include <Wire.h>
#include <DS1302.h>
#include "U8glib.h"
U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NO_ACK);
int x;
char tmp_string1[8];
char tmp_string2[8];
int dataadc;
int z;
int setting = 0;
int key1 = 14;
int key2 = 15;
int key3 = 16;
int key4 = 17;
const int bt1 = 8;
const int bt2 = 9;
const int bt3 = 10;
const int bt4 = 11;
int bt1x;
int bt2x;
int bt3x;
int bt4x;
int mark1 = 0;
int mark2 = 0;
int mark3 = 0;
int mark4 = 0;
int led1 = 46;
int led2 = 44;
int led3 = 48;
int led4 = 50;
int waktu = 0;
int waktusimpan = 0;
int waktu2 = 0;
int waktusimpan2 = 0;
int waktu3 = 0;
int waktusimpan3 = 0;
int waktu4 = 0;
int waktusimpan4 = 0;
DS1302 rtc(2, 3, 4);
Time t;
void draw(void) {
if(setting == 1){
bt2x = digitalRead(bt2);
bt3x = digitalRead(bt3);
bt4x = digitalRead(bt4);
itoa(waktu, tmp_string1, 8);
u8g.setFont(u8g_font_unifont);
u8g.drawStr(0, 22, "SET JAM KLM4");
u8g.drawStr(0, 40, tmp_string1);
if(bt2x == 0){
delay(200);
waktu = waktu + 1;
}
if(bt3x == 0){
delay(200);
waktu = waktu - 1;
}
if(bt4x == 0){
delay(1000);
waktusimpan = waktu;
setting = 2;
}
}
if(setting == 2){
bt2x = digitalRead(bt2);
bt3x = digitalRead(bt3);
bt4x = digitalRead(bt4);
itoa(waktu2, tmp_string1, 8);
u8g.setFont(u8g_font_unifont);
u8g.drawStr(0, 22, "SET JAM KLM1");
u8g.drawStr(0, 40, tmp_string1);
if(bt2x == 0){
delay(200);
waktu2 = waktu2 + 1;
}
if(bt3x == 0){
delay(200);
waktu2 = waktu2 - 1;
}
if(bt4x == 0){
delay(1000);
waktusimpan2 = waktu2;
setting = 3;
}
}
if(setting == 3){
bt2x = digitalRead(bt2);
bt3x = digitalRead(bt3);
bt4x = digitalRead(bt4);
itoa(waktu3, tmp_string1, 8);
u8g.setFont(u8g_font_unifont);
u8g.drawStr(0, 22, "SET JAM KLM2");
u8g.drawStr(0, 40, tmp_string1);
if(bt2x == 0){
delay(200);
waktu3 = waktu3 + 1;
}
if(bt3x == 0){
delay(200);
waktu3 = waktu3 - 1;
}
if(bt4x == 0){
delay(1000);
waktusimpan3 = waktu3;
setting = 4;
}
}
if(setting == 4){
bt2x = digitalRead(bt2);
bt3x = digitalRead(bt3);
bt4x = digitalRead(bt4);
itoa(waktu3, tmp_string1, 8);
u8g.setFont(u8g_font_unifont);
u8g.drawStr(0, 22, "SET JAM KLM3");
u8g.drawStr(0, 40, tmp_string1);
if(bt2x == 0){
delay(200);
waktu3 = waktu3 + 1;
}
if(bt3x == 0){
delay(200);
waktu3 = waktu3 - 1;
}
if(bt4x == 0){
delay(1000);
waktusimpan3 = waktu3;
setting = 0;
}
}
if(setting == 0){
//float dua angka dibelakang koma
// itoa(z, tmp_string1, 8);
int adc1 = analogRead(A0);
int adc2 = analogRead(A1);
int adc3 = analogRead(A2);
// itoa(adc4, tmp_string2, 8);
u8g.setFont(u8g_font_unifont);
u8g.drawStr(0, 22, rtc.getDateStr(FORMAT_LONG,FORMAT_LITTLEENDIAN, '/'));
u8g.drawStr(0, 40, rtc.getTimeStr());
//u8g.drawStr(0, 50, tmp_string2);
//Serial.println(adc1);
//Serial.println(adc2);
//Serial.println(adc3);
//Serial.println(adc4);
if((t.hour == waktusimpan2)&&(mark1 == 0)){
digitalWrite(led1,LOW);
digitalWrite(led2,HIGH);
digitalWrite(led3,HIGH);
digitalWrite(led4,HIGH);
digitalWrite(key1,LOW);
digitalWrite(key2,HIGH);
digitalWrite(key3,HIGH);
digitalWrite(key4,HIGH);
int adc1 = analogRead(A0);
if(adc1 < 1010){
mark1 = 1;
}
}
if((t.hour == waktusimpan2)&&(mark1 == 1)){
digitalWrite(led1,HIGH);
digitalWrite(led2,HIGH);
digitalWrite(led3,HIGH);
digitalWrite(led4,HIGH);
digitalWrite(key1,HIGH);
digitalWrite(key2,HIGH);
digitalWrite(key3,HIGH);
digitalWrite(key4,HIGH);
}
else if((t.hour == waktusimpan3)&&(mark2 == 0)){
digitalWrite(led1,HIGH);
digitalWrite(led2,LOW);
digitalWrite(led3,HIGH);
digitalWrite(led4,HIGH);
digitalWrite(key1,HIGH);
digitalWrite(key2,LOW);
digitalWrite(key3,HIGH);
digitalWrite(key4,HIGH);
int adc2 = analogRead(A1);
if(adc2 < 1010){
mark2 = 1;
}
}
else if((t.hour == waktusimpan3)&&(mark2 == 1)){
digitalWrite(led1,HIGH);
digitalWrite(led2,HIGH);
digitalWrite(led3,HIGH);
digitalWrite(led4,HIGH);
digitalWrite(key1,HIGH);
digitalWrite(key2,HIGH);
digitalWrite(key3,HIGH);
digitalWrite(key4,HIGH);
}
else if((t.hour == waktusimpan4)&&(mark3 == 0)){
digitalWrite(led1,HIGH);
digitalWrite(led2,HIGH);
digitalWrite(led3,LOW);
digitalWrite(led4,HIGH);
digitalWrite(key1,HIGH);
digitalWrite(key2,HIGH);
digitalWrite(key3,LOW);
digitalWrite(key4,HIGH);
int adc3 = analogRead(A2);
if(adc3 < 1010){
mark3 = 1;
}
}
else if((t.hour == waktusimpan4)&&(mark3 == 1)){
digitalWrite(led1,HIGH);
digitalWrite(led2,HIGH);
digitalWrite(led3,HIGH);
digitalWrite(led4,HIGH);
digitalWrite(key1,HIGH);
digitalWrite(key2,HIGH);
digitalWrite(key3,HIGH);
digitalWrite(key4,HIGH);
}
else if((t.hour == waktusimpan)&&(mark4 == 0)){
digitalWrite(led1,HIGH);
digitalWrite(led2,HIGH);
digitalWrite(led3,HIGH);
digitalWrite(led4,LOW);
digitalWrite(key1,HIGH);
digitalWrite(key2,HIGH);
digitalWrite(key3,HIGH);
digitalWrite(key4,LOW);
int adc4 = analogRead(A3);
if(adc4 < 1010){
mark4 = 1;
}
}
else if((t.hour == waktusimpan)&&(mark4 == 1)){
digitalWrite(led1,HIGH);
digitalWrite(led2,HIGH);
digitalWrite(led3,HIGH);
digitalWrite(led4,HIGH);
digitalWrite(key1,HIGH);
digitalWrite(key2,HIGH);
digitalWrite(key3,HIGH);
digitalWrite(key4,HIGH);
}
else{
digitalWrite(led1,HIGH);
digitalWrite(led2,HIGH);
digitalWrite(led3,HIGH);
digitalWrite(led4,HIGH);
digitalWrite(key1,HIGH);
digitalWrite(key2,HIGH);
digitalWrite(key3,HIGH);
digitalWrite(key4,HIGH);
}
}
}
void setup(void) {
pinMode(key1,OUTPUT);
pinMode(key2,OUTPUT);
pinMode(key3,OUTPUT);
pinMode(key4,OUTPUT);
pinMode(led1,OUTPUT);
pinMode(led2,OUTPUT);
pinMode(led3,OUTPUT);
pinMode(led4,OUTPUT);
pinMode(bt1,INPUT);
pinMode(bt2,INPUT);
pinMode(bt3,INPUT);
pinMode(bt4,INPUT);
digitalWrite(bt1,HIGH);
digitalWrite(bt2,HIGH);
digitalWrite(bt3,HIGH);
digitalWrite(bt4,HIGH);
Serial.begin(9600);
rtc.halt(false);
rtc.writeProtect(false);
//rtc.setDOW(WEDNESDAY);
//rtc.setTime(19, 55, 00);
//rtc.setDate(15, 3, 2017);
rtc.writeProtect(true);
if ( u8g.getMode() == U8G_MODE_R3G3B2 ) {
u8g.setColorIndex(255); // white
}
else if ( u8g.getMode() == U8G_MODE_GRAY2BIT ) {
u8g.setColorIndex(3); // max intensity
}
else if ( u8g.getMode() == U8G_MODE_BW ) {
u8g.setColorIndex(1); // pixel on
}
else if ( u8g.getMode() == U8G_MODE_HICOLOR ) {
u8g.setHiColorByRGB(255,255,255);
}
}
void loop(void) {
bt1x = digitalRead(bt1);
if(bt1x == 0){
delay(200);
setting = 1;
}
t = rtc.getTime();
u8g.firstPage();
do {
draw();
} while( u8g.nextPage() );
delay(1000);
}
f. Video Hasilnya
No comments:
Post a Comment