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

Inverted Redstone signal

Started by Bloodhawk1990, 07 April 2013 - 10:11 AM
Bloodhawk1990 #1
Posted 07 April 2013 - 12:11 PM
Hi i want to make a mob farm with the soul shards mod, in the ultimate FTB pack, what these can do is spawn mobs at any light level, but when a redstone signal is applied to them they will stop spawning. Anyways i want to make a code for a computer so that it will redstone pulse to the right of the computer every two seconds when there is no signal, but turn off when a redstone current is applied to the left. If anybody could help me with some coding i would REALLY appreciate it! thanks to anybody who can offer some coding advice.
OmegaVest #2
Posted 07 April 2013 - 12:26 PM

local on = false  -- So we'll know whether to fire the pulse or not.'

local function doTime  -- The pulse
  rs.setOutput("right", true)
  sleep(0.2)
  rs.setOutput("right", false)
end

while true do
  local tmr = os.startTimer(2.0)  -- Make a timer for 2 seconds

  local evt, arg1, arg2 = os.pullEvent()  -- Catch-all for our functions
  if evt == "redstone" then
	if rs.getInput("left") then  -- If left is on
	  on = true
	else  -- Or not.
	  on = false
	end
  elseif evt == "timer" and arg1 == tmr and on then
	doTime()
  end
end


And I did that in about a minute, give or take typing speed.
theoriginalbit #3
Posted 07 April 2013 - 08:03 PM
For OmegaVest, you have a few bugs, and it didn't suit OP requirements, see spoiler for more.
Spoiler
Spoiler

local on = false  -- So we'll know whether to fire the pulse or not.'

local function doTime  -- The pulse
  rs.setOutput("right", true)
  sleep(0.2)
  rs.setOutput("right", false)
end

while true do
  local tmr = os.startTimer(2.0)  -- Make a timer for 2 seconds

  local evt, arg1, arg2 = os.pullEvent()  -- Catch-all for our functions
  if evt == "redstone" then
	if rs.getInput("left") then  -- If left is on
	  on = true
	else  -- Or not.
	  on = false
	end
  elseif evt == "timer" and arg1 == tmr and on then
	doTime()
  end
end
And I did that in about a minute, give or take typing speed.

Bugs, improvements and not meeting requirements (for a learning experience) :
  • BUG: you forgot the () on the function declaration
  • IMPROVEMENT: the lines
  • 
    if rs.getInput('left') then
      on = true
    else
      on = false
    end
    
    that function returns true or false, why put it in an if statement to then just assign true/false to the variable, just assign the function call to the variable, even of you want to invert it you can do this by using 'not'
  • BUG: I could have this never pulse by spamming other events (like key press) and your timer is always being overridden, so when it comes time to see if the timer has finished it will never validate because there is a new timer id in the variable… to fix this you should always start a timer before a loop, and then restart the timer once it has fired and you detect it.
  • NOT MEETING REQUIREMENTS: the if statement to check to pulse the redstone says; if the event was a timer finishing, and the first argument was our timer, and the redstone signal on the left is ON, then pulse, OP clearly states they want it to pulse when there is no signal, so it needed to be 'not on'
  • BUG: The on variable at the start should have been assigned based on the redstone signal on the left, just incase it was on when the program started up

For Bloodhawk1990, here is the fixed code.
Spoiler

local outputSide = 'right'
local inputSide = 'left'
local disable = rs.getInput(inputSide) -- 'so we know whether to fire the pulse or not.'

local function pulseSpawner()
  rs.setOutput(outputSide, true)
  sleep(0.2)
  rs.setOutput(outputSide, false)
end

local timeout = os.startTimer(2) -- 'make a timer for 2 seconds'

while true do
  local event, timer = os.pullEventRaw()

  if event == 'redstone' then
	disable = rs.getInput(inputSide)
	if not disable then
	  timeout = os.startTimer(2) -- 'restart our timer, we dont know how long it has been disabled for'
	end
  elseif event == 'timer' and timer == timeout and not disabled then
	pulseSpawner()
	timeout = os.startTimer(2) -- 'restart our timer'
  end
end

Edited on 08 April 2013 - 05:34 AM
Bloodhawk1990 #4
Posted 08 April 2013 - 07:25 AM
When i do your code it says startup:1: Expected string, did i type something wrong into the first line? sorry for being so helpless
theoriginalbit #5
Posted 08 April 2013 - 07:34 AM
Nope that was my fault, sorry, go again.
Bloodhawk1990 #6
Posted 08 April 2013 - 07:42 AM
That worked, thanks for all the help :D/>