ComputerCraft Forums

ComputerCraft => Ask a Pro => Topic started by: moneybags on Jan 05, 2023, 04:47 AM

Title: Need help regarding rednet
Post by: moneybags on Jan 05, 2023, 04:47 AM
Hi All,
I'm currently running the 1.12.2 version of the mod.

Purpose
This program displays content depending on the rednet message it receives. When it initially receives the correct message, it will display text to the monitors that are attached it to. Until either a abort message or the correct message that matches the condition is received I have it stuck in a repeat loop. When a final message is received, it triggers a timer function that I've created.

Issue
The issue that I'm is that, when I send a initial message via rednet it does not proceed to my next loop, I'm not sure if using repeat is the correct way to accomplish this as I've also tried using while true do, but get the same issue. Any help or advice is much appreciated.

Code
https://pastebin.com/qTAUuknw

-- Launch monitor prod

-- function timer
function timerlaunch()
  local mon = peripheral.find("monitor")
  local nHours = tonumber(0)
  nHours = nHours or 0
  local nMinutes = tonumber(10)
  nMinutes = nMinutes or 0
  local nSeconds = tonumber(0)
  nSeconds = nSeconds or 0
  local totalTime = (nHours * 36000) + (nMinutes * 600) + (nSeconds * 10)
  local hours, minutes, seconds, sHours, sMinutes, sSeconds, sTenths, countdownTimer, timer, _
  if mon then
    local x
    for i = 1, #mon do
      mon[i].setTextColor(colors.white)
      mon[i].setBackgroundColor(colors.black)
      mon[i].clear()
      mon[i].setTextScale(1)
      x = mon[i].getSize()
      if x == 7 then      --# width = 1
        mon[i].setTextScale(0.5)
      elseif x == 18 then --# width = 2
        mon[i].setTextScale(nHours < 10 and 2 or 1)
      elseif x == 29 then --# width = 3
        mon[i].setTextScale(3)
      elseif x == 39 then --# width = 4
        mon[i].setTextScale(4)
      elseif x >= 50 then --# width = 5+
        mon[i].setTextScale(5)
      end
    end
  end
  for i = totalTime, 0, -1 do
    hours = totalTime > 35999 and math.floor(totalTime / 36000) or 0
    sHours = tostring(hours)
    minutes = totalTime > 599 and math.floor((totalTime - (hours * 36000)) / 600) or 0
    sMinutes = minutes > 9 and tostring(minutes) or "0" .. tostring(minutes)
    seconds = totalTime > 9 and math.floor((totalTime - ((hours * 36000) + (minutes * 600))) / 10) or 0
    sSeconds = seconds > 9 and tostring(seconds) or "0" .. tostring(seconds)
    sTenths = tostring(math.floor(totalTime - ((hours * 36000) + (minutes * 600) + (seconds * 10))))
    term.setCursorPos(1, 1)
    if totalTime == 0 and term.isColor() then term.setTextColor(colors.red) end
    term.write(sHours .. ":" .. sMinutes .. ":" .. sSeconds .. ":" .. sTenths .. " ")
    if mon then
      for i = 1, #mon do
        mon[i].setCursorPos(1, 2)
        if totalTime == 0 and mon[i].isColor() then mon[i].setTextColor(colors.red) end
        mon[i].write(sHours .. ":" .. sMinutes .. ":" .. sSeconds .. ":" .. sTenths .. " ")
      end
    end
    if totalTime > 0 then
      countdownTimer = os.startTimer(0.1)
      repeat
        _, timer = os.pullEvent("timer")
        if timer == countdownTimer then
          totalTime = totalTime - 1
        end
      until timer == countdownTimer
    end
  end
end
-- function end
local mon = peripheral.find("monitor")
mon.setTextScale(4)
mon.clear()
rednet.open("top")
mon.setCursorPos(1,1)
mon.write("Listening")
mon.setCursorPos(1,2)
mon.write("for signal")
local id_1,int_check = rednet.receive()
local id_2,handshake = rednet.receive()
local id_3,startcount = rednet.receive()
local phase2 = false
repeat
if int_check == "ping" then
-- rednet.send(senderId, "pong")
    phase2 = true
else
    mon.clear()
    mon.setCursorPos(1,1)
    mon.write("Countdown Timer")
    mon.setTextColour(colours.red)
    mon.setCursorPos(1,2)
    mon.write("Aborted due to technical issues")
    sleep(20)
    os.reboot()
  end
until phase2 == true
local phase3 = false
repeat
if handshake == "1" then
mon.setCursorPos(1,1)
mon.write("Countdown Timer")
mon.setCursorPos(1,2)
mon.setTextColour(colours.green)
mon.write("Standby for launch")
    phase3 = true
else
    mon.clear()
    mon.setCursorPos(1,1)
    mon.write("Countdown Timer")
    mon.setTextColour(colours.red)
    mon.setCursorPos(1,2)
    mon.write("Aborted due to technical issues")
    sleep(20)
    os.reboot()
  end
until phase3 == true
local phase4 = false
repeat
if startcount == "2" then
mon.clear()
timerlaunch()
    phase4 = true
os.reboot()
  else
    mon.clear()
    mon.setCursorPos(1,1)
    mon.write("Countdown Timer")
    mon.setTextColour(colours.red)
    mon.setCursorPos(1,2)
    mon.write("Aborted due to technical issues")
    sleep(20)
    os.reboot()
  end
until phase4 == true
mon.clear()
mon.setCursorPos(1,1)
mon.write("Countdown Timer")
mon.setTextColour(colours.green)
mon.setCursorPos(1,2)
mon.write("Completed")
sleep(10)
os.reboot()
Title: Need help regarding rednet
Post by: SquidDev on Jan 05, 2023, 10:47 AM
I think there's a couple of issues here:

With your first loop:

local id_1,int_check = rednet.receive()
repeat
    if int_check == "ping" then
--      rednet.send(senderId, "pong")
    phase2 = true
    else
    mon.clear()
    mon.setCursorPos(1,1)
    mon.write("Countdown Timer")
    mon.setTextColour(colours.red)
    mon.setCursorPos(1,2)
    mon.write("Aborted due to technical issues")
    sleep(20)
    os.reboot()
  end
until phase2 == true

I'd probably write it something like:

while true do
  local id_1, int_check = rednet.receive()
  if int_check == "ping" then
    break -- exit the loop: easier than setting phase2!
  else
    print("Got unknown message:", int_check)
  end
end