Jump to content




Help! Easy for anyone


  • You cannot reply to this topic
18 replies to this topic

#1 weareborg2913

  • Members
  • 10 posts

Posted 02 February 2014 - 10:33 PM

i just need a program that toggles output on the right and left side + bottom

It should be controlled by buttons on the screen above... (7 long * 3 high)

The sides are for nuke control, the bottom is for emergancy door close

#2 awsmazinggenius

  • Members
  • 930 posts
  • LocationCanada

Posted 02 February 2014 - 10:58 PM

So, control redstone? If you'd like, you can PM me with some more details and I can code it, but recently a few topics have been moved to General for requests. If you want me to code it, do you want it ASAP with minimal testing, or a nice UI and some testing?

#3 surferpup

  • Members
  • 286 posts
  • LocationUnited States

Posted 02 February 2014 - 11:27 PM

Please see the Redstone Basics tutorial. It will teach you exactly how to do toggle redstone. As for buttons and touch screen, please see responses to this question which will give you the tutorials you need to figure this out. What you are asking is not all that difficult.

This is Ask a Pro, where we answer questions from people who are writing programs, not for where we take program requests. There are dozens of examples of what you are trying to do. You will be much better off learning to code it yourself.

Edited by surferpup, 02 February 2014 - 11:29 PM.


#4 awsmazinggenius

  • Members
  • 930 posts
  • LocationCanada

Posted 03 February 2014 - 12:12 AM

surferpup, you do make a good point. When I make the program, I planned to add lots of comments for when he does decide to learn. But sometimes it's just easier to get someone else to do it for you.

#5 surferpup

  • Members
  • 286 posts
  • LocationUnited States

Posted 03 February 2014 - 01:28 AM

Here is an outline for you:
  • Establish connections to your monitors using monitor=peripheral.wrap("whateverSide")
  • Draw your buttons using monitor.setCursorPosition(x,y) and monitor.write (check out the term API)
  • turn your redstone.setOutput(side) to on/off for each side you are using according to your needs (if you need bundled cables, there are tutorials on this as well)
  • start your while loop to check for monitor touch events (os.pullEvent("monitor_touch)
  • If the button was pressed, do what you need to do.
  • continue your while loop.
I know it may be easier for you to start with an already created routine, but it is so much more fulfilling to do it yourself.

It is very kind of awsmazinggenius to offer to write code to start you out, but you really can do this yourself.

Edited by surferpup, 03 February 2014 - 01:29 AM.


#6 surferpup

  • Members
  • 286 posts
  • LocationUnited States

Posted 03 February 2014 - 03:47 AM

Alright awsmazinggenius-- I'll start. Anyone can add in:

Nuclear Fire Control Program

You all can on take explaining it and improving upon it. I realized after I wrote it that it wasn't for launch control, but for door open/close control. No matter. Just modify it as necessary.

Edited by surferpup, 03 February 2014 - 04:19 AM.


#7 surferpup

  • Members
  • 286 posts
  • LocationUnited States

Posted 03 February 2014 - 05:11 AM

Here is the same program, modified as a door control program, precisely as originally specified.

Posted Image


Door Control Program

Edit:
  • Updated some variable names.
  • Changed event variable to a local variable due to ingie's whining (he was correct).
  • Posted Screenshot of my test setup.
  • Corrected typos
  • Used math.floor function to effectively center label on button

Edited by surferpup, 03 February 2014 - 12:49 PM.


#8 awsmazinggenius

  • Members
  • 930 posts
  • LocationCanada

Posted 03 February 2014 - 09:28 AM

Thanks for writing that surferpup, let's see if it fits his needs.

EDIT: Damn ninja :ph34r:

Edited by awsmazinggenius, 03 February 2014 - 07:47 PM.


#9 ingie

  • Members
  • 69 posts
  • LocationMannin

Posted 03 February 2014 - 11:23 AM

Quote

Anyone can add in:

challenge accepted ;)

1. well done for taking up the banner/ putting your head above the parapet

which leads me onto:

2. ewwwww, you used global variables within in a function..... :o ... i feel sullied and unusual :)

[ but you're excused, due to 1. :) ]

#10 surferpup

  • Members
  • 286 posts
  • LocationUnited States

Posted 03 February 2014 - 12:22 PM

View Postingie, on 03 February 2014 - 11:23 AM, said:

2. ewwwww, you used global variables within in a function..... :o ... i feel sullied and unusual :)

Other than the event variable (my bad), there are no globals that I saw. But I might have missed one. Go take a shower, you will feel much better.

Please feel free to expand/explain/improve. After all, this is supposed to be a learning lesson for our friend weareborg2913 (although for all I know he hasn't seen any of these posts).

Edited by surferpup, 03 February 2014 - 12:28 PM.


#11 ingie

  • Members
  • 69 posts
  • LocationMannin

Posted 03 February 2014 - 12:29 PM

don't worry, i was only been jocular, not dismissing your code - sorry if you took my tone wrongly

there's several others tho... monitor/buttonheight/buttonwidth - but i'm purely being purist for "comedy" effect ... i did hint that my pedantry was irrelevant in the circumstances :)

#12 surferpup

  • Members
  • 286 posts
  • LocationUnited States

Posted 03 February 2014 - 12:32 PM

View Postawsmazinggenius, on 03 February 2014 - 09:28 AM, said:

Thanks for writing that surferpup, let's see if it fits his needs.

You are welcome. Care to use it as the basis for a tutorial? This question seems to come up a lot. I tried to incorporate a few good practices in making it (use of tables for buttons, self-documenting code, event loop with both monitor touch and key events tested, local variables -- thanks ingie -- use of functions, organized code, checking redstone states, allow for both toggle and pulse). All elements of a good door control with touch button tutorial are here.

#13 surferpup

  • Members
  • 286 posts
  • LocationUnited States

Posted 03 February 2014 - 12:37 PM

View Postingie, on 03 February 2014 - 12:29 PM, said:

don't worry, i was only been jocular, not dismissing your code - sorry if you took my tone wrongly

there's several others tho... monitor/buttonheight/buttonwidth - but i'm purely being purist for "comedy" effect ... i did hint that my pedantry was irrelevant in the circumstances :)

I took you at your tone -- that's why I said you were whining in my correction :P

For the record, the table buttons is local as are all the keys defined thereunder. Also, the variables monitor, buttonHeight and buttonWidth are declared local. You must have misread the code (shocking, right?). You are absolutely correct in pointing out that it is best practice to use local variables unless a global is absolutely essential. The only global I found was event, and I changed it. Good catch.

#14 ingie

  • Members
  • 69 posts
  • LocationMannin

Posted 03 February 2014 - 12:45 PM

View Postsurferpup, on 03 February 2014 - 12:32 PM, said:

You are welcome. Care to use it as the basis for a tutorial? This question seems to come up a lot. I tried to incorporate a few good practices in making it (use of tables for buttons, self-documenting code, event loop with both monitor touch and key events tested, local variables -- thanks ingie -- use of functions, organized code, checking redstone states, allow for both toggle and pulse). All elements of a good door control with touch button tutorial are here.

i'd agree - it's basically what is called a "design pattern" isn't it... or several nice design patterns in one bit of code...
i was looking at some of the tutorials last night and thought how although there's a lot of excellent stuff on how to solve problems and how to use libraries and devices, there's nothing specific on patterns...

the reason i think about this is that i see CC as being as much an educational tool for the "Why Johnny Can't Code" argument, as anything else. So outlining principles of general coding style and pattern is as important as "how to do x" type tutorials. This way when today's teenage CC scripters go out to work, if they choose computing, then they have good discipline in how to recognise the same thing when it crops up again, by its pattern rather than its specific need.

if you get me :)

View Postsurferpup, on 03 February 2014 - 12:37 PM, said:

I took you at your tone -- that's why I said you were whining in my correction :P

good good, i just wanted to make sure :)

View Postsurferpup, on 03 February 2014 - 12:37 PM, said:

For the record, the table buttons is local as are all the keys defined thereunder. Also, the variables monitor, buttonHeight and buttonWidth are declared local. You must have misread the code (shocking, right?). You are absolutely correct in pointing out that it is best practice to use local variables unless a global is absolutely essential. The only global I found was event, and I changed it. Good catch.

my bad in my definition, by global, i mean variables scoped outside the function... i.e. not local to the function - which is bad-ish practice for later abstraction. I always pass such variables in as params even if they are local to the main program, as that way it's simpler to extract the functions to a separate API later if needed, without any changes to the method.

Edited by ingie, 03 February 2014 - 12:46 PM.


#15 surferpup

  • Members
  • 286 posts
  • LocationUnited States

Posted 03 February 2014 - 12:45 PM

View Postingie, on 03 February 2014 - 12:42 PM, said:

if you get me :)

I get you. I actually wrote it because I have seen so many of these "Can you help me write a quick touch screen button program?" questions. So, instead of just cranking out a hack, I wanted to show that it is a relatively straightforward program that can be extended if you use good coding practices. Again -- basis for a good tutorial...

#16 ingie

  • Members
  • 69 posts
  • LocationMannin

Posted 03 February 2014 - 01:05 PM

View Postsurferpup, on 03 February 2014 - 12:45 PM, said:

I get you. I actually wrote it because I have seen so many of these "Can you help me write a quick touch screen button program?" questions. So, instead of just cranking out a hack, I wanted to show that it is a relatively straightforward program that can be extended if you use good coding practices. Again -- basis for a good tutorial...

aye, totally - i saw your button table and thought: nicely done. i have a similar table for my door proximity opening system - where additionally one of the values of a "door" item is the function by which the trigger is activated - it's things like this that make me love lua for its first-class functional style ability [ i link there not to patronise, obviously, but for others learning more about deeper principles of coding ]

p.s. sorry, i'd edited my previous post whilst you were typing that to explain what i meant by global variables - i don't like editing after a subsequent post as it can change context... but just so you know :)

#17 surferpup

  • Members
  • 286 posts
  • LocationUnited States

Posted 03 February 2014 - 01:16 PM

View Postingie, on 03 February 2014 - 12:45 PM, said:

my bad in my definition, by global, i mean variables scoped outside the function... i.e. not local to the function - which is bad-ish practice for later abstraction. I always pass such variables in as params even if they are local to the main program, as that way it's simpler to extract the functions to a separate API later if needed, without any changes to the method.

There is a fine line between Object-Oriented (OOP) principles and functional programming. Both are possible in Lua, and yes, generally speaking abstraction is good. This code is primarily functional. In the code, I have essentially only two true "abstract" functions -- drawButton and allRedstoneOff (which doesn't get called in this program). Other than that, the rest rely at the very least on the buttons table, which I do not pass in. I felt going further on abstraction would make the code less readable for the purposes of a tutorial-like code. As such, they are more like driver-functions.

View Postingie, on 03 February 2014 - 01:05 PM, said:

I have a similar table for my door proximity opening system - where additionally one of the values of a "door" item is the function by which the trigger is activated - it's things like this that make me love lua for its first-class functional style ability [ i link there not to patronise, obviously, but for others learning more about deeper principles of coding ]

Nice link to the wikipedia article on first-class functions. Did you miss a link to your code demonstrating this?

As for the rest - we're on the same page. No worries :)

#18 ingie

  • Members
  • 69 posts
  • LocationMannin

Posted 03 February 2014 - 01:21 PM

aye, again - totally understood within the realms of the thread :) i was just explaining where my pedantry arose from, not criticising your answer - i totally agree with the "do it simply rather than cleverly" in tutorials [ see my similar response to originalbit re: my turtle/AE extracting util in the programs section ]

View Postsurferpup, on 03 February 2014 - 01:16 PM, said:

Did you miss a link to your code demonstrating this?

no, but see my PM ;) as i say there, i didn't want to change the thrust of this thread into a general theory debate [ any more than i have already ;) ] when it should be about the original Q and your answer/usage of it.

#19 awsmazinggenius

  • Members
  • 930 posts
  • LocationCanada

Posted 03 February 2014 - 07:49 PM

I think I'll just say I've seen 3 similar topics including this, and surferpup send me and ingie a template for a tutorial. (Just so others know it is happening.)





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users