Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <LiquidCrystal.h>
- #include "DHT.h"
- #include <Wire.h>
- #include "RTClib.h"
- #include <PinChangeInt.h>
- #include <avr/interrupt.h>
- #include <avr/io.h>
- #include <SimpleTimer.h>
- int temp_pin = 2;
- int lcdbg_pin = 5;
- float ver = 0.1;
- LiquidCrystal lcd(12, 14, 11, 7, 8, 9, 10);
- DHT dht(temp_pin, DHT11);
- RTC_DS1307 RTC;
- SimpleTimer timer;
- int clockrunning = 0;
- int page = 0;
- int servopos = 0;
- int fanspeed = 0;
- int humidity = 0;
- int oldhumidity = 0;
- int temperature = 0;
- int oldtemperature = 0;
- unsigned int time_hour = 00;
- unsigned int time_minute = 00;
- unsigned int time_second = 00;
- unsigned int toggle = 0;
- unsigned int count = 0;
- void readtemp(){
- oldtemperature = temperature;
- oldhumidity = humidity;
- temperature = dht.readTemperature();
- humidity = dht.readHumidity();
- }
- void readtime(){
- DateTime now = RTC.now();
- time_hour = now.hour();
- time_minute = now.minute();
- time_second = now.second();
- }
- ISR(TIMER2_OVF_vect) {
- count++; //Increments the interrupt counter
- if(count > 999){
- toggle = !toggle; //toggles the LED state
- count = 0; //Resets the interrupt counter
- }
- digitalWrite(13,toggle);
- TCNT2 = 130; //Reset Timer to 130 out of 255
- TIFR2 = 0x00; //Timer2 INT Flag Reg: Clear Timer Overflow Flag
- };
- void initialize(){
- // Welcome Message + Fade In//
- lcd.setCursor(0,0);
- lcd.print(" E-Pot v");
- lcd.print(ver);
- lcd.setCursor(0,1);
- lcd.print(" Welcome! ");
- for(int fadeValue = 0 ; fadeValue <= 220; fadeValue +=5) {
- analogWrite(lcdbg_pin, fadeValue);
- delay(40);
- }
- delay(1000);
- lcd.clear();
- // End Welcome Message//
- lcd.setCursor(0,0);
- lcd.print("Testing Clock .. ");
- while(! RTC.isrunning()){
- delay(100);
- }
- lcd.setCursor(0,1);
- lcd.print(" OK ");
- delay(1000);
- lcd.clear();
- lcd.setCursor(0,0);
- lcd.print("Checking Fan .. ");
- lcd.setCursor(0,1);
- lcd.print(" OK ");
- delay(1000);
- lcd.clear();
- lcd.setCursor(0,0);
- lcd.print("Checking T/H .. ");
- if(!isnan(dht.readHumidity())){
- lcd.setCursor(0,1);
- lcd.print(" OK ");
- } else {
- lcd.setCursor(0,1);
- die("T/H Error");
- }
- delay(1000);
- lcd.clear();
- lcd.setCursor(0,1);
- lcd.print(" Initialized! ");
- delay(2000);
- lcd.clear();
- timer.setInterval(5000, readtemp);
- readtemp();
- pinMode(13, OUTPUT);
- TCCR2B = 0x00; //Disbale Timer2 while we set it up
- TCNT2 = 130; //Reset Timer Count to 130 out of 255
- TIFR2 = 0x00; //Timer2 INT Flag Reg: Clear Timer Overflow Flag
- TIMSK2 = 0x01; //Timer2 INT Reg: Timer2 Overflow Interrupt Enable
- TCCR2A = 0x00; //Timer2 Control Reg A: Normal port operation, Wave Gen Mode normal
- TCCR2B = 0x05; //Timer2 Control Reg B: Timer Prescaler set to 128
- }
- void die(String message){
- lcd.clear();
- lcd.setCursor(0,0);
- lcd.print("!!");
- lcd.print(message);
- lcd.print("!!");
- while(1>0) {
- digitalWrite(lcdbg_pin, HIGH);
- delay(1000);
- digitalWrite(lcdbg_pin, LOW);
- delay(1000);
- }
- }
- void setup()
- {
- PCintPort::attachInterrupt(A0, button_pressed, FALLING);
- PCintPort::attachInterrupt(A1, button_pressed, FALLING);
- PCintPort::attachInterrupt(A2, button_pressed, FALLING);
- PCintPort::attachInterrupt(A3, button_pressed, FALLING);
- Wire.begin();
- RTC.begin();
- lcd.begin(16, 2);
- dht.begin();
- pinMode(lcdbg_pin, OUTPUT);
- initialize();
- }
- void displaytemp(){
- lcd.setCursor(0,1);
- lcd.print(temperature);
- lcd.print((char)223);
- lcd.print("C");
- lcd.print(humidity);
- lcd.print("%");
- }
- void displaytime(){
- readtime();
- lcd.setCursor(0,0);
- lcd.print(" ");
- lcd.setCursor(4,0);
- lcd.print(time_hour, DEC);
- lcd.print(":");
- lcd.print(time_minute, DEC);
- lcd.print(":");
- lcd.print(time_second, DEC);
- }
- void changefanspeed(int upordown){
- page = 0;
- if(upordown == 1){
- if(servopos < 100){
- servopos = servopos + 5;
- } else {
- servopos = 100;
- }
- }
- if(upordown == -1){
- if(servopos > 60){
- servopos = servopos - 5;
- } else {
- servopos = 60;
- }
- }
- fanspeed = map(servopos, 60, 100, 0, 100);
- page = 2;
- }
- void loop(){
- timer.run();
- if(page == 0){
- lcd.clear();
- displaytime();
- displaytemp();
- delay(1000);
- }
- else if(page == 0) {
- lcd.clear();
- lcd.setCursor(0,0);
- lcd.print("Hey Yaron :3");
- delay(1000);
- }
- else if(page == 2) {
- lcd.clear();
- lcd.setCursor(0,0);
- lcd.print("Fan Speed:");
- lcd.print(fanspeed);
- lcd.print("%");
- page = 0;
- delay(1000);
- }
- }
- void button_pressed(){
- int pressedpin = PCintPort::arduinoPin;
- if(pressedpin == 15){
- if(page < 1){
- page++;
- } else{
- page = 0;
- }
- }
- if(pressedpin == 14){
- if(page > 0){
- page--;
- } else {
- page = 1;
- }
- }
- if(pressedpin == 16){
- changefanspeed(1);
- }
- if(pressedpin == 17){
- changefanspeed(-1);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement