program micro
TRANSCRIPT
-
8/18/2019 Program micro
1/7
1
PROGRAM ARDUINO UNO
/*
* The circuit:
* LCD RS pin to digital pin (7)
* LCD Enable pin to digital pin (6)
* LCD D4 pin to digital pin 4
* LCD D5 pin to digital pin 5
* LCD D6 pin to digital pin 6
* LCD D7 pin to digital pin 7
* LCD R/W pin to ground
* 10K resistor:* ends to +5V and ground
* wiper to LCD VO pin (pin 3)
*/
#include
//#include
#include
#include
#include
#include
#define relayPin 8
// The analog pins that connect to the sensors
#define currinPin A0 // analog 0
#define voltinPin A1 // analog 1
#define aref_voltage 5.0 // we tie 5.0V to ARef and measure it with a
multimeter!
//---------------------------------------------------------------------------
RTC_DS1307 RTC; // define the Real Time Clock object
//------------------------------------------------------------------------------
LiquidCrystal lcd(2, 3, 4, 5, 6, 7);
char buf[5];
ArduinoOutStream cout(Serial);
char date[9]; //xx/xx/xx;
char time[9]; //xx:xx:xx;
float fltvoltage = 220;
float fltcurrent;
float fltwatt;
-
8/18/2019 Program micro
2/7
2
float fltkwh = 0;
unsigned int counter = 0;
char* strkwh = "000";int intkwh;
int intKWH;
int intKWH_old = 0;
int intHarga = 1496;
void setup() {
Serial.begin(9600);
Wire.begin();
RTC.begin();
/// rtc.adjust(DateTime("Jan 01 2014","00:00:00"));
/// setTimer();if (! RTC.isrunning()) {
Serial.println("RTC is NOT running!");
RTC.adjust(DateTime("Jan 01 2014","00:00:00"));
}
lcd.begin(16, 2);
lcd.setCursor(0, 0);
lcd.print("EFISIENSI ENERGI");
lcd.setCursor(0, 1);
lcd.print(" - TERPROGRAM - ");
pinMode(relayPin, OUTPUT);
digitalWrite(relayPin, LOW);
delay(2000);
getKwh();
decodeKwh();
fltkwh = (float) intkwh;
// fltkwh = 12;
}
void loop() {
char val=0;
if( Serial.available() > 0){
val = char(Serial.read());
if (val == 'T')
{
setClock();
}
}
// Tunggu 2 detik
// delay(2000);
readData();
RTC_getDate();
-
8/18/2019 Program micro
3/7
3
RTC_getTime();
if ((date[0] == '0') && (date[1] == '1') && (time[6] == '0') && (time[7] == '0'))
{intkwh = 0;
fltkwh = 0.0;
codeKwh();
addKwh();
}
if ((time[4] == '5') && (time[6] == '5') && (time[7] == '9'))
{
getKwh();
decodeKwh();
fltkwh = (float) intkwh;
}
if ((time[0] == '1') && (time[1] == '7') && (time[3] == '3')&& (time[4] == '0'))
digitalWrite(relayPin, HIGH);
if ((time[0] == '0') && (time[1] == '6') && (time[3] == '3')&& (time[4] == '0'))
digitalWrite(relayPin, LOW);
fltkwh = fltkwh + ((fltvoltage * fltcurrent)/1800);
// fltwatt = (fltvoltage * fltcurrent);
intKWH = (int) fltkwh; // now i is 3
if (intKWH >= (intkwh+1))
{
intkwh = intKWH ;
codeKwh();
addKwh();
// intKWH_old = intKWH;
}
//Menampilkan data yang diterima melalui LCD
lcd.setCursor(0, 0);
lcd.print(" V A ");
lcd.setCursor(0, 0);
lcd.print(220);lcd.setCursor(5, 0);
lcd.print(fltcurrent);
lcd.setCursor(11, 0);
lcd.print(time[0]);
lcd.setCursor(12, 0);
lcd.print(time[1]);
lcd.setCursor(13, 0);
lcd.print(time[2]);
lcd.setCursor(14, 0);
lcd.print(time[3]);
lcd.setCursor(15, 0);
-
8/18/2019 Program micro
4/7
4
lcd.print(time[4]);
lcd.setCursor(0, 1);
lcd.print(" KWH RP. ");lcd.setCursor(0, 1);
lcd.print((int) fltkwh);
lcd.setCursor(11, 1);
lcd.print((int)(intHarga * fltkwh));
//Serial.print(f);
//Serial.print(" *F\t");
//Serial.print("Heat index: ");
//Serial.print(hi);
//Serial.println(" *F");
}
//Fungsi untuk membaca data dari sensor arus dan tegangan
void readData()
{
int c1;
int ctot1=0;
int c2;
int ctot2=0;
int v1;
int vtot1=0;
int v2;
int vtot2=0;
float fltcurrentin;
float fltcurrentintot = 0;
// read analog pins and format data
for (int i=0; i
-
8/18/2019 Program micro
5/7
5
v1 = analogRead(voltinPin);
delay(20);vtot1 = vtot1 + v1;
}
float currentin = fltcurrentintot / 20.0;
float voltinReading = vtot1 / 20.0;
// converting that reading to voltage, for 3.3v arduino use 3.3, for 5.0, use 5.0
// float currentin = (((currentinReading * aref_voltage) / 1024.0) - 2.5)/0.120;
float voltagein = ((voltinReading * aref_voltage) / 1024.0) * (42000/10000.0) *
(235/17.5);
/*
if (currentin
-
8/18/2019 Program micro
6/7
6
time[5] = ':';
time[4] = (tm.minute() % 10) | 0x30;
time[3] = (tm.minute() / 10) | 0x30;time[2] = ':';
time[1] = (tm.hour() % 10) | 0x30;
time[0] = (tm.hour() / 10) | 0x30;
return 0;
}
//Fungsi untuk menyimpan nilai daya (KWH) yang sudah digunakan
void addKwh()
{
for(int i = 0; i < 3; i++)
{EEPROM.write(i, strkwh[i]);
delay(100);
}
}
void codeKwh()
{
char chrkwh[3];
String str;
str = String(intkwh);
str.toCharArray(chrkwh,3);
strkwh = chrkwh;
}
//Fungsi mengambil data yang sudah disimpan
void getKwh()
{
for(int i = 0; i < 3; i++)
{
byte r = EEPROM.read(i);
strkwh[i] = r;
delay(100);}
}
void decodeKwh()
{
intkwh = atoi(strkwh);
}
//Fungsi untuk setting jam
void setClock()
{
-
8/18/2019 Program micro
7/7
7
int serbytes=0;
char date2[12];
char time2[9];Serial.println("Masukkan Tanggal (Jan 01 2015)");
getSerStr(date2, 11);
Serial.println("Masukkan Waktu (01:25:03)");
getSerStr(time2, 8);
if (! RTC.isrunning()) {
RTC.adjust(DateTime("Jan 01 2015","00:00:00"));
delay(500);
}
RTC.adjust(DateTime(date2,time2));
delay(500);
DateTime tm = RTC.now();
cout