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

[Solved]Light flashing loop

Started by NeeFoo, 30 August 2012 - 06:03 PM
NeeFoo #1
Posted 30 August 2012 - 08:03 PM
So basically im working on a simple program that makes my pretty lights flash until i turn it off:P
I have two colors, blue and cyan(both connected to different colored cables which are connected to a bundled cable)
I want the blue one to flash first then the cyan one then the blue and so on..

i have this code to work from right now, but i dont know where to go from here(or if its right at all).


term.clear()
term.setCursorPos(1,1)
print("lights on or off?")
  input = read()
answer = "on"
if input == answer then
  rs.setBundledOutput("right", colors.blue)
  sleep(1,0)
  rs.setBundledOutput("right", colors.cyan)
  sleep(1,0)

i also wonder if there's any way to turn it off again without using ctrl+t

Thanks in advance for any help! :)/>/>
OmegaVest #2
Posted 30 August 2012 - 08:23 PM
Okay, so you need a loop of some sort. I would suggest a while loop. Potentially a while loop with a timer, but that is extra.

Basically, what you want is this


local answer = "on"

term.clear()
term.setCursorPos(1,1)

print("Are the lights on or off?")
input = read()
input = lower(input)

if anser == input then
   while true do
	  rs.setBundledInput("right", colors.blue)
	  sleep(1.0)  -- That's not a comma. It's a decimal.
	  rs.setBundledInput("right", colors.cyan)
	  sleep(1.0)
   end
else
   print("What exactly happens here? Why does this need to be asked?")
end

And, presently, no, there is no way to turn it off save a shutdown. Though you could do this:



local answer = "on"

term.clear()
term.setCursorPos(1,1)

print("Are the lights on or off?")
input = read()
input = lower(input)

if anser == input then
   Timee["blue"] = os.startTimer(1.0)
   Timee["cyan"] = os.startTimer(2.0)
   while true do
	  event, arg1, arg2 = os.pullEvent()
	  if event == "timer" then
		 rs.setBundledOutput("right", colors[arg1])
		 Timee[arg1] = os.startTimer(2.0)
	  elseif event == "char" and arg1 == "q" then
		 break
	  end
   end
else
   print("What exactly happens here? Why does this need to be asked?")
end

If I remember correctly, this should work perfectly. If my memory is as faulty as my computer's . . . well, it should work the first time.
NeeFoo #3
Posted 30 August 2012 - 08:59 PM
that last code produced " :10: 'then' expected "
OmegaVest #4
Posted 30 August 2012 - 09:09 PM
I misspelt answer on he if line. Otherwise it should work. . I think.
NeeFoo #5
Posted 30 August 2012 - 09:11 PM
I misspelt answer on he if line. Otherwise it should work. . I think.
yeah i corrected that one when i entered the code earlier
still produdes the error
OmegaVest #6
Posted 30 August 2012 - 09:17 PM
Uhmm. No idea. That line is sound. Unless. . .

Okay, add


Timee = {}

just before the other Timee calls. I'm not sure if that will fix it, so I'm more or less just saying crap until it works. Or I think of a better solution.
NeeFoo #7
Posted 30 August 2012 - 09:23 PM
still the same error:/
OmegaVest #8
Posted 30 August 2012 - 09:31 PM
Then I have no idea. Nothing around that line is non-working. But, as a last resort, do this:


Take out that whole outer if statement. If the lights are on, great. If they are not, even better. The board will still switch to a new light when the cycle starts anyway. Otherwise, I don't know why you would be turning on the lights. This includes the read call, because at present it isn't necessary, and the local answer = "on". And the elseif that was mostly a joke to begin with.

If it still doesn't work, then something else is amiss, and I don't think it's my code at that point.
NeeFoo #9
Posted 30 August 2012 - 09:34 PM
i dont really need the "on" no :)/>/>
im going to be running it as a "startup" from a floppy

so how would the code look if i take out the if statement? im new to this so im a little confused as to how to set it up correctly B)/>/>
OmegaVest #10
Posted 30 August 2012 - 09:38 PM

Timee = {}
Timee{"blue"] = os.startTimer(1.0)
Timee{"cyan"] = os.startTimer(1.0)



Okay, this is literally as far as I got before I realized my mistake, which is pretty massive. Hold that thought for just one minute while I rearrange my code a bit.
OmegaVest #11
Posted 30 August 2012 - 09:44 PM
DOUBLE POST TIME!



Timee = {}
Timee{"blue"] = os.startTimer(1.0)
Timee{"cyan"] = os.startTimer(2.0)

while true do
   event, arg1, arg2 = os.pullEvent()
   if event == "timer" then
      for i, v in pairs(Timee) do
         if arg2 == Timee[i] then
            colore = Timee[i]
         end
      end
      rs.setBundledOutput("right", colors[colore])
      Timee[colore] = os.startTimer(2.0)
   enlseif event == "char" and arg1 == 'q' then
      break
   end
end

This is a revised code which, again, I think will work. My mistake was in my use of timer. Now it should be better. I think.
NeeFoo #12
Posted 30 August 2012 - 10:03 PM
Now it produces:

:2: '}' expected
OmegaVest #13
Posted 30 August 2012 - 10:18 PM
And, we're back to What Fresh H is This?

Oh. Change the { in lines 2 and 4 to [. I hit shift too early.
NeeFoo #14
Posted 30 August 2012 - 10:30 PM
:13: ')' expected
^ scrap this one, that was my own doing xD

new one after fixing my old one is:
:13: bad aguement: int expected, got nil
Edited on 30 August 2012 - 09:07 PM
NeeFoo #15
Posted 31 August 2012 - 10:40 AM
anyone got a solution to this?
OmegaVest #16
Posted 31 August 2012 - 07:10 PM
Okay, so run this in the middle of the i, v for loop.


print(i)
print(v)


Comment out any line that has the term colore in it, and tell me if that runs.
NeeFoo #17
Posted 31 August 2012 - 08:39 PM
Comment out? what do u mean? :)/>/>
Magus #18
Posted 31 August 2012 - 10:38 PM
in OmegaVests program this

    if arg2 == Timee[i] then
       colore = Timee[i]
    end
should be

   if arg1 == v then
      colore = i
   end
and enlseif should be elseif

you can also swap the keys and values in Timee and do without the for loop
like this

Timee = { [os.startTimer(1.0)] = colors.blue,
                 [os.startTimer(2.0)] = colors.cyan }


while true do
   event, arg1 = os.pullEvent()
   if event == "timer" then
	  colore = Timee[ arg1 ]
	  rs.setBundledOutput("right", colore)
	  Timee[os.startTimer(2.0)], Timee[ arg1 ] = colore, nil
   elseif event == "char" and arg1 == 'q' then
	  break
   end
end
NeeFoo #19
Posted 01 September 2012 - 02:00 AM
Ohh my it worked! :)/>/> Thanks to you both for your help and time! I appreciate it!