Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,027

HOME > กระทู้สำหรับภาษาต่าง ๆ > ลดจุดทศนิยมเหลือ 3ตำแหน่ง เพราะโค๊ดตอนนี้มันมีผลลัพธ์ออกมาแล้วมี จุดทศนิยมมากจนเกินความต้องการครับผม



ลดจุดทศนิยมเหลือ 3ตำแหน่ง เพราะโค๊ดตอนนี้มันมีผลลัพธ์ออกมาแล้วมี จุดทศนิยมมากจนเกินความต้องการครับผม

 
Topic : 100943

Guest



#include <LiquidCrystal.h> ///คำสั่ง #include แล้วตามด้วยไฟล์ที่อยู่ใน Folder ชื่อ Libraries 
#include <Wire.h>
#include <OneWire.h>
#include <RTClib.h> 


 
// select the pins used on the Temperature panel
int DS18S20_Pin = A7; //DS18S20 Signal pin on digital A7

//Temperature chip i/o
OneWire ds(DS18S20_Pin);  // on digital pin 2




LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
 RTC_DS1307 RTC;
// define some values used by the panel and buttons
int lcd_key     = 0;
int adc_key_in  = 0;
#define btnRIGHT  0
#define btnUP     1
#define btnDOWN   2
#define btnLEFT   3
#define btnSELECT 4
#define btnNONE   5
 
// read the buttons
int read_LCD_buttons()
{
 adc_key_in = analogRead(0);      // read the value from the sensor 
 // my buttons when read are centered at these valies: 0, 144, 329, 504, 741
 // we add approx 50 to those values and check to see if we are close
 if (adc_key_in > 1000) return btnNONE; // We make this the 1st option for speed reasons since it will be the most likely result
 // For V1.1 us this threshold
 if (adc_key_in < 50)   return btnRIGHT;  
 if (adc_key_in < 250)  return btnUP; 
 if (adc_key_in < 450)  return btnDOWN; 
 if (adc_key_in < 650)  return btnLEFT; 
 if (adc_key_in < 850)  return btnSELECT;  
 

 
 
 return btnNONE;  // when all others fail, return this...
}
 
void setup()
{
 lcd.begin(16, 2);              // start the library
 lcd.setCursor(0,0);
 Serial.begin(9600);
    Wire.begin();
    RTC.begin();
}
  
void loop()
{
 //lcd.setCursor(9,1);            // move cursor to second line "1" and 9 spaces over
 //lcd.print(millis()/1000);      // display seconds elapsed since power-up
 float temperature = getTemp();   
 int guess, buttonPressed;
 Serial.println(temperature);
 
 
 lcd.setCursor(0,1);            // move to the begining of the second line
 lcd_key = read_LCD_buttons();  // read the buttons
 
 switch (lcd_key)               // depending on which button was pushed, we perform an action
 {
   case btnRIGHT:
     {
       lcd.clear();
     lcd.print(" RIGHT ");
     delay(500);
     break;
     }
   case btnLEFT:
     {
       lcd.clear();
     lcd.print(" LEFT   ");
      delay(500);
     break;
     }
   case btnUP:
     {
       lcd.clear();
     lcd.print(" UP    ");
      delay(500);
     break;
     }
   case btnDOWN:
     {
       lcd.clear();
     lcd.print(" DOWN  ");
      delay(500);
     break;
     }
   case btnSELECT:
     {
            ///////////////////// select sensor /////////////////
                 do{
                 lcd_key = read_LCD_buttons();
                 lcd.clear();
                 lcd.print(" Temperature &");
                 lcd.setCursor(0,1);
                 lcd.print("Dissolved Oxygen");
                 
                     switch (lcd_key)               // depending on which button was pushed, we perform an action
                               {
                                 case btnUP:
                                   {
                                     do{
                                     lcd_key = read_LCD_buttons();  
                                     lcd.clear();
                                     lcd.print("Temperature : =");//Celsius
                                     delay(300);
                                     float temperature = getTemp(); 
                                     lcd.setCursor(0,1);        //ขึ้นlcdบรรทัด2
                                     lcd.print("   ");
                                     lcd.print(temperature,DEC); //โชว์ค่าอุณหภูมิน้ำ
                                     lcd.print(" Celsius");
                                     delay(500); //just here to slow down the output so it is easier to read
                                     
                                     
                                     lcd.clear(); //เคียร์หน้าจอ
                                     }while(lcd_key!=btnRIGHT);
                                     lcd.clear();
                                     lcd.print("     save    ");
                                        lcd.setCursor(0,1);
                                       lcd.print("  Temperature");
                                       delay(1000);
                                   break;
                                   }
                                   case btnDOWN:
                                   {
                                    do{
                                     lcd_key = read_LCD_buttons();  
                                     lcd.clear();
                                     lcd.print("Dissolved Oxygen");
                                     delay(300);
                                     }while(lcd_key!=btnRIGHT);
                                     lcd.clear();
                                       lcd.print("     save    ");
                                        lcd.setCursor(0,1);
                                       lcd.print("Dissolved Oxygen");
                                       delay(1000);
                                   break;
                                   }
                               }
                     
                     delay(300);
                 }while(lcd_key!=btnLEFT);
                 lcd.clear();
                 lcd.print("     Cancel     ");
                 delay(1000);
                 break;
    
               
     ///////////////////////////////////////////////////////     
     }
     case btnNONE:
     {
       time();
       break;
     }
 }
 
}
void time(void)
{
     lcd.clear(); //เคียร์หน้าจอ
  DateTime now = RTC.now();
  
    if (now.day() < 10) { Serial.print("0");
                           lcd.print(0);
                         }
    Serial.print(now.day(), DEC);
    Serial.print("/");

  lcd.print(now.day(), DEC);  //////////lcd day
  lcd.print("/");
  
  
    if (now.month() < 10) { Serial.print("0");
                            lcd.print(0); 
                          }
    Serial.print(now.month(), DEC);
    Serial.print("/");
      lcd.print(now.month(), DEC);  //////////lcd month
      lcd.print("/");
  
 
    Serial.println(now.year(), DEC);
          lcd.print(now.year(), DEC);  //////////lcd year

       lcd.setCursor(0,1);               ////set line2
    if (now.hour() < 10) { Serial.print("0"); 
                           lcd.print(0);
                         }
    Serial.print(now.hour(), DEC);  
    Serial.print(":");
    lcd.print(now.hour(), DEC);
    lcd.print(":");
 
    if (now.minute() < 10) { Serial.print("0"); 
                               lcd.print(0);
                            }
    Serial.print(now.minute(), DEC);  
    Serial.print(":");
      lcd.print(now.minute(), DEC);
    lcd.print(":");
 
 
    if (now.second() < 10) { Serial.print("0");
                             lcd.print(0);
                               }
    Serial.println(now.second(), DEC);
          lcd.print(now.second(), DEC);
    // pause
    delay(500);
}

//////////////////////////sensor///////////////////
//void temperature1(void) 
  


float getTemp(){
  //returns the temperature from one DS18S20 in DEG Celsius

  byte data[12];
  byte addr[8];

  if ( !ds.search(addr)) {
      //no more sensors on chain, reset search
      ds.reset_search();
      return -1000;
  }

  if ( OneWire::crc8( addr, 7) != addr[7]) {
      Serial.println("CRC is not valid!");
      return -1000;
  }

  if ( addr[0] != 0x10 && addr[0] != 0x28) {
      Serial.print("Device is not recognized");
      return -1000;
  }

  ds.reset();
  ds.select(addr);
  ds.write(0x44,1); // start conversion, with parasite power on at the end

  byte present = ds.reset();
  ds.select(addr);    
  ds.write(0xBE); // Read Scratchpad

  
  for (int i = 0; i < 9; i++) { // we need 9 bytes
    data[i] = ds.read();
  }
  
  ds.reset_search();
  
  byte MSB = data[1];
  byte LSB = data[0];

  float tempRead = ((MSB << 8) | LSB); //using two's compliment
  float TemperatureSum = tempRead / 16;
  
  return TemperatureSum;
  
}


ผมใช้arduinoเบิร์นโค๊ดนี้ลงไปแล้วมันได้ผมลัพธ์เป็นค่าอุณหภูมิที่มีจุดทศนิยมแต่มันมีจำนวนมากเกินไปผมจะให้มันเหลือสัก3ตำแหน่งอ้ะครับ ช่วยดูโค๊ดให้ผมด้วยนะครับรบกวนด้วยครับผม ขอบคุณมากครับ



Tag : C


Move To Hilight (Stock) 
Send To Friend.Bookmark.
Date : 2013-09-24 22:43:15 By : จักรกฤษณ์ View : 12575 Reply : 2
 

 

No. 1



โพสกระทู้ ( 74,058 )
บทความ ( 838 )

สมาชิกที่ใส่เสื้อไทยครีเอท

สถานะออฟไลน์
Twitter Facebook

ถ้าเป็น C ลองดู

printf("%.3f",int);

แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2013-09-25 06:33:17 By : mr.win
 


 

No. 2

Guest


ตอบความคิดเห็นที่ : 1 เขียนโดย : mr.win เมื่อวันที่ 2013-09-25 06:33:17
รายละเอียดของการตอบ ::
#include <LiquidCrystal.h> ///คำสั่ง #include แล้วตามด้วยไฟล์ที่อยู่ใน Folder ชื่อ Libraries 
#include <Wire.h>
#include <OneWire.h>
#include <RTClib.h> 


 
// select the pins used on the Temperature panel
int DS18S20_Pin = A7; //DS18S20 Signal pin on digital A7

//Temperature chip i/o
OneWire ds(DS18S20_Pin);  // on digital pin 2




LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
 RTC_DS1307 RTC;
// define some values used by the panel and buttons
int lcd_key     = 0;
int adc_key_in  = 0;
#define btnRIGHT  0
#define btnUP     1
#define btnDOWN   2
#define btnLEFT   3
#define btnSELECT 4
#define btnNONE   5
 
// read the buttons
int read_LCD_buttons()
{
 adc_key_in = analogRead(0);      // read the value from the sensor 
 // my buttons when read are centered at these valies: 0, 144, 329, 504, 741
 // we add approx 50 to those values and check to see if we are close
 if (adc_key_in > 1000) return btnNONE; // We make this the 1st option for speed reasons since it will be the most likely result
 // For V1.1 us this threshold
 if (adc_key_in < 50)   return btnRIGHT;  
 if (adc_key_in < 250)  return btnUP; 
 if (adc_key_in < 450)  return btnDOWN; 
 if (adc_key_in < 650)  return btnLEFT; 
 if (adc_key_in < 850)  return btnSELECT;  
 

 
 
 return btnNONE;  // when all others fail, return this...
}
 
void setup()
{
 lcd.begin(16, 2);              // start the library
 lcd.setCursor(0,0);
 Serial.begin(9600);
    Wire.begin();
    RTC.begin();
}
  
void loop()
{
 //lcd.setCursor(9,1);            // move cursor to second line "1" and 9 spaces over
 //lcd.print(millis()/1000);      // display seconds elapsed since power-up
 float temperature = getTemp();   
 int guess, buttonPressed;
 Serial.println(temperature);
 
 
 lcd.setCursor(0,1);            // move to the begining of the second line
 lcd_key = read_LCD_buttons();  // read the buttons
 
 switch (lcd_key)               // depending on which button was pushed, we perform an action
 {
   case btnRIGHT:
     {
       lcd.clear();
     lcd.print(" RIGHT ");
     delay(500);
     break;
     }
   case btnLEFT:
     {
       lcd.clear();
     lcd.print(" LEFT   ");
      delay(500);
     break;
     }
   case btnUP:
     {
       lcd.clear();
     lcd.print(" UP    ");
      delay(500);
     break;
     }
   case btnDOWN:
     {
       lcd.clear();
     lcd.print(" DOWN  ");
      delay(500);
     break;
     }
   case btnSELECT:
     {
            ///////////////////// select sensor /////////////////
                 do{
                 lcd_key = read_LCD_buttons();
                 lcd.clear();
                 lcd.print(" Temperature &");
                 lcd.setCursor(0,1);
                 lcd.print("Dissolved Oxygen");
                 
                     switch (lcd_key)               // depending on which button was pushed, we perform an action
                               {
                                 case btnUP:
                                   {
                                     do{
                                     lcd_key = read_LCD_buttons();  
                                     lcd.clear();
                                     lcd.print("Temperature : =");//Celsius
                                     delay(300);
                                     float temperature = getTemp(); 
                                     lcd.setCursor(0,1);        //ขึ้นlcdบรรทัด2
                                     lcd.print("   ");
                                     //lcd.print(temperature,DEC);
                                     float temp;
                                     temp=temperature;
                                     lcd.print(("%.2f",temp));       แก้ดูแล้วครับ ผลออกมาเป็นทศยม2ตำแหน่ง 

                                    
                                     lcd.print(" Celsius");
                                     delay(500); //just here to slow down the output so it is easier to read
                                     
                                     
                                     lcd.clear(); //เคียร์หน้าจอ
                                     }while(lcd_key!=btnRIGHT);
                                     lcd.clear();
                                     lcd.print("     save    ");
                                        lcd.setCursor(0,1);
                                       lcd.print("  Temperature");
                                       delay(1000);
                                   break;
                                   }
                                   case btnDOWN:
                                   {
                                    do{
                                     lcd_key = read_LCD_buttons();  
                                     lcd.clear();
                                     lcd.print("Dissolved Oxygen");
                                     delay(300);
                                     }while(lcd_key!=btnRIGHT);
                                     lcd.clear();
                                       lcd.print("     save    ");
                                        lcd.setCursor(0,1);
                                       lcd.print("Dissolved Oxygen");
                                       delay(1000);
                                   break;
                                   }
                               }
                     
                     delay(300);
                 }while(lcd_key!=btnLEFT);
                 lcd.clear();
                 lcd.print("     Cancel     ");
                 delay(1000);
                 break;
    
               
     ///////////////////////////////////////////////////////     
     }
     case btnNONE:
     {
       time();
       break;
     }
 }
 
}
void time(void)
{
     lcd.clear(); //เคียร์หน้าจอ
  DateTime now = RTC.now();
  
    if (now.day() < 10) { Serial.print("0");
                           lcd.print(0);
                         }
    Serial.print(now.day(), DEC);
    Serial.print("/");

  lcd.print(now.day(), DEC);  //////////lcd day
  lcd.print("/");
  
  
    if (now.month() < 10) { Serial.print("0");
                            lcd.print(0); 
                          }
    Serial.print(now.month(), DEC);
    Serial.print("/");
      lcd.print(now.month(), DEC);  //////////lcd month
      lcd.print("/");
  
 
    Serial.println(now.year(), DEC);
          lcd.print(now.year(), DEC);  //////////lcd year

       lcd.setCursor(0,1);               ////set line2
    if (now.hour() < 10) { Serial.print("0"); 
                           lcd.print(0);
                         }
    Serial.print(now.hour(), DEC);  
    Serial.print(":");
    lcd.print(now.hour(), DEC);
    lcd.print(":");
 
    if (now.minute() < 10) { Serial.print("0"); 
                               lcd.print(0);
                            }
    Serial.print(now.minute(), DEC);  
    Serial.print(":");
      lcd.print(now.minute(), DEC);
    lcd.print(":");
 
 
    if (now.second() < 10) { Serial.print("0");
                             lcd.print(0);
                               }
    Serial.println(now.second(), DEC);
          lcd.print(now.second(), DEC);
    // pause
    delay(500);
}

//////////////////////////sensor///////////////////
//void temperature1(void) 
  


float getTemp(){
  //returns the temperature from one DS18S20 in DEG Celsius

  byte data[12];
  byte addr[8];

  if ( !ds.search(addr)) {
      //no more sensors on chain, reset search
      ds.reset_search();
      return -1000;
  }

  if ( OneWire::crc8( addr, 7) != addr[7]) {
      Serial.println("CRC is not valid!");
      return -1000;
  }

  if ( addr[0] != 0x10 && addr[0] != 0x28) {
      Serial.print("Device is not recognized");
      return -1000;
  }

  ds.reset();
  ds.select(addr);
  ds.write(0x44,1); // start conversion, with parasite power on at the end

  byte present = ds.reset();
  ds.select(addr);    
  ds.write(0xBE); // Read Scratchpad

  
  for (int i = 0; i < 9; i++) { // we need 9 bytes
    data[i] = ds.read();
  }
  
  ds.reset_search();
  
  byte MSB = data[1];
  byte LSB = data[0];

  float tempRead = ((MSB << 8) | LSB); //using two's compliment
  float TemperatureSum = tempRead / 16;
  
  return TemperatureSum;
  
}

ลองแก้ดูแล้วครับ ตอนแรกผมไม่รู้จะเอาไปหย่อนลงที่ไหน ผลออกมาได้ทศนิยม2ตำแหน่งแล้วครับ ขอบคุณมากครับ


แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2013-09-25 11:50:02 By : จักรกฤษณ์
 


   

ค้นหาข้อมูล


   
 

แสดงความคิดเห็น
Re : ลดจุดทศนิยมเหลือ 3ตำแหน่ง เพราะโค๊ดตอนนี้มันมีผลลัพธ์ออกมาแล้วมี จุดทศนิยมมากจนเกินความต้องการครับผม
 
 
รายละเอียด
 
ตัวหนา ตัวเอียง ตัวขีดเส้นใต้ ตัวมีขีดกลาง| ตัวเรืองแสง ตัวมีเงา ตัวอักษรวิ่ง| จัดย่อหน้าอิสระ จัดย่อหน้าชิดซ้าย จัดย่อหน้ากึ่งกลาง จัดย่อหน้าชิดขวา| เส้นขวาง| ขนาดตัวอักษร แบบตัวอักษร
ใส่แฟลช ใส่รูป ใส่ไฮเปอร์ลิ้งค์ ใส่อีเมล์ ใส่ลิ้งค์ FTP| ใส่แถวของตาราง ใส่คอลัมน์ตาราง| ตัวยก ตัวห้อย ตัวพิมพ์ดีด| ใส่โค้ด ใส่การอ้างถึงคำพูด| ใส่ลีสต์
smiley for :lol: smiley for :ken: smiley for :D smiley for :) smiley for ;) smiley for :eek: smiley for :geek: smiley for :roll: smiley for :erm: smiley for :cool: smiley for :blank: smiley for :idea: smiley for :ehh: smiley for :aargh: smiley for :evil:
Insert PHP Code
Insert ASP Code
Insert VB.NET Code Insert C#.NET Code Insert JavaScript Code Insert C#.NET Code
Insert Java Code
Insert Android Code
Insert Objective-C Code
Insert XML Code
Insert SQL Code
Insert Code
เพื่อความเรียบร้อยของข้อความ ควรจัดรูปแบบให้พอดีกับขนาดของหน้าจอ เพื่อง่ายต่อการอ่านและสบายตา และตรวจสอบภาษาไทยให้ถูกต้อง

อัพโหลดแทรกรูปภาพ

Notice

เพื่อความปลอดภัยของเว็บบอร์ด ไม่อนุญาติให้แทรก แท็ก [img]....[/img] โดยการอัพโหลดไฟล์รูปจากที่อื่น เช่นเว็บไซต์ ฟรีอัพโหลดต่าง ๆ
อัพโหลดแทรกรูปภาพ ให้ใช้บริการอัพโหลดไฟล์ของไทยครีเอท และตัดรูปภาพให้พอดีกับสกรีน เพื่อความโหลดเร็วและไฟล์ไม่ถูกลบทิ้ง

   
  เพื่อความปลอดภัยและการตรวจสอบ กระทู้ที่แทรกไฟล์อัพโหลดไฟล์จากที่อื่น อาจจะถูกลบทิ้ง
 
โดย
อีเมล์
บวกค่าให้ถูก
<= ตัวเลขฮินดูอารบิก เช่น 123 (หรือล็อกอินเข้าระบบสมาชิกเพื่อไม่ต้องกรอก)







Exchange: นำเข้าสินค้าจากจีน, Taobao, เฟอร์นิเจอร์, ของพรีเมี่ยม, ร่ม, ปากกา, power bank, แฟลชไดร์ฟ, กระบอกน้ำ

Load balance : Server 03
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่