Advertisement
Guest User

Untitled

a guest
Jun 7th, 2012
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.22 KB | None | 0 0
  1. #include <LiquidCrystal.h>
  2. #include "DHT.h"
  3. #include <Wire.h>
  4. #include "RTClib.h"
  5. #include <PinChangeInt.h>
  6. #include <avr/interrupt.h>  
  7. #include <avr/io.h>
  8. #include <SimpleTimer.h>
  9.  
  10. int temp_pin = 2;
  11. int lcdbg_pin = 5;
  12. float ver = 0.1;
  13.  
  14. LiquidCrystal lcd(12, 14, 11, 7, 8, 9, 10);
  15. DHT dht(temp_pin, DHT11);
  16. RTC_DS1307 RTC;
  17. SimpleTimer timer;
  18.  
  19. int clockrunning = 0;
  20. int page = 0;
  21. int servopos = 0;
  22. int fanspeed = 0;
  23.  
  24. int humidity = 0;
  25. int oldhumidity = 0;
  26. int temperature = 0;
  27. int oldtemperature = 0;
  28. unsigned int time_hour = 00;
  29. unsigned int time_minute = 00;
  30. unsigned int time_second = 00;
  31. unsigned int toggle = 0;  
  32. unsigned int count = 0;  
  33.  
  34. void readtemp(){
  35.   oldtemperature = temperature;
  36.   oldhumidity = humidity;  
  37.   temperature = dht.readTemperature();
  38.   humidity = dht.readHumidity();
  39. }
  40.  
  41. void readtime(){
  42.   DateTime now = RTC.now();
  43.   time_hour = now.hour();
  44.   time_minute = now.minute();
  45.   time_second = now.second();
  46. }
  47.  
  48. ISR(TIMER2_OVF_vect) {
  49.   count++;               //Increments the interrupt counter
  50.   if(count > 999){
  51.     toggle = !toggle;    //toggles the LED state
  52.     count = 0;           //Resets the interrupt counter
  53.   }
  54.   digitalWrite(13,toggle);
  55.   TCNT2 = 130;           //Reset Timer to 130 out of 255
  56.   TIFR2 = 0x00;          //Timer2 INT Flag Reg: Clear Timer Overflow Flag
  57. };  
  58.  
  59. void initialize(){
  60.   // Welcome Message + Fade In//
  61.   lcd.setCursor(0,0);
  62.   lcd.print("   E-Pot v");
  63.   lcd.print(ver);  
  64.   lcd.setCursor(0,1);
  65.   lcd.print("    Welcome!    ");
  66.   for(int fadeValue = 0 ; fadeValue <= 220; fadeValue +=5) {
  67.     analogWrite(lcdbg_pin, fadeValue);          
  68.     delay(40);                            
  69.   }
  70.   delay(1000);
  71.   lcd.clear();
  72.   // End Welcome Message//
  73.  
  74.   lcd.setCursor(0,0);
  75.   lcd.print("Testing Clock .. ");
  76.   while(! RTC.isrunning()){
  77.     delay(100);
  78.   }
  79.   lcd.setCursor(0,1);
  80.   lcd.print("       OK       ");  
  81.   delay(1000);
  82.   lcd.clear();
  83.  
  84.   lcd.setCursor(0,0);
  85.   lcd.print("Checking Fan .. ");
  86.   lcd.setCursor(0,1);
  87.   lcd.print("       OK       ");  
  88.   delay(1000);
  89.   lcd.clear();
  90.  
  91.   lcd.setCursor(0,0);
  92.   lcd.print("Checking T/H .. ");
  93.   if(!isnan(dht.readHumidity())){
  94.     lcd.setCursor(0,1);
  95.     lcd.print("       OK       ");  
  96.   } else {
  97.     lcd.setCursor(0,1);
  98.     die("T/H Error");
  99.   }
  100.   delay(1000);
  101.      
  102.   lcd.clear();
  103.   lcd.setCursor(0,1);
  104.   lcd.print("  Initialized!  ");
  105.   delay(2000);
  106.   lcd.clear();
  107.  
  108.   timer.setInterval(5000, readtemp);
  109.   readtemp();
  110.   pinMode(13, OUTPUT);
  111.   TCCR2B = 0x00;        //Disbale Timer2 while we set it up
  112.   TCNT2  = 130;         //Reset Timer Count to 130 out of 255
  113.   TIFR2  = 0x00;        //Timer2 INT Flag Reg: Clear Timer Overflow Flag
  114.   TIMSK2 = 0x01;        //Timer2 INT Reg: Timer2 Overflow Interrupt Enable
  115.   TCCR2A = 0x00;        //Timer2 Control Reg A: Normal port operation, Wave Gen Mode normal
  116.   TCCR2B = 0x05;        //Timer2 Control Reg B: Timer Prescaler set to 128
  117. }
  118.  
  119. void die(String message){
  120.     lcd.clear();
  121.     lcd.setCursor(0,0);
  122.     lcd.print("!!");
  123.     lcd.print(message);
  124.     lcd.print("!!");
  125.     while(1>0) {
  126.       digitalWrite(lcdbg_pin, HIGH);          
  127.       delay(1000);      
  128.       digitalWrite(lcdbg_pin, LOW);          
  129.       delay(1000);          
  130.     }
  131. }
  132.  
  133. void setup()
  134. {
  135.   PCintPort::attachInterrupt(A0, button_pressed, FALLING);
  136.   PCintPort::attachInterrupt(A1, button_pressed, FALLING);
  137.   PCintPort::attachInterrupt(A2, button_pressed, FALLING);
  138.   PCintPort::attachInterrupt(A3, button_pressed, FALLING);  
  139.   Wire.begin();
  140.   RTC.begin();
  141.   lcd.begin(16, 2);
  142.   dht.begin();
  143.   pinMode(lcdbg_pin, OUTPUT);
  144.   initialize();
  145.  }
  146.  
  147. void displaytemp(){
  148.   lcd.setCursor(0,1);
  149.   lcd.print(temperature);
  150.   lcd.print((char)223);
  151.   lcd.print("C");
  152.   lcd.print(humidity);
  153.   lcd.print("%");
  154. }
  155.  
  156. void displaytime(){
  157.   readtime();
  158.   lcd.setCursor(0,0);
  159.   lcd.print("                ");
  160.   lcd.setCursor(4,0);
  161.   lcd.print(time_hour, DEC);
  162.   lcd.print(":");
  163.   lcd.print(time_minute, DEC);
  164.   lcd.print(":");  
  165.   lcd.print(time_second, DEC);
  166. }
  167.  
  168. void changefanspeed(int upordown){
  169.   page = 0;
  170.   if(upordown == 1){
  171.     if(servopos < 100){
  172.        servopos = servopos + 5;  
  173.     } else {
  174.       servopos = 100;
  175.     }
  176.   }
  177.   if(upordown == -1){
  178.     if(servopos > 60){
  179.        servopos = servopos - 5;  
  180.     } else {
  181.       servopos = 60;
  182.     }
  183.   }
  184.  
  185.   fanspeed = map(servopos, 60, 100, 0, 100);
  186.   page = 2;
  187. }
  188. void loop(){
  189.   timer.run();
  190.   if(page == 0){
  191.     lcd.clear();
  192.     displaytime();
  193.     displaytemp();
  194.     delay(1000);
  195.   }
  196.   else if(page == 0) {
  197.     lcd.clear();
  198.     lcd.setCursor(0,0);
  199.     lcd.print("Hey Yaron :3");
  200.     delay(1000);
  201.   }
  202.    else if(page == 2) {
  203.     lcd.clear();
  204.     lcd.setCursor(0,0);
  205.     lcd.print("Fan Speed:");
  206.     lcd.print(fanspeed);
  207.     lcd.print("%");
  208.     page = 0;
  209.     delay(1000);
  210.   }
  211. }
  212.  
  213. void button_pressed(){
  214.   int pressedpin = PCintPort::arduinoPin;
  215.   if(pressedpin == 15){
  216.     if(page < 1){
  217.       page++;
  218.     } else{
  219.      page = 0;
  220.     }
  221.   }
  222.   if(pressedpin == 14){
  223.     if(page > 0){
  224.       page--;
  225.     } else {
  226.      page = 1;
  227.     }
  228.   }
  229.   if(pressedpin == 16){
  230.     changefanspeed(1);
  231.   }
  232.   if(pressedpin == 17){
  233.     changefanspeed(-1);
  234.   }
  235.  
  236. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement