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

Led Counting Display

Started by tehdetox, 07 February 2012 - 02:07 AM
tehdetox #1
Posted 07 February 2012 - 03:07 AM
LED COUNTING DISPLAY


Big thanks to Fuzzypurp & Espen for helping me with my code.

The most advanced thing I've done to date so as novice as it may seem, I'm proud :P/>/>




images (before making it look nice for the video):

Spoiler



[left]The (long winded) code:[/left]
[left]
Spoiler


print("Counter v1.0 Activated")

--1
mix = colors.combine(colors.orange, colors.green)
redstone.setBundledOutput("back", mix, true)
sleep(4)

--2
redstone.setBundledOutput("back", 0)
mix = colors.combine(colors.pink, colors.orange, colors.brown, colors.red, colors.white)
redstone.setBundledOutput("back", mix, true)
sleep(4)

--3
redstone.setBundledOutput("back", 0)
mix = colors.combine(colors.pink, colors.orange, colors.brown, colors.green, colors.white)
redstone.setBundledOutput("back", mix, true)
sleep(4)

--4
redstone.setBundledOutput("back", 0)
mix = colors.combine(colors.purple, colors.brown, colors.orange, colors.green)
redstone.setBundledOutput("back", mix, true)
sleep(4)

--5
redstone.setBundledOutput("back", 0)
mix = colors.combine(colors.pink, colors.purple, colors.brown, colors.green, colors.white)
redstone.setBundledOutput("back", mix, true)
sleep(4)

--6
redstone.setBundledOutput("back", 0)
mix = colors.combine(colors.purple, colors.brown, colors.pink, colors.green, colors.red, colors.white)
redstone.setBundledOutput("back", mix, true)
sleep(4)

--7
redstone.setBundledOutput("back", 0)
mix = colors.combine(colors.pink, colors.orange, colors.green)
redstone.setBundledOutput("back", mix, true)
sleep(4)

--8
redstone.setBundledOutput("back", 0)
mix = colors.combine(colors.purple, colors.brown, colors.orange, colors.green, colors.pink, colors.red, colors.white)
redstone.setBundledOutput("back", mix, true)
sleep(4)

--9
redstone.setBundledOutput("back", 0)
mix = colors.combine(colors.purple, colors.brown, colors.orange, colors.green, colors.pink, colors.white)
redstone.setBundledOutput("back", mix, true)
sleep(4)

--0
redstone.setBundledOutput("back", 0)
mix = colors.combine(colors.purple, colors.orange, colors.green, colors.pink, colors.red, colors.white)
redstone.setBundledOutput("back", mix, true)
sleep(4)

--reset
redstone.setBundledOutput("back", 0)
[/left]
Espen #2
Posted 07 February 2012 - 11:03 AM
Hey, congratz to your functioning 7-segment display. :P/>/>

A few things about your code:
You're using 'true' within the setBundledOutput functions, but you don't need that. It only takes the number of the colors you provide and sets the output to that number.
To go into a little bit more detail:
All the colors represent certain bytes:
white = 1
orange = 2
magenta = 4
lightBlue = 8
yellow = 16
lime = 32
pink = 64
gray = 128
lightGray = 256
cyan = 512
purple = 1024
blue = 2048
brown = 4096
green = 8192
red = 16384
black = 32768
Also see the file: .minecraft\mods\ComputerCraft\lua\rom\apis\colors

So if you e.g. want to turn the wires orange and magenta on, then you can just add their numbers together (2 + 4 = 6) and set the output to that:
redstone.setBundledOutput( "back", 6 )

But if you use the names instead of the numbers, then you use colors.combine() and colors.subtract().
Here's some code showing what they essentially do:
magentaAndOrange = colors.combine( colors.magenta, colors.orange )
magentaAndOrange = 4 + 2

So why not simply use this?
magentaAndOrange = colors.magenta + colors.orange

Well, you can of course and I've seen some people who do it this way. But the numbers really represent byte values and the colors.combine() function therefore uses binary addition.
At the moment it makes no difference, but if the code would change in the future, then if you just added them together with simple addition you might have to change your code, whereas if you used colors.combine() you don't need to change any code.

Another thing is that you have parts in your code which repeat and are almost the same.
You can take advantage of loops to shorten repeating code to make your program much slimmer.
I've taken the freedom to do just that, I hope you don't mind, just want to help. :P/>/>

Here goes: http://pastebin.com/yqgM4d43
tehdetox #3
Posted 07 February 2012 - 11:44 AM
Hey espen, thanks for your feedback :P/>/>

Once i had the first few numbers it was litterally matter of copy and pasting each block and changing the colours in the combine() lol

You mentioned before about using their byte values but i was never aware that you could add them up and use the result to display specific colours :D/>/>

Thanks for the code suggestion, i dont think i would have ever thought of doing it that way, nor would i ever have thought it possible lol

I was also thinking about using a case switch to input a number and get it to output on the display. This is something i will look at when i get in.

(p.s forgive any spelling mistakes lol im on my phone :P/>/>)
Espen #4
Posted 07 February 2012 - 02:19 PM
Btw. I just noticed something with the code I posted earlier.

I use a for-loop with a hardcoded range, i.e. from 1 to 10.
But to make it more dynamic and easier to adjust it might be better to use:
for i = 1, #mix do
The '#' character is used if you want to get the length of a table or string, etc.
That means '#mix' returns the size of your 'mix' table.

This way if you add or remove entries from the 'mix' table (and therefore grow or shrink its size), then you don't need to always adjust the for-loop numbers.
By using '#mix' as the upper limit. It does this automatically for you.
Just thought I'd share this in case you didn't know yet.