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

Touchscreen Control of Modems

Started by Cleveland, 13 December 2013 - 09:23 AM
Cleveland #1
Posted 13 December 2013 - 10:23 AM
Alright, so I'm trying to make a central touchscreen that has buttons for each thing I am wanting to control, and I am a bit of a noob. I am using wireless modems since RP2 left me without bundled cable. I want a singular modem from the central screen to send out specific messages according to which button is pressed. I keep getting an error that says " rednet : 11 : string expected" although I am sure that isn't the only thing wrong with my code. Any help is appreciated. Here is the code:

**Edit Here is the raw code. sorry about that, I'm a newcomer still learning the ropes.

Spoilermonside = "top" –enter the side your monitor is on
txtsize = 1 –enter desired size of text
mdmside = right –enter side rednet modem is on

rednet.open(mdmside)
mon = peripheral.wrap(monside)
mon.setTextScale(txtsize)
mon.setBackgroundColor(colors.black)
mon.clear()
mon.setCursorPos(1,1)
w,h = mon.getSize()
write(w)
write(" x ")
write(h)
local button = {}
function properties(name, xmin, xmax, ymin, ymax, sendtoid)
button[name] = {}
button[name]["active"] = false
button[name][xmin] = xmin
button[name][xmax] = xmax
button[name][ymin] = ymin
button[name][ymax] = ymax
button[name][message] = message
end
– may need to insert toggle function here
function toggle()
if button["Engines"]["active"] == true then
rednet.broadcast(message)
else
rednet.broadcast("Engines Off")
print("Engines Off")
end
end–ends function toggle()
function ButtonName() –Where you can add more buttons
properties("Engines", 1, 10, 1, w, Engines)

end
function colorfill(text, color, bData)
–determines where buttons are located
mon.setBackgroundColor(color)
local yspot = math.floor((bData["ymin"] + bData["ymax"]) /2)
local xspot = math.floor((bData["xmax"] - bData["xmin"] - string.len(text)) /2) +1
for j = bData["ymin"], bData["ymax"] do
mon.setCursorPos(bData["xmin"], j)
if j == yspot then
for k = 0, bData["xmax"] - bData["xmin"] - string.len(text) + 1 do
if k == xspot then
mon.write(text)
else
mon.write(" ")
end
end
else
for i = bData["xmin"], bData["xmax"] do
mon.write(" ")
end
end
end
mon.setBackgroundColor(colors.black)
end

function screen()
local currColor
for name, data in pairs(buttons) do
local on = data["active"]
if on == true then
currColor = colors.lime
else
currColor = colors.red
fill(name, currColor, data) –may need to be colorfill
end
end
end

function checkxy(x, y)
for name, data in pairs(button) do
if y >= data["ymin"] and y <= data["ymax"] then
if x>= data["xmin"] and x <= data["xmax"] then
toggle(data["active"])
data["active"] = not data["active"]
print(name)
end
end
end
end
ButtonName()
while true do
mon.clear()
screen()
local e,side,x,y = os.pullEvent("monitor_touch")
print(x..":"..y)
checkxy(x,y)
sleep(.1)
end

So I think I've sorted that problem out, but now it says it has an "error comparing __le to nil" in the function checkxy(x, y) in the first IF statement. Heres the updated code:

Spoilermonside = "top" –enter the side your monitor is on
txtsize = 1 –enter desired size of text
mdmside = right –enter side rednet modem is on

rednet.open("right")
mon = peripheral.wrap("top")
mon.setTextScale(1)
mon.setBackgroundColor(colors.black)
mon.clear()
mon.setCursorPos(1,1)
h,w = mon.getSize()
print(w.."x"..h)
local button = {}
function properties(name, xmin, xmax, ymin, ymax)
button[name] = {}
button[name]["active"] = false
button[name][xmin] = xmin
button[name][xmax] = xmax
button[name][ymin] = ymin
button[name][ymax] = ymax
–button[name][message] = message
end
function toggle()
if button["Engines"]["active"] == true then
rednet.broadcast("EnginesOn")
print("EnginesOn")
else
rednet.broadcast("EnginesOff")
print("Engines Off")
end
end–ends function toggle()
function ButtonName() –Where you can add more buttons
–properties("ButtonName", xmin, xmax, ymin, ymax)
properties("Engines", 1, 29, 1, 5)

end
function colorfill(text, color, bData)
–determines where buttons are located
mon.setBackgroundColor(color)
local yspot = math.floor((bData["ymin"] + bData["ymax"]) /2)
local xspot = math.floor((bData["xmax"] - bData["xmin"] - string.len(text)) /2) +1
for j = bData["ymin"], bData["ymax"] do
mon.setCursorPos(bData["xmin"], j)
if j == yspot then
for k = 0, bData["xmax"] - bData["xmin"] - string.len(text) + 1 do
if k == xspot then
mon.write(text)
else
mon.write(" ")
end
end
else
for i = bData["xmin"], bData["xmax"] do
mon.write(" ")
end
end
end
mon.setBackgroundColor(colors.black)
end

function screen()
local currColor
for name, data in pairs(button) do
local on = data["active"]
if on == true then
currColor = colors.lime
else
currColor = colors.red
–fill(name, currColor, data) –may need to be colorfill
end
end
end

function checkxy(x, y)
for name, data in pairs(button) do
if y >= data["ymin"] and y <= data["ymax"] then
if x>= data["xmin"] and x <= data["xmax"] then
toggle(data["active"])
data["active"] = not data["active"]
print(name)
end
end
end
end
ButtonName()
while true do
mon.clear()
screen()
local e,side,x,y = os.pullEvent("monitor_touch")
print(x..":"..y)
checkxy(x,y)
sleep(.1)
end
Edited on 13 December 2013 - 02:53 PM
Csstform #2
Posted 13 December 2013 - 12:49 PM
Put that in spoilers please!
Spoiler
Your code is also extremely hard to read. Can you post the raw text?

Correct me if I'm wrong, but I believe 'w' is being returned as an int. The 'write' function needs a string. If it was java, I'd know how to fix it. However this is lua. Somehow you need to get 'w' to be recognized as a string.

'h' will also need fixed.
Edited on 13 December 2013 - 11:52 AM
mrpoopy345 #3
Posted 13 December 2013 - 03:53 PM
I believe it is because there is no such thins as "write". You would have to use something like term.write() or print.
Quick rewrite:

monside = "top" --enter the side your monitor is on
txtsize = 1 --enter desired size of text
mdmside = right --enter side rednet modem is on

rednet.open(mdmside)
mon = peripheral.wrap(monside)
mon.setTextScale(txtsize)
mon.setBackgroundColor(colors.black)
mon.clear()
mon.setCursorPos(1,1)
w,h = mon.getSize()
term.write(w)
term.write(" x ")
term.write(h)
local button = {}
function properties(name, xmin, xmax, ymin, ymax, sendtoid)
button[name] = {}
button[name]["active"] = false
button[name][xmin] = xmin
button[name][xmax] = xmax
button[name][ymin] = ymin
button[name][ymax] = ymax
button[name][message] = message
end
-- may need to insert toggle function here
function toggle()
if button["Engines"]["active"] == true then
rednet.broadcast(message)
else
rednet.broadcast("Engines Off")
print("Engines Off")
end
end--ends function toggle()
function ButtonName() --Where you can add more buttons
properties("Engines", 1, 10, 1, w, Engines)

end
function colorfill(text, color, bData)
--determines where buttons are located
mon.setBackgroundColor(color)
local yspot = math.floor((bData["ymin"] + bData["ymax"]) /2)
local xspot = math.floor((bData["xmax"] - bData["xmin"] - string.len(text)) /2) +1
for j = bData["ymin"], bData["ymax"] do
mon.setCursorPos(bData["xmin"], j)
if j == yspot then
for k = 0, bData["xmax"] - bData["xmin"] - string.len(text) + 1 do
if k == xspot then
mon.write(text)
else
mon.write(" ")
end
end
else
for i = bData["xmin"], bData["xmax"] do
mon.write(" ")
end
end
end
mon.setBackgroundColor(colors.black)
end

function screen()
local currColor
for name, data in pairs(buttons) do
local on = data["active"]
if on == true then
currColor = colors.lime
else
currColor = colors.red
fill(name, currColor, data) --may need to be colorfill
end
end
end

function checkxy(x, y)
for name, data in pairs(button) do
if y >= data["ymin"] and y <= data["ymax"] then
if x>= data["xmin"] and x <= data["xmax"] then
toggle(data["active"])
data["active"] = not data["active"]
print(name)
end
end
end
end
ButtonName()
while true do
mon.clear()
screen()
local e,side,x,y = os.pullEvent("monitor_touch")
print(x..":"..y)
checkxy(x,y)
sleep(.1)
end
Edited by
Cleveland #4
Posted 13 December 2013 - 04:04 PM
Alright, updated everything. Continuing need of help D:
amtra5 #5
Posted 13 December 2013 - 10:01 PM
On your third line, you state mdmside = right

right is a string, so it should be mdmside = "right"
Ajt86 #6
Posted 14 December 2013 - 02:02 AM
Always define a string using "", you typed:
mdmside = right
And that's saying that right must be a variable, but in fact it is nil (or null) because you didn't define it, you're program is erroring out at
rednet.open(mdmside)
Even though it should have been:

mdmside = "right"
Because instead of passing in a type string, you're passing in a type nil. I hope this helped you understand what you did wrong.
Cleveland #7
Posted 15 December 2013 - 11:37 AM
You guys were right! I made a few other tweaks and it works beautifully now. Thanks for your help!
Bomb Bloke #8
Posted 15 December 2013 - 06:19 PM
I am using wireless modems since RP2 left me without bundled cable.
It may or may not be worth noting that ComputerCraft 1.51 (the first version to move to MineCraft 1.5, leaving Redpower behind) added wired modems, and as of CC 1.53, Minefactory Reloaded's cables can be used in the same manner as RedPower's.