14 posts
Location
Denmark
Posted 07 February 2013 - 12:22 AM
Hi.
As the title suggest i have an error called:
attempt to compare __le on nil and number
Heres the full code of the program:
-- Clears everything and wraps the peripheral
term.clear()
term.setCursorPos(1, 1)
m = peripheral.wrap("right")
m.clear()
m.setCursorPos(2, 2)
m.setTextColor(colors.white)
m.setBackgroundColor(colors.lime)
-- Writes the button
m.write(" ")
m.setCursorPos(2, 3)
m.write(" Activate ")
m.setCursorPos(2, 4)
m.write(" ")
m.setBackgroundColor(colors.black)
m.setCursorPos(1, 1)
-- Checks if clicked within specified x and y
while true do
event, button, x, y = os.pullEvent(monitor_touch)
if x <= 12 and x >= 2 and y <= 4 and y >= 2 then
rs.setOutput("back", true)
sleep(1)
rs.setOutput("back", false)
end
end
Basically once i pressed the button i get the error.
It won't loop through it again and check if the button is pressed again.
Thanks.
48 posts
Location
Colorado, USA
Posted 07 February 2013 - 12:35 AM
I don't know exactly what caused it, but adding in an additional sleep fixed it:P
while true do
event, button, x, y = os.pullEvent(monitor_touch)
if x <= 12 and x >= 2 and y <= 4 and y >= 2 then
rs.setOutput("back", true)
sleep(1)
rs.setOutput("back", false)
end
sleep(0.5) --This sleep here
end
Just add that one in:P I'm sure someone would be able to explain the issue or something… But it's working xD
14 posts
Location
Denmark
Posted 07 February 2013 - 12:37 AM
I don't know exactly what caused it, but adding in an additional sleep fixed it:P
while true do
event, button, x, y = os.pullEvent(monitor_touch)
if x <= 12 and x >= 2 and y <= 4 and y >= 2 then
rs.setOutput("back", true)
sleep(1)
rs.setOutput("back", false)
end
sleep(0.5) --This sleep here
end
Just add that one in:P I'm sure someone would be able to explain the issue or something… But it's working xD
Oh.
Your right!
Thanks :)/>
Now if anyone would care to explain why this works, i would be even happier :)/>
515 posts
Location
Australia
Posted 07 February 2013 - 01:12 AM
I don't know exactly what caused it, but adding in an additional sleep fixed it:P
while true do
event, button, x, y = os.pullEvent(monitor_touch)
if x <= 12 and x >= 2 and y <= 4 and y >= 2 then
rs.setOutput("back", true)
sleep(1)
rs.setOutput("back", false)
end
sleep(0.5) --This sleep here
end
Just add that one in:P I'm sure someone would be able to explain the issue or something… But it's working xD
Oh.
Your right!
Thanks :)/>
Now if anyone would care to explain why this works, i would be even happier :)/>
That actually technically didn't fix the true problem. The reason why adding a sleep worked is because when you do os.pullEvent(monitor_touch), you forgot to add quotes, so lua thinks that's nil. It being nil means it will wait for "any" event to occur. So an event occured but it didn't supply the parameters x and y, so the comparison failed, trying to compare nil with a number
Adding a sleep fixed it because sleep itself calls an event, and I think that event has enough parameters to fill x and y.
So really, your problem isnt solved, you might have noticed that if you triggered another event like keys or just a standard touchscreen event it may stuff up/not work as you expected. So to fix this just add quotes to monitor_touch: os.pullEvent("monitor_touch")
2088 posts
Location
South Africa
Posted 07 February 2013 - 01:51 AM
Adding a sleep fixed it because sleep itself calls an event, and I think that event has enough parameters to fill x and y.
If I'm not mistaken, the event it uses is os.startTimer() and queues for the timer - which doesn't supply x and y?
515 posts
Location
Australia
Posted 07 February 2013 - 02:47 AM
Adding a sleep fixed it because sleep itself calls an event, and I think that event has enough parameters to fill x and y.
If I'm not mistaken, the event it uses is os.startTimer() and queues for the timer - which doesn't supply x and y?
Hmm yeah, I'm not so sure what's really happening. I suppose you'll need to use a event logger to see :P/>
2005 posts
Posted 08 February 2013 - 10:37 AM
The sleep function eats all events up to and including the timer it set.