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

CC Elevator | Mod Version 1.33 | Need help diagnosing

Started by GamersUnited556, 04 August 2018 - 03:51 PM
GamersUnited556 #1
Posted 04 August 2018 - 05:51 PM
I have started a code to make an elevator, It uses two separate timer loops to determine to go up or down based on elevator position and selected floors.
But the math functions in the code are messing up with negative values.

--[[ Local variables ]]

local termWidth, termHeight = term.getSize()
local selectedItem = 1
local a = 0 -- Current floor in block height
local b = 0 -- Selected floor in block height
local c = 0 -- Floor block height difference
local d = 0 -- Absolute value of c for timer loop
local running = true
--[[ Menu Methods ]]
function e() -- Math / Timer Function
  if rs.getBundledOutput("back",colors.white)==true then
    a = 0
  elseif rs.getBundledOutput("back",colors.black)== true then
a = -5
  elseif rs.getBundledOutput("back",colors.red)== true then
a = -10
  elseif rs.getBundledOutput("back",colors.green)== true then
a = -15
  end -- Value of a is negative to
 
  c = a + b
  d = math.abs(c)
 
  if c > 0 then -- Timer loop to go down
    for x=1, d do
   rs.setBundledOutput("back",colors.combine(rs.getBundledOutput("back"),colors.blue))
	  sleep(.2)
   rs.setBundledOutput("back",colors.subtract(rs.getBundledOutput("back"),colors.blue))
   sleep(.2)
end
  elseif c < 0 then -- Timer loop to go up
    for x=1, d do
   rs.setBundledOutput("back",colors.combine(rs.getBundledOutput("back"),colors.gray))
	  sleep(.2)
   rs.setBundledOutput("back",colors.subtract(rs.getBundledOutput("back"),colors.gray))
   sleep(.2)
end
  elseif c == 0 then
    return
  end
end
function C1() -- Floor selection to give wanted height variable
  if rs.getBundledOutput("back",colors.white)==true then
    return
  else
b = 0
e(B)/>
  end
end
function C2()
  if rs.getBundledOutput("back",colors.black)==true then
    return
  else
b = 5
e(B)/>
  end
end
function C3()
  if rs.getBundledOutput("back",colors.red)==true then
    return
  else
b = 10
e(B)/>
  end
end
function C4()
  if rs.getBundledOutput("back",colors.green)==true then
    return
  else
b = 15
e(B)/>
  end
end

--[[ Menu Definitions ]]

mainMenu = {
  [1] = { text = "1st Floor", handler = C1},
  [2] = { text = "2nd Floor", handler = C2},
  [3] = { text = "3rd Floor", handler = C3},
  [4] = { text = "4th Floor", handler = C4}
}

--[[ Printing Methods ]]

function printMenu( menu )
  term.clear()
  term.setCursorPos(1,1)
  print("-=Please Select a Floor=-")
  term.setCursorPos(1,2)
  for i=1,#menu do
    if i == selectedItem then
	  print(">> "..menu[i].text)
    else
	  print("   "..menu[i].text)
    end
  end
end

--[[ Handler method ]]

function onKeyPressed(key, menu)
  if key == 28 then
    onItemSelected(menu)
  elseif key == 200 then
    if selectedItem > 1 then
	  selectedItem = selectedItem - 1
    end
  elseif key == 208 then
    if selectedItem < #menu then
	  selectedItem = selectedItem + 1
    end
  end
end

function onItemSelected( menu )
  menu[selectedItem].handler()
end

--[[ Main method ]]
function main()
    while running do
	  printMenu(mainMenu)
	  event, key = os.pullEvent("key")
	  onKeyPressed(key,mainMenu)
   e()
    end
  end
end
main()
https://pastebin.com/kJLrcGnp

in that program it won't switch to the separate timer loops using the greater than or less than functions, but in the program below which is part of the code
to test it works and detects negatives.

function test()
  term.clear()
  term.setCursorPos(1,1)
  local a = 5
  local b = 10
  local c = 0
  if a > b then --Similar code used in the main program as example
    a = a * -1
    rs.setOutput("right",true)
  elseif a < b then
    a = a * -1
    rs.setOutput("left",true)
  end

  c = a + b
  print(c)
end
test()
https://pastebin.com/qa102SWD

Any help would be appriciated
Edited on 04 August 2018 - 04:09 PM
Lyqyd #2
Posted 04 August 2018 - 09:44 PM
You might try using redstone.getBundledOutput correctly, assuming you have some other function/program that would set those output colors in the first place.
GamersUnited556 #3
Posted 04 August 2018 - 10:15 PM
You might try using redstone.getBundledOutput correctly, assuming you have some other function/program that would set those output colors in the first place.
Should have mentioned that this version is modified to accept a 2nd string for colors for the bundled cables get output.
Edited on 04 August 2018 - 08:21 PM
Bomb Bloke #4
Posted 05 August 2018 - 02:20 AM
Can't really comment on whether your modified function is working without seeing the code for it. But I suggest that you use some print() and type() calls to ensure that it's giving you the output you're expecting.
Lyqyd #5
Posted 05 August 2018 - 09:50 PM
You might try using redstone.getBundledOutput correctly, assuming you have some other function/program that would set those output colors in the first place.
Should have mentioned that this version is modified to accept a 2nd string for colors for the bundled cables get output.

Emphasis added. You're passing it a number. As Bomb Bloke said, we'd need to see the modified function.

Also, please confirm that you are, in fact, setting those bundled outputs in some other piece of code running on the computer. If so, please post that code as well. If not, you'll need to set those outputs at some point.
Ta©ti_Tac0Z #6
Posted 13 August 2018 - 12:16 AM
wait elevetor? so a redstone elevator controlled by a CC computer? or a sperated mod?