Robofun 機器人論壇

 找回密碼
 申請會員
搜索
熱搜: 活動 交友 discuz
查看: 3144|回復: 2

新手詢問兩個變數皆成立才會啟動某設備的程式碼撰寫

[複製鏈接]
發表於 2016-3-31 22:26:48 | 顯示全部樓層 |閱讀模式
大家好,我是超菜的ARDUINO新手,我想請問一下如何將兩個變數都成立才啟動某設備程式碼該如何撰寫?
例如,我希望溫度大於28度並且濕度高於70度才啟動RELAY,要怎麼做呢?

我之前只會寫單一變數
int templabel;
templabel = analogRead(0);
if (templabel > 28)
{
digitalWrite(2, HIGH);
}

例如是這樣,但是如果要把濕度變數要加入,該怎麼寫?可以請大家幫忙一下嗎?
發表於 2016-4-1 06:34:21 | 顯示全部樓層
本帖最後由 超新手 於 2016-4-1 08:24 編輯

if((a >28)&&(b>70)){



}
一般c語言的書都有介紹
且就用 && 或就用 ||
 樓主| 發表於 2016-4-1 17:01:03 | 顯示全部樓層
感謝您~已經解決了!
我大致的寫法改為

  int moistureState;
  moistureState = analogRead(0);
  int WatersensorState;
  WatersensorState = analogRead(2);
  if ((moistureState < 290)&&(WatersensorState > 2))
  {      
     digitalWrite(2, HIGH);                      // 切換繼電器開關
     digitalWrite(11, HIGH);
  }
  else
  {      
     digitalWrite(2, LOW);                      // 切換繼電器開關
     digitalWrite(11, LOW);
  }
您需要登錄後才可以回帖 登錄 | 申請會員

本版積分規則

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

GMT+8, 2024-3-29 06:25 , Processed in 0.187618 second(s), 7 queries , Apc On.

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

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