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

Trying to create a Traffic light script. Need help!

Started by Nicecool19, 10 May 2017 - 01:29 PM
Nicecool19 #1
Posted 10 May 2017 - 03:29 PM
So, I'm trying to create my own script to use the monitor as a traffic light. The traffic light uses a proximity sensor to detect a player and change the screen background from red to green. I'm not at my computer to show you all of what I have done, but I really need help.(I am a NOOB at this. No knowledge at all, and very new to Lua and Computercraft. Opencomputers is way too complicated to me but I think both CC and OpenComp are very cool.) This is why I am trying to create something simple, but that doesn't seem to work.

I can try to replicate what I did:

m = peripheral.wrap("back")
while true do <—-(to loop)
if rs.getInput("top") then <—–(to grab redstone signal)
m.setBackgroundColor(colors.green)
else
m.clear()
m.setBackgroundColor(colors.red)
m.clear()
end

Sorry about the blank spots, once I get home I'll look at what I did and add it here. So, basically, I want the background color to change green when Redstone is detected, and red when there is no input. I'll reverse this order for the horizontal lanes of traffic. Or, if some could show me how to transmit signals wirelessly. As in: I have the proximity sensor near the spot I would like to monitor, then the computer next to the sensor will receive the input, and wirelessly transmit the setBackgroundColor to the actual traffic light. I actually like this way better. All I need to include is the wireless signal, and what the receiving computer should do.. If I only knew how…
Edited on 10 May 2017 - 07:08 PM
KingofGamesYami #2
Posted 11 May 2017 - 01:23 AM
In computercraft you *have* to yield. BombBloke has a long post saved that talks about this, but basically you have to pull an event or something else that "pauses".

In your case, since you're waiting for the redstone signal, you should wait for a "redstone" event.


m = peripheral.wrap( "back" )
while true do --# This is good
  if rs.getInput( "top" ) then --# this is also good
    m.setBackgroundColor( colors.green ) --# also correct
  else
    m.setBackgroundColor( colors.red ) --#notice I removed the clear call.  This will be explained later...
  end
  m.clear() --#Why is this here?  Well, you want to update the screen in both cases,
            --#so you don't need to put it inside the if statement at all
  os.pullEvent("redstone") -- wait for a change to occur in the redstone state.
end

As for the wireless thing: I'd go with network cable/wired modems. Then you just have to change the way you wrap the monitor. It's certainly possible to do wirelessly, but it adds a layer of complexity and isn't secure. Not that security is really a concern with this sort of thing.
Nicecool19 #3
Posted 11 May 2017 - 10:52 AM
Alright, thank you so much! I appreciate your help!

Hmmm, I keep getting an attempt to index error.

My bad, an "Expected number" error.

Fixed! Thank you so much!