Robofun 機器人論壇

 找回密碼
 申請會員
搜索
熱搜: 活動 交友 discuz
查看: 2747|回復: 4
打印 上一主題 下一主題

如何修改程式 來跳出迴圈呢?

[複製鏈接]
跳轉到指定樓層
1#
發表於 2015-12-14 17:04:29 | 只看該作者 |只看大圖 回帖獎勵 |倒序瀏覽 |閱讀模式
大家好,小弟最近遇到兩個問題始終無法解決,懇請各位不吝給予指教 先謝謝各位了 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);

}
2#
發表於 2015-12-14 19:58:05 | 只看該作者
如果按住*按鍵超過一秒以上,
螢幕也不會清空嗎?
3#
 樓主| 發表於 2015-12-14 20:17:39 | 只看該作者
回復 2# 超新手


  剛又再試了一下 , 長按* 超過5秒 也沒有任何反應   還是很謝謝大大的幫忙 3q
4#
發表於 2015-12-14 20:36:29 | 只看該作者
請問你的測試步驟是...?
按下1, 8, # 後
螢幕顯示一次溫度, 然後再按 *
卻沒清空
是這樣嗎?
5#
 樓主| 發表於 2015-12-15 00:41:05 | 只看該作者
回復 4# 超新手


   您好,順序是先按下 8 , 1(此為第一加熱棒 及顯示在p1 若要顯示在p2 則在功率後在多打上2)    最後按下 # 此時 p1 處 顯示功率為8 , 又因此時p1 !=0 lcd 才會顯示溫度,在溫度顯示後 每秒會更新一次量測到的溫度。
   若一開始輸入 8 , 2 # 則會顯示 p2: 8 此時 p1為0 溫度不會顯示 。
您需要登錄後才可以回帖 登錄 | 申請會員

本版積分規則

小黑屋|手機版|Archiver|機器人論壇 from 2005.07

GMT+8, 2024-4-25 17:07 , Processed in 0.233825 second(s), 11 queries , Apc On.

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回復 返回頂部 返回列表