| 
 | 
 
大家好,小弟最近遇到兩個問題始終無法解決,懇請各位不吝給予指教 先謝謝各位了 1.當溫度感測器 量到溫度時 鍵盤按下*字鍵 無法清空螢幕 所以 也無法修改功率(p1 p2 為功率值?/255)  
推測為程式卡在溫度感測迴圈內 造成不斷更新溫度 而無法處理其他鍵盤之輸入 
2.目前lcd螢幕可顯示一個溫度感測器所測量的溫度,小弟想多加裝一個溫度感測器,使其同時量測兩處之溫度,並同時顯示於lcd螢幕上 
 
還望大家能抽空幫忙 感激不盡  
附上email avril90@kimo.com 再次謝謝大家 
 
 
以下是小弟的程式碼 
#include <OneWire.h> 
#include <Wire.h> 
#include <LiquidCrystal.h> 
#include <Keypad.h> 
int heaterPin_1 = 8;  
int heaterPin_2 = 9; 
int h1 = 0;   
int h2 = 0; 
char text1 [] = "P1: "; 
char text2 [] = "P2: "; 
// initialize the library with the numbers of the interface pins 
LiquidCrystal lcd(12, 11, 5, 4, 3, 2); 
// 3x4 Keypad 
const byte ROWS = 4; // 4 Rows 
const byte COLS = 3; // 3 Columns 
 
// 定義 Keypad 的按鍵 
char keys[ROWS][COLS] = { 
  {'1','2','3'}, 
  {'4','5','6'}, 
  {'7','8','9'}, 
  {'*','0','#'} 
}; 
 
// 定義 Keypad 連到 Arduino 的接腳 
byte rowPins[ROWS] = {32, 22, 24, 28}; // 連到 Keypad 的 4 個 Rows: Row0, Row1, Row2, Row3  
byte colPins[COLS] = {30, 34, 26};   // 連到 Keypad 的 3 個 Columns: Column0, Column1, Column2 
 
// 建立 Keypad 物件 
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS ); 
 
OneWire  ds(10);  // on pin 10 
 
 
 
 
void setup() { 
  pinMode(heaterPin_1, OUTPUT); // 設定 pin 8 為輸出 
  Serial.begin(9600);  
  // set up the LCD's number of columns and rows:  
  lcd.begin(16, 2); 
  const int keyin[4]= {} ; 
    ds.reset(); 
} 
 
 
unsigned int readBytes(int count) 
{ 
  unsigned int val = 0; 
  for (int i = 0; i < count; i++) 
  { 
    val |= (unsigned int)(ds.read() << i * 8); 
  } 
  return val; 
} 
int a = 0; 
void loop() { 
   // 讀取 Keypad 的輸入 
  char key = keypad.getKey(); 
  // NO_KEY 代表沒有按鍵被按下 
 
 
 
if (h1 != 0){ 
 
temp();} 
 
 
if (key != NO_KEY){ 
 
int b = key - 48;//因為是ASCII數值,所以才要-48 
 
lcd.print(b); 
 
 
if(key == '#'){ 
 
 
 
lcd.clear(); 
 
if(a % 2 == 0){ // 代表偶數,代表加熱棒2 
 
a /= 10; 
 
h2 = a; 
 
analogWrite(heaterPin_2, a); // heater輸出功率 
 
} 
 
else{ // 代表奇數,代表加熱棒1 
 
a /= 10; 
 
h1 = a; 
 
analogWrite(heaterPin_1, a); // heater輸出功率 
} 
 
lcd.clear(); 
 
lcd.print(text1 ); 
 
lcd.setCursor(3, 0); 
 
lcd.print(h1); 
 
lcd.setCursor(0, 1); 
 
lcd.print("P2:"); 
 
lcd.setCursor(3, 1); 
 
lcd.print(h2); 
} 
 
else if(key == '*'){ 
 
lcd.clear(); 
 
a = 0;//輸入錯誤從新開始打一次 
 
} 
 
else{ 
 
a = a*10+b; 
 
} 
 
} 
} 
 
void temp() { 
  byte temp_read = 0; 
  unsigned int count_remain = 0; 
  unsigned int count_per_c = 0; 
  byte configuration_register = 0; 
 
  ds.reset(); 
  ds.write(0xEE); //Start Converting the temperatures 
 
  do { 
    delay(1); 
    configuration_register = 0; 
    ds.reset(); 
    ds.write(0xAC); 
 
    // Read the configuration Register from the DS1821 
    configuration_register = readBytes(1); 
  } while ((configuration_register & (1 << 7)) == 0); // If Bit #8 is 1 then we are finished converting the temp 
 
 
  // Get Temp 
  ds.reset(); 
  ds.write(0xAA); 
  temp_read = readBytes(1); ; 
 
  // Get Count Remaining 
  ds.reset(); 
  ds.write(0xA0); 
  count_remain = readBytes(2); 
 
  // Load The Counter to populate the slope accumulator 
  ds.reset(); 
  ds.write(0x41); 
 
  // Read Count Per Deg 
  ds.reset(); 
  ds.write(0xA0); 
  count_per_c = readBytes(2); 
 
  // If we are reading above the 200 mark then we are below 0 and need to compensate the calculation 
  if (temp_read >= 200) temp_read -= 256; 
 
  float highResTemp = (float)temp_read - .5 + (((float)count_per_c - (float)count_remain) / (float)count_per_c); 
 
  highResTemp = ( highResTemp); 
  lcd.setCursor(7, 0); 
  lcd.print("t1:"); 
  lcd.print(highResTemp); 
  delay(1000); 
 
} |   
 
 
 
 |