[Lua] Noteblock Player
#1
Posted 05 September 2012 - 09:50 PM
#2
Posted 05 September 2012 - 10:09 PM
For starters, every wire in a bundled cable has a numerical value to it. White is 1, Orange 2, Magenta 4, and so on up the binary ladder. (I think that's the right order). And by combining these numbers, we can get different ones. Like white+orange is 3, magenta and orange is 5, etc. By the way, using 'help colors' should give you the list of colors, and in the correct order. Just, it helps to know binary counting.
So, if you want to play note 1, you can type in '1', and then pass the input to a bundled output, like so.
input = tonumber(read()) rs.setBundledOutput(side, input)
Now it's really just a matter of understanding how to make each wire activate. The simplest way is to make an array that is nothing but the colors.
myColors = {}
myColors[1] = 1
myColors[2] = 2
myColors[3] = 4
myColors[4] = 8
myColors[5] = 16
myColors[6] = 32
myColors[7] = 64
myColors[8] = 128
myColors[9] = 256
myColors[10] = 512
myColors[11] = 1024
myColors[12] = 2048
myColors[13] = 4096
myColors[14] = 8192
myColors[15] = 16384
myColors[16] = 32768
And then apply what we have already learned to the array.
rs.setBundledOutput(side, myColors[input])
And that should get you started, if nothing else presently.
#3
Posted 05 September 2012 - 10:32 PM
#4
Posted 05 September 2012 - 10:39 PM
#5
Posted 05 September 2012 - 10:42 PM
OmegaVest, on 05 September 2012 - 10:09 PM, said:
Now it's really just a matter of understanding how to make each wire activate. The simplest way is to make an array that is nothing but the colors.
myColors = {}
myColors[1] = 1
myColors[2] = 2
myColors[3] = 4
myColors[4] = 8
myColors[5] = 16
myColors[6] = 32
myColors[7] = 64
myColors[8] = 128
myColors[9] = 256
myColors[10] = 512
myColors[11] = 1024
myColors[12] = 2048
myColors[13] = 4096
myColors[14] = 8192
myColors[15] = 16384
myColors[16] = 32768
And then apply what we have already learned to the array.
rs.setBundledOutput(side, myColors[input])
And that should get you started, if nothing else presently.
Um, what about colors.(color)? Last time I checked, that worked just fine....
#6
Posted 05 September 2012 - 10:45 PM
NIN3, on 05 September 2012 - 10:42 PM, said:
OmegaVest, on 05 September 2012 - 10:09 PM, said:
Now it's really just a matter of understanding how to make each wire activate. The simplest way is to make an array that is nothing but the colors.
myColors = {}
myColors[1] = 1
myColors[2] = 2
myColors[3] = 4
myColors[4] = 8
myColors[5] = 16
myColors[6] = 32
myColors[7] = 64
myColors[8] = 128
myColors[9] = 256
myColors[10] = 512
myColors[11] = 1024
myColors[12] = 2048
myColors[13] = 4096
myColors[14] = 8192
myColors[15] = 16384
myColors[16] = 32768
And then apply what we have already learned to the array.
rs.setBundledOutput(side, myColors[input])
And that should get you started, if nothing else presently.
Um, what about colors.(color)? Last time I checked, that worked just fine....
#7
Posted 05 September 2012 - 10:49 PM
NIN3, on 05 September 2012 - 10:42 PM, said:
As well as colors[color].
I actually wrote a noteblock player a while ago that would read a list of outputs to induce. I had it play gangnam style but it didn't work out that well because of inaccurate timing.
However this would be much easier to do:
local tCommands = {
[keys.q] = 'white';
[keys.w] = 'red';
[keys.e] = 'blue';
--continue this with all of your colors, but in the order of how you would want your keys mapped in correspondence to the order of redstone outputs to the noteblocks.
}
while true do
local _, k = os.pullEvent('key')
if tCommands[k] then
rs.setBundledOutput('back', 0) --replacing 'back' with whatever side you want, of course.
rs.setBundledOutput('back', colors[tCommands[k]])
end
end
#8
Posted 05 September 2012 - 11:11 PM
#9
Posted 05 September 2012 - 11:22 PM
#10
Posted 05 September 2012 - 11:40 PM
#11
Posted 06 September 2012 - 12:02 AM
#12
Posted 06 September 2012 - 01:56 AM
TristanTroll, on 05 September 2012 - 11:11 PM, said:
TristanTroll, on 05 September 2012 - 11:40 PM, said:
Lua is an easy language, trust me. (I know Lua, VB, C#, bits of perl and bits of C++, and lua is by far the easiest to get to grips with).
There's not much point trying to use computercraft unless you're willing to learn a bit about how to program. Part of the whole reason the mod was created was so people had a reason to learn how to program (As well as make something current programmers could use in minecraft to help simplify things).
Programming as a concept isn't hard, the hard bit is knowing what processes you have to perform to get your program to do what you want it to. For example you might know that you want to get a noteblock to play when someone presses a button while using the computer, but there are so many different ways you could go about it, it's sometimes hard to clear your head enough to think how you would go about doing it.
Also, it's pretty much an unwritten rule around here that you should never expect to come to the pros section and expect someone to provide you with a full running program, you should always assume they are just going to tell you where you are going wrong or suggest ways of doing things or things you may have missed (like the existence of a function that would help),which is one of the reasons Cranium was annoyed (also the fact that he's put a lot of effort into learning how to program in lua and had to overcome a lot of obstacles. Any programmer worth their salt has been through this struggle and gets annoyed when beginners want to take short cuts or people expect the code monkeys to deliver any program they ask for on a plate).
regardless, here is some code that will output data to a bundled cable at the back of a computer so you can play it like a keyboard(providing noteblocks are attached):
local side = "back" -- variable holding the side to send the data
term.clear() -- clear screen
term.setCursorPos(1, 1) --reset cursor
while true do
local e, k = os.pullEvent("key")
--you can choose the keys you want to set corresponding code to by using the keys constant (keys.keyname)
--k represents the key pressed. Test what key it is by using a key constant.
--here I demonstrate
--the w key sending output to the red wire,
--the r key sending output to the blue wire and
--the e key sending output to the white wire
--these can be changed by changing the key constant, the colour and
--more options can be added by adding more "else if k == keys.keyname then" blocks
--this is not the most efficient method, but if you are a beginner you dont need to worry about efficiency,
--just understanding what is going on
if k == keys.w then
redstone.setBundledOutput(side, colours.red) -- sets output to red
elseif k == keys.r then
redstone.setBundledOutput(side, colours.blue) -- sets output to blue
elseif k == keys.e then
redstone.setBundledOutput(side, colours.white) -- sets output to white
end
sleep()
redstone.setBundledOutput(side, 0) --cuts all output
end
#13
Posted 06 September 2012 - 02:16 AM
#14
Posted 06 September 2012 - 02:38 AM
Kingdaro, on 06 September 2012 - 02:16 AM, said:
No it isn't brilliant programming but firstly, lua doesn't have a switch statement and secondly, it's no good bombarding a beginner with advanced efficient techniques if they aren't going to understand what it does. Showing things clearly is how people learn, not by showing them stuff that they need to understand a load of other junk first to understand.
Also I'd like to point out that quotes are much better than @ symbols, this isn't twitter or Java.
#15
Posted 06 September 2012 - 02:58 AM
Pharap, on 06 September 2012 - 02:38 AM, said:
Pharap, on 06 September 2012 - 02:38 AM, said:
#16
Posted 06 September 2012 - 03:14 AM
#17
Posted 06 September 2012 - 03:15 AM
Kingdaro, on 06 September 2012 - 02:58 AM, said:
Pharap, on 06 September 2012 - 02:38 AM, said:
Pharap, on 06 September 2012 - 02:38 AM, said:
I'm glad you understand why I did it though, I feel that a lot of experienced programmers start to forget what it was like when they were first starting out and tend to give people code that is wonderfully efficient but really hard for beginners, hence why I always try to explain code I give to people who are just starting out as well as making it require as least knowledge as possible. That way people learn whether they want to or not lol.
As for any mistakes I made, I put it up to tiredness (it's 04:14 where I am, UTC-0 is a late place to be right now)
#18
Posted 06 September 2012 - 03:25 AM
TristanTroll, on 06 September 2012 - 03:14 AM, said:
You're welcome. I mostly saw 'noteblock player' and skimmed the rest, since it's a commonly asked for program.
As long as you tune it correctly, it should be fine. I'm not sure how many wire colours there are, but hopefully enough to match the 24 notes that noteblocks can produce, if not, just rig it up so there's a separate bundled cable on each side of the computer.
I'm glad you understood it, I've had an arduous journey in programming to get to where I am now, and since I only started a year ago, I still remember my first few weeks learning to program, so when I hear that someone is still new, I look back and say 'how would I explain this to myself back then' and thus try to make my code as simple as possible. Also I have a 'functionality over all' attitude, which basically means I worry more about if code works than if it's efficient.
Regardless of my ramblings, I'm glad you understand now, hopefully you'll get your noteblock keyboard working and soon move onto bigger and better things
#19
Posted 06 September 2012 - 11:25 AM
#20
Posted 06 September 2012 - 12:36 PM
TristanTroll, on 06 September 2012 - 11:25 AM, said:
while true do playSong() -- replace this with your song playing code. sleep(3600) end
However if you wanted the song to sound at all like a real song, you would have a waiting period in the song that would offset the hour-long cycle. Another way to do this would be to make a while loop using the parallel API to play both the timer and the song at the same time.
local function songTimer() sleep(3600) end while true do parallel.waitForAll(songTimer, playSong) end
The parallel API waits for both the song to complete, and for the timer to cycle 3600 seconds. Hopefully this makes sense.
Pharap, on 06 September 2012 - 03:25 AM, said:
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users











