This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
TheWoWClown's profile picture

Redstone Detect and Situational toggle

Started by TheWoWClown, 27 June 2012 - 04:42 PM
TheWoWClown #1
Posted 27 June 2012 - 06:42 PM
So I was working on this all day today, and could only get the status display to work. What this does is detects all outputs and then charts which ones are on and working. What I want to also happen is, if power is off, I want it to turn on, if the power is on I want it to turn off. I am new to this and just need some help accomplishing it.

print ("Redstone output status list.")
print("Left=", rs.getOutput("left"))
rs.getOutput("left")
if false then
rs.setOutput("left", true)
else
rs.setOutput("left", false)
end
print ("Right=", rs.getOutput("right"))
rs.getOutput("right")
if false then
rs.setOutput("right", true)
else
rs.setOutput("right", false)
end
print ("Bottom=", rs.getOutput("bottom"))
rs.getOutput("bottom")
if false then
rs.setOutput("bottom", true)
else
rs.setOutput("bottom", false)
end
print ("Top=", rs.getOutput("top"))
rs.getOutput("top")
if false then
rs.setOutput("top", true)
else
rs.setOutput("top", false)
end
print ("Back=", rs.getOutput("back"))
rs.getOutput("back")
if false then
rs.setOutput("back", true)
else
rs.setOutput("back", false)
end

Thanks!
Dirkus7 #2
Posted 27 June 2012 - 07:14 PM
You are comparing 'false' with 'true', so it will never work. I changed the code and clarified what i did:
Spoiler

print ("Redstone output status list.")
print("Left=", rs.getOutput("left"))
left = rs.getOutput("left")
if left == false then -- this will work, comparing left with false
rs.setOutput("left", true)
else
rs.setOutput("left", false)
end
print ("Right=", rs.getOutput("right"))
right = rs.getOutput("right")
if not right then -- this will also work, comparing not right with true
rs.setOutput("right", true)
else
rs.setOutput("right", false)
end
print ("Bottom=", rs.getOutput("bottom"))
bottom = rs.getOutput("bottom")
if not bottom == true then -- same as above
rs.setOutput("bottom", true)
else
rs.setOutput("bottom", false)
end
print ("Top=", rs.getOutput("top"))
top = rs.getOutput("top")
if top == true then -- you can also compare it with true, but don't forget to change the things below:
rs.setOutput("top", false) -- will give the same result, don't worry
else
rs.setOutput("top", true)
end
print ("Back=", rs.getOutput("back"))
back = rs.getOutput("back") -- or do this, don't check it, just change it
rs.setOutput("back", not back)
MysticT #3
Posted 27 June 2012 - 07:29 PM
A shorter version:

print("Redstone output status list.")
for _,side in ipairs(rs.getSides()) do -- loop through all sides
  print(side, " = ", rs.getOutput(side)) -- print the side and it's state
  rs.setOutput(side, not rs.getOutput(side)) -- switch the output
end
TheWoWClown #4
Posted 27 June 2012 - 07:31 PM
Thank you for the help. This will really help in the build I am planning.
TheWoWClown #5
Posted 27 June 2012 - 08:14 PM
Okay I am sorry to waste your time again, but I split it into two programs so that the status check would actually be useful. Now, I have s monitor above my computer, and was wondering how I would be able to make the status updates project right onto the monitor so that I can see it without going into the console. The new code for the status update is

print("Redstone output status list.")
for _,side in ipairs(rs.getSides()) do
  print(side, " = ", rs.getOutput(side))
end
I want it to project onto a screen, and will probably install it on it's own computer so that I can use my main computer for other things. If you could please help me again it would be amazing. And I am sorry for wasting your time at first Dirkus.
MysticT #6
Posted 27 June 2012 - 08:30 PM
Here you go:

local side = "top" -- the side the monitor is on

if peripheral.isPresent(side) and peripheral.getType(side) == "monitor" then
  term.redirect(peripheral.wrap(side))
else
  print("Monitor not found")
  return
end

print("Redstone output status list.")
for _,side in ipairs(rs.getSides()) do
  print(side, " = ", (rs.getOutput(side) and "On") or "Off") -- print "On" and "Off" instead of true/false
end

term.restore()
Not tested, but it should work.
TheWoWClown #7
Posted 27 June 2012 - 08:40 PM
Thank you, so much. This worked flawlessly. And it leaves console completely open which helps a lot. Thanks for the help Mystic!
TheWoWClown #8
Posted 27 June 2012 - 08:44 PM
I'm really sorry I have one more question, is there a way to make it update every few seconds or so, so I can keep a real time monitor?


local side = "top" -- the side the monitor is on
if peripheral.isPresent(side) and peripheral.getType(side) == "monitor" then
  term.redirect(peripheral.wrap(side))
else
  print("Monitor not found")
  return
end
print("Redstone output status list.")
for _,side in ipairs(rs.getSides()) do
  print(side, " = ", (rs.getOutput(side) and "On") or "Off") -- print "On" and "Off" instead of true/false
end
term.restore()

Thanks so much, I know this is really annoying, but I am really new and am trying to learn as much as I can.
MysticT #9
Posted 27 June 2012 - 09:00 PM
Adding a simple while loop and using sleep should be enough:

local side = "top" -- the side the monitor is on
local nTime = 3 -- time between updates, in seconds

if peripheral.isPresent(side) and peripheral.getType(side) == "monitor" then
  term.redirect(peripheral.wrap(side))
else
  print("Monitor not found")
  return
end

local function clear()
  term.clear()
  term.setCursorPos(1, 1)
end

while true do
  clear()
  print("Redstone output status list.")
  for _,side in ipairs(rs.getSides()) do
	print(side, " = ", (rs.getOutput(side) and "On") or "Off") -- print "On" and "Off" instead of true/false
  end
  sleep(nTime)
end
TheWoWClown #10
Posted 27 June 2012 - 09:10 PM
The code freezes up my computer and does not let me use it. Though, the updater works. I tested this by changing the code to input, and it updates when it has a new input. I think I might be able to us this if I branch redstone off of all sides and string them to the corresponding sides of a machine meant for recording inputs. If you can't fix the code I can just do this.
MysticT #11
Posted 27 June 2012 - 09:16 PM
Well, you would need parallel if you want to use the computer while running the program.
I think this should work:

local side = "top" -- the side the monitor is on
local nTime = 3 -- time between updates, in seconds

local mon
if peripheral.isPresent(side) and peripheral.getType(side) == "monitor" then
  mon = peripheral.wrap(side)
else
  print("Monitor not found")
  return
end

local function clear()
  term.clear()
  term.setCursorPos(1, 1)
end

local function run()
  while true do
    term.redirect(mon)
    clear()
    print("Redstone output status list.")
    for _,side in ipairs(rs.getSides()) do
      print(side, " = ", (rs.getOutput(side) and "On") or "Off") -- print "On" and "Off" instead of true/false
    end
    term.restore()
    sleep(nTime)
  end
end

parallel.waitForAny(run, function() shell.run("shell") end)

And if you want it for monitoring intput, use this one:

local side = "top" -- the side the monitor is on

local mon
if peripheral.isPresent(side) and peripheral.getType(side) == "monitor" then
  mon = peripheral.wrap(side)
else
  print("Monitor not found")
  return
end

local function clear()
  term.clear()
  term.setCursorPos(1, 1)
end

local function run()
  while true do
    term.redirect(mon)
    clear()
    print("Redstone input status list.")
    for _,side in ipairs(rs.getSides()) do
      print(side, " = ", (rs.getInput(side) and "On") or "Off") -- print "On" and "Off" instead of true/false
    end
    term.restore()
    os.pullEvent("restone")
  end
end

parallel.waitForAny(run, function() shell.run("shell") end)
It updates every time there's a change in redstone input.
Dirkus7 #12
Posted 27 June 2012 - 10:01 PM
Okay I am sorry to waste your time again, but I split it into two programs so that the status check would actually be useful. Now, I have s monitor above my computer, and was wondering how I would be able to make the status updates project right onto the monitor so that I can see it without going into the console. The new code for the status update is

print("Redstone output status list.")
for _,side in ipairs(rs.getSides()) do
  print(side, " = ", rs.getOutput(side))
end
I want it to project onto a screen, and will probably install it on it's own computer so that I can use my main computer for other things. If you could please help me again it would be amazing. And I am sorry for wasting your time at first Dirkus.
No problem dude, i hope you understand that you can't compare false with true :P/>/> That was just my point
TheWoWClown #13
Posted 27 June 2012 - 11:44 PM
Thank you everyone! The new script works perfectly with what I want it to do.
The help was wonderful.