Advertisement
hevohevo

CC: exercise10_2

May 21st, 2016
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.14 KB | None | 0 0
  1. -- exercise10_2
  2. -- 特定のコンピュータによる命令によって扉を開けるプログラム(メッセージ受信側)
  3.  
  4. -- ########### Config ###########
  5. local open_msg = "Open" -- 開くメッセージ
  6. local close_msg = "Close" -- 閉めるメッセージ
  7. local sender_id = 10  -- 命令を送信する側のコンピュータのIDを調べて記述
  8.  
  9. rednet.open("right") -- 右側に設置したモデムを使うという宣言
  10.  
  11. -- ########### Functions ###########
  12. function judgeMessage(msg)
  13.   if msg == open_msg then -- もし受信メッセージが"Open"ならば、
  14.     print("Opened the door")
  15.     redstone.setOutput("top", true)
  16.   elseif msg == close_msg then -- もし受信メッセージが"Close"ならば、
  17.     print("Closed the door")
  18.     redstone.setOutput("top", false)
  19.   else -- どれでもなければ、
  20.     print("wrong")
  21.   end
  22. end
  23.  
  24. -- ########### Main ###########
  25. while true do
  26.   local id, msg = rednet.receive() -- メッセージを待ち受ける
  27.   print(id, msg)
  28.   if id == sender_id then
  29.     print("correct sender!")
  30.     judgeMessage(msg)
  31.   else
  32.     print("incorrect sender!")
  33.   end
  34. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement