Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
48 views

Listing Program Master

The document contains code for an Arduino program that monitors temperature and humidity using DHT11 and LM35 sensors. It displays the readings on an LCD screen and transmits the data over I2C to a slave board. The program controls servos and LEDs based on sensor values and the time from an RTC module. It can receive commands over serial to control the servos and responds with the sensor readings in ASCII format.

Uploaded by

Supriyadi
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views

Listing Program Master

The document contains code for an Arduino program that monitors temperature and humidity using DHT11 and LM35 sensors. It displays the readings on an LCD screen and transmits the data over I2C to a slave board. The program controls servos and LEDs based on sensor values and the time from an RTC module. It can receive commands over serial to control the servos and responds with the sensor readings in ASCII format.

Uploaded by

Supriyadi
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 16

Program master

//Tab master

#include <Wire.h>
#include "I2C_Anything.h"
#include <LiquidCrystal.h>
#include "DHT.h"
#include <Servo.h>
#define tempPin1 A3
#define DHTPIN A0
#define DHTTYPE DHT11
#include "RTClib.h"
RTC_DS1307 RTC;
Servo myservo1;
Servo myservo2;
int pos;
float temperatureC1 ;
int temperatureC ;
float h2;
DHT dht(DHTPIN, DHTTYPE);
LiquidCrystal lcd(2, 3, 4, 5, 6, 7);
int ledPin1 = 8;
int ledPin2 = 9;
int ledPin3 = 10;
int ledKipas1 = 12;
int ledKipas2 = 13;
const byte SLAVE_ADDRESS = 42;
int VALUE;
char digit;
uint16_t satuan1, XVALUE, satuan, puluhan, ratusan, ribuan;

void setup()
{
myservo1.attach(11);
myservo1.write(0);
myservo2.attach(A2);
myservo2.write(0);
lcd.begin(16, 2);
Wire.begin (SLAVE_ADDRESS);
Serial.begin(9600);
Wire.onReceive (receiveEvent);
RTC.begin();
lcd.clear();
if (! RTC.isrunning()) {
lcd.println("RTC is NOT running!");
RTC.adjust(DateTime(__DATE__, __TIME__));
}
pinMode(ledPin1, OUTPUT);
digitalWrite(ledPin1, HIGH);
pinMode(ledPin2, OUTPUT);
digitalWrite(ledPin2, HIGH);
pinMode(ledPin3, OUTPUT);
digitalWrite(ledPin3, HIGH);
pinMode(ledKipas1, OUTPUT);
digitalWrite(ledKipas1, LOW);
pinMode(ledKipas2, OUTPUT);
digitalWrite(ledKipas2, LOW);
}
volatile boolean haveData = false;
volatile float tempC1;
volatile float h;

void loop()
{
rtc2();
Kelembaban();
LM35();
setting();
if (UDR0 == 'A')
{
if (haveData)
{
Serial.write(0x0D);
konversi_ascii2(tempC1,4);
Serial.write(32);
konversi_ascii2(h,3);
Serial.write(32);
konversi_ascii2(temperatureC,4);
Serial.write(32);
konversi_ascii2(h2+=38,3);
haveData = false;
delay(100);
{
while (Serial.available() == 0);
int val = Serial.read() - '0';
if (val == 0)
{
for(pos = 0; pos < 100; pos += 1)
{
myservo1.write(pos);
delay(70);

}
}
else if (val==1)
{
for(pos = 100; pos>=1; pos-=1)
{
myservo1.write(pos);
delay(70);
}
}
if (val==2)
{
for(pos = 0; pos < 100; pos += 1)
{
myservo2.write(pos);
delay(70);
}
}
else if (val==3)
{
for(pos = 100; pos>=1; pos-=1)
{
myservo2.write(pos);
delay(70);
}
}
}
}
}
else if (UDR0 == 'B')
{
}

}
void receiveEvent (int howMany)
{
if (howMany >= (sizeof tempC1) + (sizeof h))
{
I2C_readAnything (tempC1);
I2C_readAnything (h);
haveData = true;
}
}
void konversi_ascii2(uint16_t XVALUE, char digit)
{

if (XVALUE < 0 )
{
Serial.write('-');
XVALUE = -1 * XVALUE;
}
satuan = (XVALUE%10)+0x30;
puluhan = (XVALUE%100)/10;
puluhan += 0x30;
ratusan = (XVALUE % 1000)/100;
ratusan +=0x30;
char ribuan = (XVALUE%10000)/1000;
ribuan +=0x30;
char puluhanribu = (XVALUE%100000)/10000;
puluhanribu +=0x30;
if(digit == 1)
{
Serial.write(satuan);
}
else if(digit == 2)

{
Serial.write(puluhan);
Serial.write(satuan);
}
else if(digit == 3)
{
Serial.write(ratusan);
Serial.write(puluhan);
Serial.write(satuan);
}
else if(digit == 4)
{
Serial.write(ribuan);
Serial.write(ratusan);
Serial.write(puluhan);
Serial.write(satuan);
}
else if(digit == 5)
{
Serial.write(puluhanribu);
Serial.write(ribuan);
Serial.write(ratusan);
Serial.write(puluhan);
Serial.write(satuan);
}
}

//Tab DHT11

void Kelembaban()
{
h2 = (dht.readHumidity());
delay(50);
}

//Tab Lm35

void LM35()
{
temperatureC = analogRead(tempPin1);
temperatureC1 = (5.0*temperatureC*100.0/1024.0); //voltage calculation
delay(100);
}

//Tab I2C_Anything.h

#include <Arduino.h>
#include <Wire.h>
template <typename T> int I2C_writeAnything (const T& value)
{
const byte * p = (const byte*) &value;
unsigned int i;
for (i = 0; i < sizeof value; i++)
Wire.write(*p++);
return i;
}
template <typename T> int I2C_readAnything(T& value)

{
byte * p = (byte*) &value;
unsigned int i;
for (i = 0; i < sizeof value; i++)
*p++ = Wire.read();
return i;
}

//Tab RTC

void rtc2()
{
lcd.clear();
DateTime now = RTC.now();
lcd.setCursor(0,0);
lcd.print(now.hour());
lcd.print(':');
lcd.print(now.minute());
lcd.print(':');
lcd.print(now.second());
//lcd.println();
lcd.setCursor(9,0);
lcd.print(now.day());
lcd.print("/");
lcd.print(now.month());
lcd.print("/");
(now.year());
lcd.print("14");
lcd.setCursor(0,1);
lcd.print("H=");
lcd.print((int)(h2+=36),DEC);

lcd.print("%");
lcd.setCursor(7,1);
lcd.print("T=");
lcd.print(temperatureC1);
lcd.print("\337C");
if (now.hour()==6 && now.minute()==0 && now.second()==0)
{
for(pos = 0; pos < 100; pos += 1)
{
myservo1.write(pos);
myservo2.write(pos);
delay(50);
}
}
if (now.hour()==18 && now.minute()==0 && now.second()==0)
{
for(pos = 100; pos>=1; pos-=1)
{
myservo1.write(pos);
myservo2.write(pos);
delay(50);
}
}
if (now.day()==7 && now.month()==2 && now.year()==2014)
{
myservo1.write(0);
myservo2.write(0);
delay(50);
}
else if (now.day()==8 && now.month()==2 && now.year()==2014)
{

}
}
//Tab Setting_suhu

void setting()
{
if (temperatureC1 > 38)
{
digitalWrite(ledKipas1, HIGH);
}
else
{
digitalWrite(ledKipas1, LOW);
}
delay(100);
if (temperatureC1 >39)
{
digitalWrite(ledPin2,LOW);
}
else
{
digitalWrite(ledPin2,HIGH);
}
delay(100);
if (temperatureC1 > 40)
{
digitalWrite(ledPin3,LOW);
}
else
{
digitalWrite(ledPin3,HIGH);

}
delay(100);
if (temperatureC1 > 41)
{
digitalWrite(ledPin1,LOW);
}
else
{
digitalWrite(ledPin1,HIGH);
}
delay(100);
}

Program Slave

//Tab slave
#include <Wire.h>
#include <LiquidCrystal.h>
#include <dht11.h>
#include "I2C_Anything.h"
#include "DHT.h"
#define tempPin A1
#define DHTPIN A0
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
const byte MY_ADDRESS = 42;
int ledPin1 = 8;
int ledPin2 = 9;
int ledPin3 = 10;
int ledKipas1 = 12;
int ledKipas2 = 13;
LiquidCrystal lcd(2, 3, 4, 5, 6, 7);
float tempC1;
float h;
void setup()
{
lcd.begin(16, 2);
Wire.begin (MY_ADDRESS);
Serial.begin (9600);
pinMode(8,OUTPUT);
digitalWrite(8, HIGH);
pinMode(9,OUTPUT);
digitalWrite(9, HIGH);
pinMode(10,OUTPUT);
digitalWrite(10, HIGH);
pinMode(12,OUTPUT);

digitalWrite(12, LOW);
}
void loop()
{
delay(0);
{
kelembaban2();
LM352();
Setting2();
{
Wire.beginTransmission (MY_ADDRESS);
I2C_writeAnything (tempC1);
I2C_writeAnything (h);
Wire.endTransmission ();
delay (20);
}
}
}
//Tab DHT11
void kelembaban2()
{
h = (dht.readHumidity());
delay(50);
lcd.setCursor(0,0);
lcd.print("Humidity = ");
lcd.print((int)(h+=35),DEC);
lcd.print("%");
}

//Tab LM35

void LM352()
{
tempC1 = analogRead(tempPin);
tempC1 = (5.0*tempC1*100.0/1024.0);
delay(100);
lcd.setCursor(0,1);
lcd.print("Suhu = ");
lcd.print(tempC1);
lcd.print("\337C");
}

//Tab IC2_anything.h

#include <Arduino.h>
#include <Wire.h>
template <typename T> int I2C_writeAnything (const T& value)
{
const byte * p = (const byte*) &value;
unsigned int i;
for (i = 0; i < sizeof value; i++)
Wire.write(*p++);
return i;
}
template <typename T> int I2C_readAnything(T& value)
{
byte * p = (byte*) &value;
unsigned int i;
for (i = 0; i < sizeof value; i++)
*p++ = Wire.read();

return i;
}
//Tab setting_suhu

void Setting2()
{
if (tempC1 > 38)
{
digitalWrite(ledKipas1, HIGH);
}
else
{
digitalWrite(ledKipas1, LOW);
}
delay(100);
if (tempC1 > 39)
{
digitalWrite(ledPin2,LOW);
}
else
{
digitalWrite(ledPin2,HIGH);
}
delay(100);
if (tempC1 > 40)
{
digitalWrite(ledPin3,LOW);
}
else
{
digitalWrite(ledPin3,HIGH);

}
delay(100);
if (tempC1 > 41)
{
digitalWrite(ledPin1,LOW);
}
else
{
digitalWrite(ledPin1,HIGH);
}
delay(100);
}

You might also like