Jump to content
  • Sky
  • Blueberry
  • Slate
  • Blackcurrant
  • Watermelon
  • Strawberry
  • Orange
  • Banana
  • Apple
  • Emerald
  • Chocolate
  • Charcoal
  • 0
PHOBOSS98

How do you make a loop stop on user input?

Question

10 answers to this question

Recommended Posts

  • 0
local component = require("component")
local event = require("event")
local m = component.modem -- # make local too
m.open(1) -- # 001 ??

repeat
  -- # run in the loop to repeat reception of 'cmd'
  local cmd = select(6, event.pull("modem_message")) -- # this is cleaner
until cmd == "stop"

 

Edited by Molinko
Link to post
Share on other sites
  • 0

I do need a loop that can stop by a modem event.. but you see when I tried the code you sent me (with a few changes):

local component = require("component")
local event = require("event")
local m = component.modem
m.open(1)

repeat
print("hello") --#the thing
  local cmd = select(6, event.pull("modem_message"))
until cmd == "stop" 

it did end the program when I sent it the message but it doesn't print it repeatedly it just does this: 

cro.thumb.png.74d5a8838dc98b1da9abe6040dfafa3f.png

and so I tried and tested something and changed the code:

local component = require("component")
local event = require("event")
local m = component.modem
m.open(1)

repeat
  print("hello")
  local cmd = select(6, event.pull("modem_message"))
  print("hi")--#the new thing
until cmd == "stop"

and ended up with this:
sdf.png.69c4332d32c8e2c1213f0db0d55a3437.png

(note: i didn't end the program this time)

it seems that the loop stops at 

local cmd = select(6, event.pull("modem_message"))

but still thinks it's running since when I broadcasted the stop command it did this:

asdasdsfdfsad.png.63e42430e108661dda909cf4cc44976e.png

 

Link to post
Share on other sites
  • 0

Well, it executes exactly what's written in the code, and is the expected behavior. event.pull("modem_message") stops the program (more specifically, the thread) until it gets the modem_message event. You may want to set a timeout, so that the pull would stop early — returning nil in such case; for example, event.pull(1, "modem_message") sets the timeout to 1 second.

Let's step through the program line by line. The first 5 lines don't cause any difficulties, so we skip them. There's the repeat-until loop, and the first iteration is run unconditionally. First, the string "hello" is printed to the screen. After that there's the event.pull line, which (as I said before) stops the program until it gets the modem message.

The execution now does not go any further. The program is running — it just waits for a modem message now. You broadcast a stop message; the program gets it, and goes on. select is called, and the actual message is set to the cmd variable.

Only then "hi" is written. As the condition is satisfied, the loop stops, and the program exits.

if this is not what you wanted, please clarify the intended behavior.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use and Privacy Policy.