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.
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.
Any help would be appriciated
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/kJLrcGnpin 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/qa102SWDAny help would be appriciated
Edited on 04 August 2018 - 04:09 PM