1. // include the library code:
  2. #include <LiquidCrystal.h>
  3. #include <OneWire.h>
  4. #define  lcdBacklightPin    10
  5.  
  6.  
  7. OneWire  ds(3);      // Temperature sensor on pin 5
  8. int x = 1;           //Variable to get out of while loop
  9. // initialize the library with the numbers of the interface pins
  10. LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
  11. float temp_c,temp_f;
  12. int y;
  13. int state = 0;
  14. int right=0,left=0,up=0,down=0;
  15. int sel=0;
  16. unsigned long up_time_1,up_time_2,up_time;
  17. double time_1,time_2,time;
  18. int last_st=0,st=0;
  19.  
  20.  
  21. void setup() {
  22.   // set up the LCD's number of columns and rows:
  23.   lcd.begin(16, 2);
  24.   pinMode( lcdBacklightPin, INPUT );
  25.   digitalWrite(lcdBacklightPin, LOW);
  26.   // Print a message to the LCD.
  27.   lcd.setCursor(0,0);
  28.   lcd.print("Control center");
  29.   lcd.setCursor(0,1);
  30.   lcd.print("Choose an option");
  31.   up_time_1 = millis();
  32.   time_1 = millis();  
  33. }
  34.  
  35. void loop() {
  36.  
  37.   time_2 = millis();
  38.   last_st = state;
  39.   read_state();
  40.   st = state;
  41.   if(st != last_st){
  42.     process_state();
  43.     process_display();
  44.     //delay(45);
  45.     time_1 = millis();
  46.   }
  47.   time = time_2 - time_1;
  48.   time = time/1000;
  49.   if(time >=10){
  50.     pinMode( lcdBacklightPin, OUTPUT);
  51.   }else{
  52.     pinMode( lcdBacklightPin, INPUT );
  53.   }
  54.   delay(90);
  55.  
  56.  
  57. }
  58.  
  59. void process_display(){
  60.  
  61.   switch (right) {
  62.     case 0:
  63.       lcd.clear();
  64.       lcd.setCursor(0,0);
  65.       lcd.print("Control center");
  66.       lcd.setCursor(0,1);
  67.       lcd.print("Choose an option");
  68.       break;
  69.     case 1:
  70.       lcd.clear();
  71.       lcd.setCursor(0,0);
  72.       lcd.print("Get temp 1");
  73.       lcd.setCursor(0,1);
  74.       lcd.print("Press select");
  75.       if(sel == 1){
  76.         get_temp();
  77.         sel = 0;
  78.         lcd.clear();
  79.         lcd.setCursor(0,0);
  80.         lcd.print("Temperature");
  81.         lcd.setCursor(0,1);
  82.         lcd.print(temp_c);
  83.         lcd.print("C//");
  84.         lcd.print(temp_f);
  85.         lcd.print("F");
  86.         delay(3000);
  87.       }
  88.       break;
  89.     case 2:
  90.       lcd.clear();
  91.       lcd.setCursor(0,0);
  92.       lcd.print("Get up time");
  93.       lcd.setCursor(0,1);
  94.       lcd.print("Press select");
  95.       if(sel == 1){
  96.         get_up_time();
  97.         sel = 0;
  98.         lcd.clear();
  99.         lcd.setCursor(0,0);
  100.         lcd.print("Up time");
  101.         lcd.setCursor(0,1);
  102.         lcd.print(up_time);
  103.         lcd.print(" sec");
  104.         delay(3000);
  105.       }
  106.       break;
  107.     case 3:
  108.       lcd.clear();
  109.       lcd.setCursor(0,0);
  110.       lcd.print("RxBuffer 10");
  111.       lcd.setCursor(0,1);
  112.       lcd.print("Press select");
  113.       if(sel == 1){
  114.         sel = 0;
  115.         lcd.setCursor(0,0);
  116.         lcd.print("VALUE OF 10");
  117.       }
  118.       break;
  119.        
  120.   }
  121.  
  122. }
  123.  
  124. void process_state(){
  125.  
  126.   switch (state) {
  127.     case 1://right
  128.       right = right + 1;
  129.       break;
  130.     case 2://up
  131.       up = up + 1;
  132.       break;
  133.     case 3://down
  134.       up = up - 1;
  135.       break;
  136.     case 4://left
  137.       right = right - 1;
  138.       break;
  139.     case 5:
  140.     break;
  141.    
  142.   }
  143.  
  144. }
  145.  
  146. void read_state(){
  147.  
  148.   state = 0;
  149.   y = analogRead (0);
  150.   //lcd.setCursor(10,1);
  151.   if (y < 100) {
  152.     //lcd.print ("Right ");
  153.     state = 1;
  154.   }
  155.   else if (y < 200) {
  156.     //lcd.print ("Up    ");
  157.     state = 2;
  158.   }
  159.   else if (y < 400){
  160.     //lcd.print ("Down  ");
  161.     state = 3;
  162.   }
  163.   else if (y < 600){
  164.     //lcd.print ("Left  ");
  165.     state = 4;
  166.   }
  167.   else if (y < 800){
  168.     //lcd.print ("Select");
  169.     state = 5;
  170.     sel = 1;
  171.   }
  172.  
  173. }
  174.  
  175. void get_up_time(){
  176.  
  177.   up_time_2 = millis();
  178.   up_time = up_time_2 - up_time_1;
  179.   up_time = up_time/1000;        //time in seconds
  180.  
  181.  
  182.      
  183. }
  184. void get_temp() {
  185.  
  186.   x = 1;
  187.   while(x == 1){
  188.       byte i;
  189.       byte present = 0;
  190.       byte type_s;
  191.       byte data[12];
  192.       byte addr[8];
  193.       float celsius, fahrenheit;
  194.      
  195.       if ( !ds.search(addr)) {
  196.         //Serial.println("No more addresses.");
  197.         //Serial.println();
  198.         ds.reset_search();
  199.         delay(250);
  200.         return;
  201.       }
  202.  
  203.       if (OneWire::crc8(addr, 7) != addr[7]) {
  204.           return;
  205.       }
  206.      
  207.       ds.reset();
  208.       ds.select(addr);
  209.       ds.write(0x44,1);         // start conversion, with parasite power on at the end
  210.      
  211.       present = ds.reset();
  212.       ds.select(addr);    
  213.       ds.write(0xBE);         // Read Scratchpad
  214.    
  215.       for ( i = 0; i < 9; i++) {           // we need 9 bytes
  216.         data[i] = ds.read();
  217.       }
  218.       // convert the data to actual temperature
  219.       unsigned int raw = (data[1] << 8) | data[0];
  220.       if (type_s) {
  221.         raw = raw << 3; // 9 bit resolution default
  222.         if (data[7] == 0x10) {
  223.           // count remain gives full 12 bit resolution
  224.           raw = (raw & 0xFFF0) + 12 - data[6];
  225.         }
  226.       } else {
  227.         byte cfg = (data[4] & 0x60);
  228.         if (cfg == 0x00) raw = raw << 3;  // 9 bit resolution, 93.75 ms
  229.         else if (cfg == 0x20) raw = raw << 2; // 10 bit res, 187.5 ms
  230.         else if (cfg == 0x40) raw = raw << 1; // 11 bit res, 375 ms
  231.         // default is 12 bit resolution, 750 ms conversion time
  232.       }
  233.       celsius = (float)raw / 16.0;
  234.       fahrenheit = celsius * 1.8 + 32.0;
  235.       x = 0;
  236.       temp_c = celsius;
  237.       temp_f = fahrenheit;
  238.      
  239.   }
  240. }