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

Need suggestion on looping automated response system

Started by zsakul, 13 June 2013 - 08:29 PM
zsakul #1
Posted 13 June 2013 - 10:29 PM
So to make a long story short, I have a program on one computer to control doors, timers, and check on the status of the mentioned items (IE. Type "status" and it should spit out whether doors are open or timers are on)
Alright, so if this can be done on one computer, that would be nice but I dont know how.
Current script

______________________________________________________________
rednet.open("top")
id, message = rednet.receive()
id, stop = rednet.receieve()

local a = 1
local b = 1
local c = 1

repeat
if message == "status" then
print(message) –This was just to check if the message was getting through to the computer
rednet.send(19, a)
rednet.send(19, B)/>– I have the hope that just typing a b or c would transmit the local numbers typed earlier
rednet.send(19, c)
sleep(10)
else
print("Input failure")
end
until message == "stop"

_______________________________________________________________

ALRIGHT so the current problem. It loops infinitely with no end. I'd like it to go through 1 cycle then wait for "status" to be transmitted to the computer again before re-transmitting variables a,b and c.

Any answer much obliged,

Thanks in advance!
-Z
Lyqyd #2
Posted 13 June 2013 - 11:20 PM
Split into new topic.

You aren't changing the value of `message` once you enter the loop, so it will either run once, or continue running forever.
theoriginalbit #3
Posted 13 June 2013 - 11:26 PM
the solution for this is quite simple actually, just move this

id, message = rednet.receive()
into the top of your repeat loop, it will then wait there until a message is received.


Also what is the point of the following 4 lines of code?

id, stop = rednet.receieve()
local a = 1
local b = 1
local c = 1
the first line will halt until it gets a second message, also overriding the first messages, "id" variable…
will a, b, and c ever be incremented or are they always going to be 1? If they're always going to be 1 you could just put them in the rednet.send calls as a `magic number` instead of storing them in a variable to then be sent, or alternatively store them in just one variable.

Also welcome to the forums. A little tip for the future, these forums have [code][/code] tags which format the code better, as you can see above, they also preserve indenting of code, meaning it is a really good idea to use them :)/> also if the code is really long, it is a good idea to use pastebin.com, because most of us don't like counting down to a line number…
zsakul #4
Posted 13 June 2013 - 11:39 PM
Thank you for your responses I will try shortly!

the solution for this is quite simple actually, just move this

id, message = rednet.receive()
into the top of your repeat loop, it will then wait there until a message is received.


Also what is the point of the following 4 lines of code?

id, stop = rednet.receieve()
local a = 1
local b = 1
local c = 1
the first line will halt until it gets a second message, also overriding the first messages, "id" variable…
will a, b, and c ever be incremented or are they always going to be 1? If they're always going to be 1 you could just put them in the rednet.send calls as a `magic number` instead of storing them in a variable to then be sent, or alternatively store them in just one variable.

Also welcome to the forums. A little tip for the future, these forums have [code][/code] tags which format the code better, as you can see above, they also preserve indenting of code, meaning it is a really good idea to use them :)/> also if the code is really long, it is a good idea to use pastebin.com, because most of us don't like counting down to a line number…

Also those bottom 3 locals are going to be changed so that certain things can change the values like a = a + 1 then when a = 2 the receiving computer will recognize "if rednet.receive() == "a=2"" then detect the doors are open, sorry if I can't describe it very well =/.

Thank you for the greetings! I will be sure to utilize the code tags and that website.

EDIT: BTW I must have misunderstood the purpose of the id function, I though I can use it repeatedly without conflicting arguments. As hard as it may be to believe, I've only started learning Lua 2 days ago with no previous programming experience.
theoriginalbit #5
Posted 13 June 2013 - 11:52 PM
No problems :)/>

EDIT: BTW I must have misunderstood the purpose of the id function, I though I can use it repeatedly without conflicting arguments. As hard as it may be to believe, I've only started learning Lua 2 days ago with no previous programming experience.
Yeh that is ok. but the `id` variable will be overwritten each time. So say we have the code

id, msg = rednet.receive()
id, msg = rednet.receive()
then the `id` and `message` variables will only contain the last message received. You will need to do it like this


id, msg = rednet.receive()
id2, msg2 = rednet.receive()
or just have a better thinking method about how you're wanting to receive messages.
zsakul #6
Posted 14 June 2013 - 12:29 AM
Hey
ComputerCrafter,

so your solution worked, to an extent. The program is no longer endlessly cycling, however it no longer accepts wireless inputs after the first loop. If you type anything into the receiving computer the loop repeats and it again accepts the wireless message (once).

Thanks again!
-Z

EDIT: MY MISTAKE!, for some unfathomable reason I somehow snuck a "read()" command in there, causing the cycle to fail after 1 try. Thank you for your help and I will post the full script to my program when complete!
zsakul #7
Posted 14 June 2013 - 02:20 PM
Hello, so… I kinda have 2 problems. Long story short.

A) The variables a,b and c used in lines 3, 4, 5, 10, 12, 14,16, 18 and 20 aren't displaying properly in the print() function used in lines 37-39.
My intent was to have this as an output on the local computer

Message "tdo" received

What I want as an output from "tdo":
S2 Ignored
2
1
1

What I actually get:
S2 Ignored
1
1
1

Therefore something isn't being recognized.

B)If someone wouldn't mind telling me if I did lines 27-29 right I'd appreciate it. I essentially just want the variable outputs for a, b, and c sent to another computer, so just wasn't sure if I plugged that in right. (I haven't tested whether the receiving computer is getting the a,b and c message cause I'd have to write another program for that to work, so want this done 1 step at a time.


[list]
rednet.open("top")

local a = 1
local b = 1
local c = 1

repeat
id, message = rednet.receive()
if message == "tdo" then
local a = 2
elseif message == "alo" then
local b = 2
elseif message == "cdo" then
local c = 2
elseif message == "tdc" then
local a = 1
elseif message == "alc" then
local b = 1
elseif message == "cdc" then
local c = 1
else
print("S1 Ignored")
end

if message == "status" then
print(message)
rednet.send(19, a)
rednet.send(19, b)
rednet.send(19, c)
elseif message =="door" then
redstone.setOutput("back", true)
sleep(0.5)
redstone.setOutput("back", false)
else
print("S2 Ignored")
end
print(a)
print(b)
print(c)
until message == "stop"
[/list]

Thanks for your time!
-Z

EDIT: I'm so sorry for some reason I can't get the list function to work, I'm new to this =/. I'm using pastebin.com, selected lua and i either get a huge list of giberish when I post, or just raw code =/…
Lyqyd #8
Posted 14 June 2013 - 02:48 PM
Take away the `local` from each assignment in the if block. You're declaring new local variables that exist solely within the scope of that if block rather than changing the values of the variables of the above scope.

And it's better to post the code here than to post it on pastebin anyway, unless it's huge.
Lyqyd #9
Posted 14 June 2013 - 02:50 PM
Threads merged. Please stick to one topic for multiple questions about one program/piece of code.
zsakul #10
Posted 14 June 2013 - 03:00 PM
Take away the `local` from each assignment in the if block. You're declaring new local variables that exist solely within the scope of that if block rather than changing the values of the variables of the above scope.

Thank you worked like a charm! Also, I want to try and make reading code as easy on the eyes as I can, short of putting numbers before every line is there actually a command that will do it automatically?
Bomb Bloke #11
Posted 14 June 2013 - 07:18 PM
If you want line numbers, use a text editor that automatically displays them without actually putting them in the file. I use TextPad myself, but there are lots of such editors out there. Notepad++ is popular.

Indent your code! Whenever you go in to a "for", "while", "if", etc block, add a tab or space to the start of each line within that block. Whenever you exit the block, move the following text back to the left. This makes it FAR easier to read, as it's immediately obvious what code is within which blocks. Eg, the below is a mess:

for x=1,30 do
if x%3==0 then
y=0
while y==0 do
y=y+1
end
end
end

With indentation, it's clear what happens when:

for x=1,30 do
  if x%3==0 then
    y=0
    while y==0 do
      y=y+1
    end
  end
end

A decent text editor may automatically handle indentation for you.
zsakul #12
Posted 15 June 2013 - 04:02 PM
Hey all! So something is really bugging me on a few computers. I attempt to run lua, and then enter shell.run(startup). Computer responds with "false". Why?

Thanks for your time!
-Z
Bomb Bloke #13
Posted 15 June 2013 - 07:38 PM
"startup" SANS quotes is a variable, one you haven't defined - hence you're not telling it to run anything. "startup" WITH quotes is a string, and tells it to run the file (if it exists).
zsakul #14
Posted 16 June 2013 - 02:11 PM
I feel very special right now -_-/>. Thank you lol

-Z