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

[lua] [question] problem with redstone and message board

Started by DarkBebi, 28 December 2012 - 06:16 PM
DarkBebi #1
Posted 28 December 2012 - 07:16 PM
This is my first post so here goes nothing

I have a message board and it has 1 page on it full of rules and everything! looks amazing

but I want to create multiple pages or essentially messages so I attached a redstone to my comptuer and I came up with this code

local green=false
local white=false


while true do
if rs.testBundledInput("right",colors.green) then
  if not green then
    mon = peripheral.wrap("top")
    mon.clear()
    mon.setCursorPos(1,1)
    mon.write("Rule1:Blah Blah Blah Blah")
    green=true
  end
else
   green=false
end
sleep(0.1)
end

now how do I go about to pushing the same button execute a second message? I got some help from the people on the IRC channel but the code I got back was incomplete or did not work, this was their code


local presses
lastState = 0
rs.getInput("back") 
while true do
os.pullEvent("redstone")
if lastState and not rs.getInput("back") then
lastState = false
presses = presses + 1
print(presses) elseif not
lastState and rs.getInput("back") then
lastState = true
end
end

sorry if that isnt entered the right way, got it off of IRC, lol

so that code only outputs a 1 and nothing else


Now my question is how do I create a message board that when I press a button it switches to the next page?
Grim Reaper #2
Posted 28 December 2012 - 07:38 PM
Well, it would seem that the code they gave you IS only supposed to print the number of presses.

Here is some code that isn't complete but you should be able to add it to your existing code without any issues.
Code:
Spoiler


-- NOTE: All functions that draw or do something related to drawing use
--	   a global handle on the wrapped monitor called 'monitor'.

-- Replace 'monitorSide' with whatever side your monitor is on.
local monitor = peripheral.wrap(monitorSide)

-- All of the pages of rules. Each table within this table is a page.
-- Each entry in a table within this table is a rule. The number is up to you to keep track of.
local pages = {
	[1] = {
		"Rule 1: This is the first rule.",
		"Rule 2: This is the second rule.",
		"Rule 3: This is the third rule."
	},

	[2] = {
		"Rule 4: This is the fourth rule.",
		"Rule 5: This is the fifth rule.",
		"Rule 6: This is the sixth rule."
	}
}
local currentPage = 1 -- What page we are current on.

-- Draws the given page to the screen.
function drawPage(page)
	-- Clear the screen.
	monitor.clear()

	-- Draw the given page table.
	for ruleIndex, rule in ipairs(page) do
		monitor.setCursorPos(1, ruleIndex)
		monitor.write(rule)
	end
end

-- Waits for a button press to change the page.
-- Make sure to change this redstone logic to whatever it is your setup will
-- require.
function waitForButtonPress()
	local event = os.pullEvent()

	-- If the event was a redstone pulse, then we should change the page.
	if event == "redstone" then
		-- If the current page is the last page, then switch to the first page. However, if it is not the last page,
		-- then go to the next page.
		if currentPage == #pages then
			currentPage = 1
		else
			currentPage = currentPage + 1
		end
	end
end

-- Main loop.
-- Make sure to substitute whatever code is necessary for this to work with your setup.
while true do
	local page = pages[currentPage] -- The current page table we are on.

	-- Draw the current page of rules and wait for a button press to cycle the pages.
	drawPage(page)
	waitForButtonPress()
end


Change the redstone event logic to whatever it is your setup is. I'm not all that familiar with bundled cables so I don't think I'll be much help in that department, but it looks like you should be able to handle that.

Hope I helped,
PaymentOption.
DarkBebi #3
Posted 28 December 2012 - 11:17 PM
thank you so much Grim Reaper but i am a complete noob at this sort of stuff right now, my code is basically an elevator code that I whipped up, and I used multiple buttons for that to work so using 1 button to display multiple parts of text is beyond me. I have typed in your code in cc and i got a
bios:355: bad argument: string expected, got nil

don't know how to fix that, never got that problem before