Advertisement
Guest User

newSoftSerial and IRremote

a guest
Nov 20th, 2010
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 10.04 KB | None | 0 0
  1.  
  2.  
  3. #include <LiquidCrystal.h>
  4. #include <avr/pgmspace.h>
  5. #include <Time.h>
  6. #include <IRremote.h>
  7. #include <NewSoftSerial.h>
  8.  
  9.  
  10. #define recvPin 10 //Pin na katerem je IR sprejemnik
  11. #define virtualSerialRX 6
  12. #define virtualSerialTX 7
  13. #define onRelay 9
  14. #define offRelay 8
  15. #define TONE_PIN 13
  16.  
  17. #define an_gumbi 0
  18. #define an_light 1
  19.  
  20.  
  21.  
  22.  
  23.  
  24.  
  25.  
  26. NewSoftSerial mySerial(virtualSerialRX, virtualSerialTX);
  27.  
  28.  
  29.  
  30.  
  31.  
  32.  
  33. IRrecv irrecv(recvPin);
  34. IRsend irsend;
  35. decode_results results;
  36.  
  37.  
  38.  
  39.  
  40. short int st_oseb = 1;
  41. boolean luc_prizgana = false;
  42. unsigned short int svetlost = 0;
  43. unsigned long int cas_off = 0;
  44.  
  45.  
  46.  
  47. byte smiley[8] = {
  48.   B01110,
  49.   B10001,
  50.   B11011,
  51.   B10101,
  52.   B11111,
  53.   B10001,
  54.   B01110,
  55. };
  56.  
  57.  
  58.  
  59.  
  60.  
  61. //Flash based string table. Necessary because otherwise the strings will take up all of our ram.
  62. prog_char string_0[] PROGMEM =   "< Luc on/off >";
  63. prog_char string_1[] PROGMEM =   "< Budilka >";
  64. prog_char string_2[] PROGMEM =   "3. DODAJ UPORABNIKA";
  65. prog_char string_3[] PROGMEM =   "< Nastavi uro >";
  66. prog_char string_4[] PROGMEM =   "< Pokazi RAM >";
  67. prog_char string_5[] PROGMEM =   "< Zapri meni >";
  68. prog_char string_6[] PROGMEM =   "                  ";
  69. prog_char string_7[] PROGMEM =   "V teku...";
  70. prog_char string_8[] PROGMEM =   "Napaka!";
  71. prog_char string_9[] PROGMEM =   "Koncano!";
  72. PROGMEM const char *StringTable[] = {string_0, string_1, string_2, string_3, string_4, string_5, string_6, string_7, string_8, string_9};
  73.  
  74.  
  75. //Zacasni spremenljivki, ki jih uporabljamo znotraj programa - tako prihranimo na RAMu
  76. char buffer[21];
  77. char minibuff[5];
  78.  
  79.  
  80. time_t cas;
  81. boolean budilka_nastavljena;
  82. unsigned short int budilka_ura;
  83. unsigned short int budilka_minuta;
  84.  
  85.  
  86. short int meniOpcija = 0;
  87.  
  88.  
  89. LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
  90.  
  91.  
  92.  
  93.  
  94.  
  95.  
  96.  
  97.  
  98.  
  99. void setup()
  100. {    
  101.   pinMode(onRelay, OUTPUT);
  102.   pinMode(offRelay, OUTPUT);
  103.   pinMode(TONE_PIN, OUTPUT);
  104.  
  105.  
  106.   mySerial.begin(9600);
  107.  
  108.   irrecv.enableIRIn();
  109.  
  110.    Serial.begin(9600);
  111.    Serial.println("start");
  112.  
  113.    lcd.createChar(0, smiley);
  114.  
  115.  
  116.   lcd.begin(16, 2);
  117.  
  118.  
  119.   //lcd.print("Start now");
  120.   lcd.clear();
  121.  
  122.   //availableMemory();
  123. }
  124.  
  125.  
  126.  
  127.  
  128.  
  129. //Funkcija systemSync poskrbi za nastavljanje ure in datuma (poslje zahtevek za uro na racunalnik in prebere rezultate)
  130. void systemSync()
  131. {
  132.   lcd.clear();
  133.   lcd.setCursor(0,1);
  134.   strcpy_P(buffer, (char*)pgm_read_word(&(StringTable[7]))); //Izpiši "V teku..."
  135.   lcd.print(buffer);
  136.  
  137.   Serial.flush();
  138.   Serial.print("IN\0");
  139.   delay(5000);
  140.   unsigned int index = 0;
  141.  
  142.   while (Serial.available() > 0)
  143.   {
  144.     buffer[index] = Serial.read();
  145.     index++;
  146.    
  147.     if (index > 14)
  148.     {
  149.       break;
  150.     }
  151.   }
  152.  
  153.   unsigned short int i = 0;
  154.   if (buffer[i] == 'N' && buffer[i+1] == 'C' && buffer[i+2] == '=')
  155.   {
  156.    
  157.     unsigned short int ura = 0;
  158.     unsigned short int minuta = 0;
  159.     unsigned short int dan = 0;
  160.     unsigned short int mesec = 0;
  161.     unsigned short int leto = 0;
  162.      
  163.     minibuff[0] = buffer[i+3];
  164.     minibuff[1] = buffer[i+4];
  165.     minibuff[2] = '\0';
  166.     ura = atoi(minibuff);
  167.      
  168.     minibuff[0] = buffer[i+5];
  169.     minibuff[1] = buffer[i+6];
  170.     minibuff[2] = '\0';
  171.     minuta = atoi(minibuff);
  172.      
  173.     minibuff[0] = buffer[i+7];
  174.     minibuff[1] = buffer[i+8];
  175.     minibuff[2] = '\0';
  176.     dan = atoi(minibuff);
  177.      
  178.     minibuff[0] = buffer[i+9];
  179.     minibuff[1] = buffer[i+10];
  180.     minibuff[2] = '\0';
  181.     mesec = atoi(minibuff);
  182.      
  183.     minibuff[0] = buffer[i+11];
  184.     minibuff[1] = buffer[i+12];
  185.     minibuff[2] = buffer[i+13];
  186.     minibuff[3] = buffer[i+14];
  187.     minibuff[4] = '\0';
  188.     leto = atoi(minibuff);
  189.      
  190.     setTime(ura, minuta, 0, dan, mesec, leto);
  191.   }
  192.  
  193.   if (timeStatus() == timeSet || timeStatus() == timeNeedsSync)
  194.   {
  195.     lcd.clear();
  196.     lcd.setCursor(0,1);
  197.     strcpy_P(buffer, (char*)pgm_read_word(&(StringTable[9])));
  198.     lcd.print(buffer);
  199.     cas = now();
  200.   }
  201.   else
  202.   {
  203.     lcd.clear();
  204.     lcd.setCursor(0,1);
  205.     strcpy_P(buffer, (char*)pgm_read_word(&(StringTable[8])));
  206.     lcd.print(buffer);
  207.   }
  208.  
  209.   delay(3000);
  210. }
  211.  
  212.  
  213.  
  214.  
  215.  
  216.  
  217.  
  218.  
  219.  
  220. void showFirstLine()
  221. {
  222.     strcpy_P(buffer, (char*)pgm_read_word(&(StringTable[6])));
  223.     lcd.setCursor(0,0);
  224.     lcd.print(buffer);
  225.    
  226.    
  227. }
  228.  
  229. void showHomeScreen()
  230. {
  231.   cas = now();
  232.  
  233.   strcpy_P(buffer, (char*)pgm_read_word(&(StringTable[6])));
  234.  
  235.   itoa(hour(cas), minibuff, 10);
  236.   if (hour(cas) < 10)
  237.   {
  238.     buffer[0] = '0';
  239.     buffer[1] = minibuff[0];
  240.   }
  241.   else
  242.   {
  243.     buffer[0] = minibuff[0];
  244.     buffer[1] = minibuff[1];
  245.   }
  246.   buffer[2] = ':';
  247.  
  248.   itoa(minute(cas), minibuff, 10);
  249.   if (minute(cas) < 10)
  250.   {
  251.     buffer[3] = '0';
  252.     buffer[4] = minibuff[0];
  253.   }
  254.   else
  255.   {
  256.     buffer[3] = minibuff[0];
  257.     buffer[4] = minibuff[1];
  258.   }
  259.  
  260.   itoa(day(cas), minibuff, 10);
  261.   if (day(cas) < 10)
  262.   {
  263.     buffer[6] = '0';
  264.     buffer[7] = minibuff[0];
  265.   }
  266.   else
  267.   {
  268.     buffer[6] = minibuff[0];
  269.     buffer[7] = minibuff[1];
  270.   }
  271.   buffer[8] = '.';
  272.  
  273.   itoa(month(cas), minibuff, 10);
  274.   if (month(cas) < 10)
  275.   {
  276.     buffer[9] = '0';
  277.     buffer[10] = minibuff[0];
  278.   }
  279.   else
  280.   {
  281.     buffer[9] = minibuff[0];
  282.     buffer[10] = minibuff[1];
  283.   }
  284.   buffer[11] = '.';
  285.  
  286.   itoa(year(cas), minibuff, 10);
  287.   buffer[12] = minibuff[0];
  288.   buffer[13] = minibuff[1];
  289.   buffer[14] = minibuff[2];
  290.   buffer[15] = minibuff[3];
  291.  
  292.   lcd.setCursor(0,0);
  293.   lcd.print(buffer);
  294.  
  295.   strcpy_P(buffer, (char*)pgm_read_word(&(StringTable[6])));
  296.   //lcd.setCursor(0,1);
  297.   //lcd.print(buffer);
  298.  
  299.   lcd.setCursor(0,1);
  300.   lcd.write(0);
  301.  
  302.   itoa(st_oseb, minibuff, 10);
  303.   if (st_oseb < 10)
  304.   {
  305.     buffer[0] = minibuff[0];
  306.     buffer[1] = ' ';
  307.   }
  308.   else
  309.   {
  310.     buffer[0] = minibuff[0];
  311.     buffer[1] = minibuff[1];
  312.   }
  313.   buffer[2] = ' ';
  314.  
  315.   lcd.print(buffer);
  316.  
  317.  
  318. }
  319.  
  320.  
  321. int availableMemory() {
  322.   int size = 2048; // Use 2048 with ATmega328
  323.   byte *buf;
  324.  
  325.   while ((buf = (byte *) malloc(--size)) == NULL)
  326.     ;
  327.  
  328.   free(buf);
  329.  
  330.  
  331.   return size;
  332. }
  333.  
  334.  
  335.  
  336.  
  337.  
  338.  
  339.  
  340.  
  341.  
  342.  
  343. void scanSerial()
  344. {
  345.   if (mySerial.available() > 0)
  346.   {
  347.     minibuff[0] = mySerial.read();
  348.     delay(500);
  349.     if (minibuff[0] == 'S')
  350.     {
  351.       minibuff[1] = mySerial.read();
  352.       minibuff[2] = mySerial.read();
  353.       if (minibuff[1] == 'T' && minibuff[2] == '=')
  354.       {
  355.         minibuff[0] = mySerial.read();
  356.         minibuff[1] = mySerial.read();
  357.         minibuff[2] = '\0';
  358.        
  359.         short int osebice = atoi(minibuff);
  360.         Serial.println("poslana vrednost");
  361.         Serial.println(osebice);
  362.        
  363.         autoLight(st_oseb, osebice);
  364.        
  365.         if (osebice != st_oseb)
  366.         {
  367.           noise(true);
  368.           noise(true);
  369.           noise(false);
  370.         }
  371.        
  372.         st_oseb = osebice;
  373.        
  374.       }
  375.     }
  376.   }
  377. }
  378.  
  379.  
  380.  
  381.  
  382.  
  383.  
  384.  
  385.  
  386.  
  387.  
  388. void scanRemote()
  389. {
  390.   if (irrecv.decode(&results))
  391.   {
  392.     switch (results.value)
  393.     {
  394.       case 0x20DF30CF: Serial.println("Power pritisnjen"); digitalWrite(onRelay, HIGH); delay(1000); digitalWrite(onRelay, LOW); break;
  395.       case 0x20DFB04F: Serial.print("Svetloba: "); Serial.println(svetlost);break;
  396.       case 0x20DFB847: mySerial.print("ST=0"); mySerial.print(st_oseb+1); mySerial.print("\0"); delay(100); Serial.println("play"); break;
  397.       case 0x20DFB24D: mySerial.print("ST=0"); mySerial.print(st_oseb-1); mySerial.print("\0"); delay(100); Serial.println("pauza"); break;
  398.       case 0x20DF0AF5: Serial.println("luc prizigam..."); prizgiLuc(); break;
  399.     }
  400.    
  401.     irrecv.resume();
  402.   }
  403. }
  404.  
  405.  
  406.  
  407.  
  408.  
  409.  
  410.  
  411.  
  412. void scanButton()
  413. {
  414.   int prebrano = analogRead(0);
  415.  
  416.   if (prebrano <= 10)
  417.   {
  418.   }
  419.   else if (prebrano <= 30)
  420.   {
  421.     meniOpcija++;
  422.     if (meniOpcija > 6)
  423.     {
  424.       meniOpcija = 1;
  425.     }
  426.     showMenu();
  427.   }
  428.   else if (prebrano <= 120)
  429.   {
  430.     meniOpcija--;
  431.     if (meniOpcija < 1)
  432.     {
  433.       meniOpcija = 6;
  434.     }
  435.     showMenu();
  436.   }
  437.   else if (prebrano <= 250)
  438.   {
  439.     switch (meniOpcija)
  440.     {
  441.       case 0: break;
  442.       case 1: (luc_prizgana == true ? ugasniLuc() : prizgiLuc()); break; //if luc == prizgana -> ugasni luc, if luc == ugasnjena -> prizgi luc
  443.       case 2: break;
  444.       case 3: break;
  445.       case 4: systemSync(); break;
  446.       case 5: lcd.clear(); lcd.setCursor(0,0); lcd.print("Prosti RAM: "); lcd.setCursor(0,1); lcd.print(availableMemory()); delay(5000); break;
  447.       case 6: meniOpcija = 0; break;
  448.     }
  449.     showMenu();
  450.   }
  451.   else
  452.   {
  453.   }
  454.  
  455. }
  456.  
  457.  
  458. void showMenu()
  459. {
  460.   if (meniOpcija == 0)
  461.   {
  462.     showHomeScreen();
  463.   }
  464.   else
  465.   {
  466.     showFirstLine();
  467.    
  468.     strcpy_P(buffer, (char*)pgm_read_word(&(StringTable[6])));
  469.     lcd.setCursor(0,1);
  470.     lcd.print(buffer);
  471.    
  472.     lcd.setCursor(0,1);
  473.     strcpy_P(buffer, (char*)pgm_read_word(&(StringTable[meniOpcija-1])));
  474.     lcd.print(buffer);
  475.   }
  476.  
  477.   delay(500);
  478.  
  479. }
  480.  
  481.  
  482.  
  483.  
  484.  
  485.  
  486. void autoLight(int old_st, int new_st)
  487. {
  488.   if (old_st == 0 && new_st > 0)
  489.   {
  490.     if (svetlost < 600 && luc_prizgana == false)
  491.     {
  492.       prizgiLuc();
  493.     }
  494.    
  495.     cas_off = 0;
  496.   }
  497.  
  498. }
  499.  
  500.  
  501. void ugasniLuc()
  502. {
  503.   digitalWrite(offRelay, HIGH);
  504.   delay(1000);
  505.   digitalWrite(offRelay, LOW);
  506.   luc_prizgana = false;
  507.   cas_off = 0;
  508. }
  509.  
  510. void prizgiLuc()
  511. {
  512.   digitalWrite(onRelay, HIGH);
  513.   delay(1000);
  514.   digitalWrite(onRelay, LOW);
  515.   luc_prizgana = true;
  516.   cas_off = 0;
  517. }
  518.  
  519.  
  520.  
  521.  
  522.  
  523. void scanSensor()
  524. {
  525.  
  526.   svetlost = analogRead(an_light);
  527.  
  528.  
  529.   if ((st_oseb <= 0) && (luc_prizgana == true))
  530.   {
  531.     if (cas_off == 0)
  532.     {
  533.       cas_off = millis();
  534.       Serial.println("Ugasanje...");
  535.     }
  536.    
  537.     if (millis() > cas_off + 300000)
  538.     {
  539.       ugasniLuc();
  540.       Serial.println("Ugasnjeno!");
  541.     }
  542.    
  543.   }
  544.  
  545.  
  546.  
  547.  
  548. }
  549.  
  550.  
  551.  
  552.  
  553.  
  554. void noise(boolean up)
  555. {
  556.   if (up == true)
  557.   {
  558.     tone(TONE_PIN, 1175);
  559.     delay(170);
  560.     noTone(TONE_PIN);
  561.   }
  562.   else
  563.   {
  564.     tone(TONE_PIN, 784);
  565.     delay(170);
  566.     noTone(TONE_PIN);
  567.   }
  568. }
  569.  
  570.  
  571.  
  572.  
  573.  
  574. void loop()
  575. {
  576.   scanSensor();
  577.   scanSerial();
  578.   scanButton();
  579.   scanRemote();
  580.   if (meniOpcija == 0)
  581.   {
  582.     showHomeScreen();
  583.   }
  584.  
  585.  
  586.  
  587.  
  588. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement