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

Help with an Open Blocks radio controller

Started by hunter239833, 01 March 2014 - 01:19 AM
hunter239833 #1
Posted 01 March 2014 - 02:19 AM
i had written a program to controll 2 radios from open blocks, and it worked fine, then i discovered the terminal glasses from open peripherals, and decided to impliment them into the radio program so i could controll them from anywhere near my base. now i am having trouble with it. it runs fine once if i use station 1, but if i try any of the others, or try to run it agian, it just stops working. no error, just non responsive.i would like it to be possible to be activate agian when it finishes, so i can use it agian when i want. im getting pretty burnt out here, so i would greatly appreciate some help.
here are the codes.
the semi-working updated code:

local bridge = peripheral.wrap("top")
while true do
local event, destination = os.pullEvent("chat_command")
if destination == "radio" then
  bridge.clear()
  bridge.addText(6, 6, "You are now in the Radio Channel")
  bridge.addText(6, 15, "Please select a station: Station1, Station2, Off")
local event, station = os.pullEvent("chat_command")
if station == "Station1" then
  bridge.clear()
  bridge.addText(6, 6, "Please Select a Volume: 1-15")
  local event, volIn = os.pullEvent("chat_command")
  local volOut = tonumber(volIn)
  redstone.setOutput("back", false)
  redstone.setOutput("bottom", false)
  redstone.setAnalogOutput("back", volOut)
  bridge.clear()
  bridge.addText(6, 6, "You are Listening to")
  bridge.addText(6, 15, "Station1 Radio,")
  bridge.addText(6, 24, "Enjoy!")
  sleep(15)
  bridge.clear()
end
end
local event, station = os.pullEvent("chat_command")
if station == "Station2" then
  bridge.clear()
  bridge.addText(6, 6, "Please Select a Volume: 1-15")
  local event, volIn = os.pullEvent("chat_command")
  local volOut = tonumber(volIn)
  redstone.setOutput("back", false)
  redstone.setOutput("bottom", false)
  redstone.setAnalogOutput("bottom", volIn)
  bridge.clear()
  bridge.addText(6, 6, "You are Listening to")
  bridge.addText(6, 15, "Station2,")
  bridge.addText(6, 24, "Enjoy!")
  sleep(15)
  bridge.clear()
end
end
local event, station = os.pullEvent("chat_command")
if station == "Off" then
  bridge.clear()
  bridge.addText(6, 6, "Radio Shutting Off")
  redstone.setOutput("back", false)
  redstone.setOutput("bottom", false)
  sleep(10)
  bridge.clear()
  bridge.addText(6, 6, "Radio Off")
  sleep(5)
  bridge.clear()
end
and the working original:

local function use()
   print("Stations")
   print("Station1")
   print("station2")
end
local tArgs = { ... }
if tArgs[1] == nul then
  print("Usage:  <Station> <Volume>")
  print("Stations: Off, Station1, station2")
  print("Volume: 0-15")
end
local tArgs = { ... }
if tArgs[1] == "Off" then
  redstone.setOutput("back", false)
  redstone.setOutput("bottom", false)
  print(" Shutting Off")
  print("Bye!")
end
local tArgs = { ... }
local vol = tonumber(tArgs[2])
if tArgs[1] == "Station1" then
  redstone.setOutput("back", false)
  redstone.setOutput("bottom", false)
  redstone.setAnalogOutput("bottom", vol)
  print("You are Listening to")
  print("Station1 ")
  print("Enjoy!")
end
local tArgs = { ... }
local vol = tonumber(tArgs[2])
if tArgs[1] == "station2" then
  redstone.setOutput("back", false)
  redstone.setOutput("bottom", false)
  redstone.setAnalogOutput("back", vol)
  print("You are Listening to")
  print("station2")
  print("Enjoy!")
end

thanks!
CometWolf #2
Posted 01 March 2014 - 09:21 AM
D-D-D-double kill! Another indentation problem…
You seem to have randomly added ends at the wrong places in the new program.
hunter239833 #3
Posted 01 March 2014 - 04:26 PM
i removed the extra ends, now its giving me an error, this is why i added the extra ends in the first place, also if possible could you explain my indenting errors? im self tought and only put in the indents because the "read first" ppost suggested it to inprove readability.
the error "bios:339: [string "radio") :52: 'end' expected (to close 'while' at line 2)

i do understand a bit of what it is saying, that it errored at line 52 because there wasnt an end, but adding an extra end at the end did nothing.
also could you offer any advice on why the second station or off isnt working? perhaps maybe switch out the ends closing out the 'if's with an else?
theoriginalbit #4
Posted 02 March 2014 - 12:03 AM
okay so you've removed too many ends. this is definitely where indentation does help. so now to teach you indentation.

to indent correctly you increase the indentation level each time a code block starts and decreased when the code block ends. Here are some examples

function

local function foo()
  --# code here
end

while

while true do
  --# code here
end

repeat

repeat
  --# code here
until false

incremental for

for i = 1, 2 do
  --# code here
end

generic for

for k,v in pairs(t) do
  --# code here
end

do

do
  --# code here
end

if

if condition then
  --# code here
end

if .. else

if condition then
  --# code here
else
  --# code here
end

if .. elseif

if condition then
  --# code here
elseif condition then
  --# code here
end


So as a larger example where using multiple blocks inside one another it would end up like this

--# code starts here with zero indentation
while true do
  --# any code would be here
  for i = 1, 8 do
	--# now it'd be here
	if i < 4 then
	  --# now here
	elseif i == 4 then
	  --# here again
	else
	  --# and again
	end
	--# now it's back here
  end
  --# and back here again
end
--# and back to zero indentation until we reach a code block

NOTE: the forums does some weird formatting, but the above example each indentation adds an additional 2 spaces on top of the previous indentation level. 2 space indentation is ideal for posting on the forums as using tabs the forum doesn't display them, and using any more than 2 spaces can make the formatting look really bad!
Edited on 01 March 2014 - 11:04 PM
CometWolf #5
Posted 02 March 2014 - 11:56 AM
Just so you can see what im getting at, here's the code with proper indentation

local bridge = peripheral.wrap("top")
while true do
  local event, destination = os.pullEvent("chat_command")
  if destination == "radio" then
    bridge.clear()
    bridge.addText(6, 6, "You are now in the Radio Channel")
    bridge.addText(6, 15, "Please select a station: Station1, Station2, Off")
    local event, station = os.pullEvent("chat_command")
    if station == "Station1" then
	  bridge.clear()
	  bridge.addText(6, 6, "Please Select a Volume: 1-15")
	  local event, volIn = os.pullEvent("chat_command")
	  local volOut = tonumber(volIn)
	  redstone.setOutput("back", false)
	  redstone.setOutput("bottom", false)
	  redstone.setAnalogOutput("back", volOut)
	  bridge.clear()
	  bridge.addText(6, 6, "You are Listening to")
	  bridge.addText(6, 15, "Station1 Radio,")
	  bridge.addText(6, 24, "Enjoy!")
	  sleep(15)
	  bridge.clear()
    end
  end
  local event, station = os.pullEvent("chat_command")
  if station == "Station2" then
    bridge.clear()
    bridge.addText(6, 6, "Please Select a Volume: 1-15")
    local event, volIn = os.pullEvent("chat_command")
    local volOut = tonumber(volIn)
    redstone.setOutput("back", false)
    redstone.setOutput("bottom", false)
    redstone.setAnalogOutput("bottom", volIn)
    bridge.clear()
    bridge.addText(6, 6, "You are Listening to")
    bridge.addText(6, 15, "Station2,")
    bridge.addText(6, 24, "Enjoy!")
    sleep(15)
    bridge.clear()
  end
end
local event, station = os.pullEvent("chat_command")
if station == "Off" then
  bridge.clear()
  bridge.addText(6, 6, "Radio Shutting Off")
  redstone.setOutput("back", false)
  redstone.setOutput("bottom", false)
  sleep(10)
  bridge.clear()
  bridge.addText(6, 6, "Radio Off")
  sleep(5)
  bridge.clear()
end
See something wrong?
Inumel #6
Posted 03 March 2014 - 01:23 AM
Your while loop doesn't have an end, add one more to the very end and you should be good to go


EDIT: Also your station2 if statement has one too many ends :)/>
Edited on 03 March 2014 - 12:24 AM
hunter239833 #7
Posted 03 March 2014 - 07:46 PM
thank you guys for helping me out and explaining the identation, and also sorry i havent been able to respond untill now, some real life stuff came up that i had to sort through. after seeing it indented like that, it clears it up quite a bit, im going to fix the indentation in game and do some trial and error to find my mistakes (i think i know them, but i just want to check first)
hunter239833 #8
Posted 03 March 2014 - 08:30 PM
i see that im ending the station selection in the first station procedure, and then trying to end it agian in the second station procedure, if i remove the second end it would fix having too many ends, but that still leaves me with anything after the first procedure being ignored completely, how would fix that? the only thing i can think of is moving the station if to the end and making the whole thing into a huge if-then chain, would that be the best way to do it? or am i over thinking this?
CometWolf #9
Posted 03 March 2014 - 09:01 PM
You don't need a pullEvent for each if statement, and you should learn to use elseif and else like bit suggested.
Give this a shot.

local bridge = peripheral.wrap("top")
while true do
  local event, destination = os.pullEvent("chat_command")
  if destination == "radio" then
	bridge.clear()
	bridge.addText(6, 6, "You are now in the Radio Channel")
	bridge.addText(6, 15, "Please select a station: Station1, Station2, Off")
	local event, station = os.pullEvent("chat_command")
	if station == "Station1"
	or station == "Station2" then
	  bridge.clear()
	  bridge.addText(6, 6, "Please Select a Volume: 1-15")
	  local event, volIn = os.pullEvent("chat_command")
	  local volOut = tonumber(volIn)
	  if Station == "Station1" then
		redstone.setOutput("bottom", false)
		redstone.setAnalogOutput("back", volOut)
	  else
		redstone.setOutput("back", false)
		redstone.setAnalogOutput("bottom", volOut)
	  end
	  bridge.clear()
	  bridge.addText(6, 6, "You are Listening to")
	  bridge.addText(6, 15, station..",")
	  bridge.addText(6, 24, "Enjoy!")
	  sleep(15)
	  bridge.clear()
	elseif station == "Off" then
	  bridge.clear()
	  bridge.addText(6, 6, "Radio Shutting Off")
	  redstone.setOutput("back", false)
	  redstone.setOutput("bottom", false)
	  sleep(10)
	  bridge.clear()
	  bridge.addText(6, 6, "Radio Off")
	  sleep(5)
	  bridge.clear()
	end
  end
end
Edited on 03 March 2014 - 08:02 PM
hunter239833 #10
Posted 03 March 2014 - 09:28 PM
thank you, it worked! (except you capitalized the last instance of "station" but i caught it) i honestly feel a littlel cheap using it, as i technically didnt write it, but i appreciate it very much! and thank you for explaining it to me as well