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

[SOLVED] 4 - 7 Segment Displays Programming Problem

Started by Haidy777, 15 March 2012 - 02:37 PM
Haidy777 #1
Posted 15 March 2012 - 03:37 PM
Hi @all,

yesterday I build some 7 Segment Displays (total 4), now i had a problem, with controlling them.
I got to Bundled Buses out of the Computer to the segments, but I am not the lua Pro :D/>/>
Maybe somebody of you can help me.
I need a Program where I can enter a 4 Digit Number an the Computer will make the Segments light.
I thought a bit about that and got these code lines, who tells me which colors I must turn on to display the entered number.
a0 to b9 are on side right, c0 to d9 are on side top
(a0 stands vor segment a, number 0)

a0  = colors.combine(colors.white, colors.black, colors.gray, colors.blue, colors.magenta, colors.lightBlue)--100,101,102,104,105,106
a1  = colors.combine(colors.black, colors.magenta)--101,105
a2  = colors.combine(colors.white, colors.black, colors.purple, colors.blue, colors.lightBlue)--100,101,103,104,106
a3  = colors.combine(colors.white, colors.black, colors.purple, colors.magenta, colors.lightBlue)--100,101,103,105,106
a4  = colors.combine(colors.black, colors.gray, colors.purple, colors.magenta)--101,102,103,105
a5  = colors.combine(colors.white, colors.gray, colors.purple, colors.magenta, colors.lightBlue)--100,102,103,105,106
a6  = colors.combine(colors.white, colors.gray, colors.purple, colors.blue, colors.magenta, colors.lightBlue)--100,102,103,104,105,106
a7  = colors.combine(colors.white, colors.black, colors.magenta)--100,101,105
a8  = colors.combine(colors.white, colors.black, colors.gray, colors.purple, colors.blue, colors.magenta, colors.lightBlue)--100,101,102,103,104,105,106
a9  = colors.combine(colors.white, colors.black, colors.gray, colors.magenta, colors.lightBlue, colors.purple)--100,101,102,103,105,106

b0  = colors.combine(colors.yellow, colors.orange, colors.green, colors.red, colors.pink, colors.lime)--107,108,109,111,112,113
b1  = colors.combine(colors.orange, colors.red)--108,111
b2  = colors.combine(colors.yellow, colors.green, colors.cyan, colors.pink, colors.lime)--107,109,110,112,113
b3  = colors.combine(colors.yellow, colors.orange, colors.cyan, colors.red, colors.lime)--107,108,110,111,113
b4  = colors.combine(colors.orange, colors.green, colos.cyan, colors.red)--108,109,110,111
b5  = colors.combine(colors.yellow, colors.green, colors.cyan, colors.red, colors.lime)--107,109,110,111,113
b6  = colors.combine(colors.yellow, colors.green, colors.cyan, colors.red, colors.pink, colors.lime)--107,109,110,111,112,113
b7  = colors.combine(colors.yellow, colors.orange, colors.red)--107,108,111
b8  = colors.combine(colors.yellow, colors.orange, colors.green, colors.cyan, colors.red, colors.pink, colors.lime)--107,108,109,110,111,112,113
b9  = colors.combine(colors.yellow, colors.orange, colors.green, colors.cyan, colors.red, colors.lime)--107,108,109,110,111,113

c0  = colors.combine(colors.lightGray, colors.white, colors.black, colors.purple, colors.blue,colors.magenta)--114,115,116,118,119,120
c1  = colors.combine(colors.white, colors.purple)--115,118
c2  = colors.combine(colors.lightGray, colors.white, colors.gray, colors.blue, colors.magenta)--114,115,117,119,120
c3  = colors.combine(colors.lightGray, colors.white, colors.gray, colors.purple, colors.magenta)--114,115,117,118,120
c4  = colors.combine(colors.white, colors.black, colors.gray, colors.purple)--115,116,117,118
c5  = colors.combine(colors.lightGray, colors.black, colors.gray, colors.purple, colors.magenta)--114,116,117,118,120
c6  = colors.combine(colors.lightGray, colors.black, colors.gray, colors.purple, colors.blue, magenta)--114,116,117,118,119,120
c7  = colors.combine(colors.lightGray, colors.white, colors.purple,)--114,115,118
c8  = colors.combine(colors.lightGray, colors.white, colors.black, colors.gray, colors.purple, colors.blue, colors.magenta)--114,115,116,117,118,119,120
c9  = colors.combine(colors.lightGray, colors.white, colors.black, colors.gray, colors.purple, colors.magenta)--114,115,116,117,118,120

d0  = colors.combine(colors.lightBlue, colors.yellow, colors.orange, colors.cyan, colors.red, colors.pink)--121,122,123,125,126,127
d1  = colors.combine(colors.yellow, colors.cyan)--122,125
d2  = colors.combine(colors.lightBlue, colors.yellow, colors.green, colors.red, colors.pink)--121,122,124,126,127
d3  = colors.combine(colors.lightBlue, colors.yellow, colors.green, colors.cyan, colors.pink)--121,122,124,125,127
d4  = colors.combine(colors.yellow, colors.orange, colors.green, colors.cyan)--122,123,124,125
d5  = colors.combine(colors.lightBlue, colors.orange, colors.green, colors.cyan, colors.pink)--121,123,124,125,127
d6  = colors.combine(colors.lightBlue, colors.orange, colors.green, colors.cyan, colors.red, colors.pink)--121,123,124,125,126,127
d7  = colors.combine(colors.lightBlue, colors.yellow, colors.cyan)--121,122,125
d8  = colors.combine(colors.lightBlue, colors.yellow, colors.orange, colors.green, colors.cyan, colors.red, colors.pink)--121,122,123,124,125,126,127
d9  = colors.combine(colors.lightBlue, colors.yellow, colors.orange, colors.green, colors.cyan, colors.pink)--121,122,123,124,125,127

Now I want a program where i can enter 1234 in the Computer and the Computer will switch it so that the four 7 segment Displays display 1234.
(at Programm startup it should set all outputs to off)

I hope you understand me, and sorry for my bad english,
I'm Austrian.

Thanks in advance!
Haidy777

Edit: I use CC 1.3 on MC 1.1
Edited on 16 March 2012 - 06:30 PM
TheOnlyAnti #2
Posted 15 March 2012 - 06:18 PM
Well for the most part, if you have all the digits already together and colors required for each number than your almost done.


local LeftDigits = "right" -- set a0-b9 wires as the left 2 digits of display
local RightDigits = "top" -- set c0-d9 wires as the right 2 digits of display
local Number = 0
local WireTemp1 = 0
local WireTemp2 = 0
local exit = 0

while exit == 0 do
shell.run("clear")
write(" Enter a 4 digit number: ") --prompt for number
Number = tonumber(read())
--"1234"
WireTemp1 = {d,Number[0]} --assign "4"
WireTemp2 = {c,Number[1]} --assign "3"
rs.setBundledOutput(RightDigits,color.combine(WireTemp1,WireTemp2)) --output signals to display

WireTemp1 = {b,Number[2]}  --assign "2"
WireTemp2 = {a,Number[3]} --assign "1"
rs.setBundledOutput(LeftDigits,color.combine(WireTemp1,WireTemp2)) --output signals to display

print("")
print("  Again? Y/N ") --prompt to enter more numbers
input = read()
if input == "N" or input == "n" then
exit = 1
else
exit = 0
end
end

In theory this should work, im still new to lua so no guarantees. It should at least get you going in the right direction
Espen #3
Posted 15 March 2012 - 10:08 PM
Hey Haidy777,
I made a fully functional version, you'll only have to setup the cables like so:


Other views:
Spoiler

And here's the code for the control program:
Spoiler

local sCableA = "right"  -- Side of bundled cable output for displays 1 and 2.
local sCableB = "left"  -- Side of bundled cable output for displays 3 and 4.
local tDisplay = {}  -- Table for storing all displays.
-- Prepare space for 4 displays.
for i = 1, 4 do
  tDisplay[i] = {}
end

-- Define digits for displays 1 and 2 (these run on bundled cable 'sCableA'):
tDisplay[1][0] = colors.combine(colors.white, colors.black, colors.gray, colors.blue, colors.magenta, colors.lightBlue)
tDisplay[1][1] = colors.combine(colors.black, colors.magenta)
tDisplay[1][2] = colors.combine(colors.white, colors.black, colors.purple, colors.blue, colors.lightBlue)
tDisplay[1][3] = colors.combine(colors.white, colors.black, colors.purple, colors.magenta, colors.lightBlue)
tDisplay[1][4] = colors.combine(colors.black, colors.gray, colors.purple, colors.magenta)
tDisplay[1][5] = colors.combine(colors.white, colors.gray, colors.purple, colors.magenta, colors.lightBlue)
tDisplay[1][6] = colors.combine(colors.white, colors.gray, colors.purple, colors.blue, colors.magenta, colors.lightBlue)
tDisplay[1][7] = colors.combine(colors.white, colors.black, colors.magenta)
tDisplay[1][8] = colors.combine(colors.white, colors.black, colors.gray, colors.purple, colors.blue, colors.magenta, colors.lightBlue)
tDisplay[1][9] = colors.combine(colors.white, colors.black, colors.gray, colors.magenta, colors.lightBlue, colors.purple)

tDisplay[2][0] = colors.combine(colors.yellow, colors.orange, colors.green, colors.red, colors.pink, colors.lime)
tDisplay[2][1] = colors.combine(colors.orange, colors.red)
tDisplay[2][2] = colors.combine(colors.yellow, colors.orange, colors.cyan, colors.pink, colors.lime)
tDisplay[2][3] = colors.combine(colors.yellow, colors.orange, colors.cyan, colors.red, colors.lime)
tDisplay[2][4] = colors.combine(colors.orange, colors.green, colors.cyan, colors.red)
tDisplay[2][5] = colors.combine(colors.yellow, colors.green, colors.cyan, colors.red, colors.lime)
tDisplay[2][6] = colors.combine(colors.yellow, colors.green, colors.cyan, colors.red, colors.pink, colors.lime)
tDisplay[2][7] = colors.combine(colors.yellow, colors.orange, colors.red)
tDisplay[2][8] = colors.combine(colors.yellow, colors.orange, colors.green, colors.cyan, colors.red, colors.pink, colors.lime)
tDisplay[2][9] = colors.combine(colors.yellow, colors.orange, colors.green, colors.cyan, colors.red, colors.lime)

-- Define digits for displays 3 and 4 (these run on bundled cable 'sCableB'):
tDisplay[3][0] = colors.combine(colors.lightGray, colors.white, colors.black, colors.purple, colors.blue,colors.magenta)
tDisplay[3][1] = colors.combine(colors.white, colors.purple)
tDisplay[3][2] = colors.combine(colors.lightGray, colors.white, colors.gray, colors.blue, colors.magenta)
tDisplay[3][3] = colors.combine(colors.lightGray, colors.white, colors.gray, colors.purple, colors.magenta)
tDisplay[3][4] = colors.combine(colors.white, colors.black, colors.gray, colors.purple)
tDisplay[3][5] = colors.combine(colors.lightGray, colors.black, colors.gray, colors.purple, colors.magenta)
tDisplay[3][6] = colors.combine(colors.lightGray, colors.black, colors.gray, colors.purple, colors.blue, colors.magenta)
tDisplay[3][7] = colors.combine(colors.lightGray, colors.white, colors.purple)
tDisplay[3][8] = colors.combine(colors.lightGray, colors.white, colors.black, colors.gray, colors.purple, colors.blue, colors.magenta)
tDisplay[3][9] = colors.combine(colors.lightGray, colors.white, colors.black, colors.gray, colors.purple, colors.magenta)

tDisplay[4][0] = colors.combine(colors.lightBlue, colors.yellow, colors.orange, colors.cyan, colors.red, colors.pink)
tDisplay[4][1] = colors.combine(colors.yellow, colors.cyan)
tDisplay[4][2] = colors.combine(colors.lightBlue, colors.yellow, colors.green, colors.red, colors.pink)
tDisplay[4][3] = colors.combine(colors.lightBlue, colors.yellow, colors.green, colors.cyan, colors.pink)
tDisplay[4][4] = colors.combine(colors.yellow, colors.orange, colors.green, colors.cyan)
tDisplay[4][5] = colors.combine(colors.lightBlue, colors.orange, colors.green, colors.cyan, colors.pink)
tDisplay[4][6] = colors.combine(colors.lightBlue, colors.orange, colors.green, colors.cyan, colors.red, colors.pink)
tDisplay[4][7] = colors.combine(colors.lightBlue, colors.yellow, colors.cyan)
tDisplay[4][8] = colors.combine(colors.lightBlue, colors.yellow, colors.orange, colors.green, colors.cyan, colors.red, colors.pink)
tDisplay[4][9] = colors.combine(colors.lightBlue, colors.yellow, colors.orange, colors.green, colors.cyan, colors.pink)

local function setDisplays( input )
  local nEnteredDigit = {}
  if (input == nil) or (#input ~= #tDisplay) then return false end  -- Input was invalid.

  -- Check and convert input to single digits.
  for i = 1, #input do
    nEnteredDigit[i] = tonumber( string.sub( input, i, i ) )
    if ( type(nEnteredDigit[i]) ~= "number" ) or ( nEnteredDigit[i] < 0 ) or ( nEnteredDigit[i] > 9 ) then return false end  -- Input was invalid.
  end

  -- Set bundled cable outputs for displays 1 and 2.
  rs.setBundledOutput( sCableA, colors.combine( tDisplay[1][ nEnteredDigit[1] ], tDisplay[2][ nEnteredDigit[2] ] ) )
  -- Set bundled cable outputs for displays 3 and 4.
  rs.setBundledOutput( sCableB, colors.combine( tDisplay[3][ nEnteredDigit[3] ], tDisplay[4][ nEnteredDigit[4] ] ) )
  return true
end


--[[ === MAIN LOOP === ]]
local nTermWidth, nTermHeight = term.getSize()
while true do
  term.clear()
  term.setCursorPos(1, nTermHeight)
  write( "Enter "exit" to quit the program.")
  term.setCursorPos(1, 3)
  write( "Enter 4 digits: " )
  local input = read()

  if string.lower( input ) == "exit" then
    term.clear()
    term.setCursorPos(1, 1)
    break
  end

  if not setDisplays( input ) then
    print("nWrong input!nOnly 4 digits please.")
    sleep(1.5)
  end
end
Preview:
Spoiler
Haidy777 #4
Posted 16 March 2012 - 08:27 AM
Thanks for the Replys.
@TheOnlyAnti: I Used yours, but i got an error: "controll.lua:27: attempt to index ? (a nil value)"
Maybe you know whats wrong?
The Complete Code is in the Spoiler:
Spoiler

local bundlea	 = "right" --100 bis 113
local bundleb	 = "top"   --114 bis 127
local Number	  = 0
local WireTemp1	  = 0
local WireTemp2	  = 0
local ex		   = 0

a0  = colors.combine(colors.white, colors.black, colors.gray, colors.blue, colors.magenta, colors.lightBlue)--100,101,102,104,105,106
a1  = colors.combine(colors.black, colors.magenta)--101,105
a2  = colors.combine(colors.white, colors.black, colors.purple, colors.blue, colors.lightBlue)--100,101,103,104,106
a3  = colors.combine(colors.white, colors.black, colors.purple, colors.magenta, colors.lightBlue)--100,101,103,105,106
a4  = colors.combine(colors.black, colors.gray, colors.purple, colors.magenta)--101,102,103,105
a5  = colors.combine(colors.white, colors.gray, colors.purple, colors.magenta, colors.lightBlue)--100,102,103,105,106
a6  = colors.combine(colors.white, colors.gray, colors.purple, colors.blue, colors.magenta, colors.lightBlue)--100,102,103,104,105,106
a7  = colors.combine(colors.white, colors.black, colors.magenta)--100,101,105
a8  = colors.combine(colors.white, colors.black, colors.gray, colors.purple, colors.blue, colors.magenta, colors.lightBlue)--100,101,102,103,104,105,106
a9  = colors.combine(colors.white, colors.black, colors.gray, colors.magenta, colors.lightBlue, colors.purple)--100,101,102,103,105,106

b0  = colors.combine(colors.yellow, colors.orange, colors.green, colors.red, colors.pink, colors.lime)--107,108,109,111,112,113
b1  = colors.combine(colors.orange, colors.red)--108,111
b2  = colors.combine(colors.yellow, colors.green, colors.cyan, colors.pink, colors.lime)--107,109,110,112,113
b3  = colors.combine(colors.yellow, colors.orange, colors.cyan, colors.red, colors.lime)--107,108,110,111,113
b4  = colors.combine(colors.orange, colors.green, colos.cyan, colors.red)--108,109,110,111
b5  = colors.combine(colors.yellow, colors.green, colors.cyan, colors.red, colors.lime)--107,109,110,111,113
b6  = colors.combine(colors.yellow, colors.green, colors.cyan, colors.red, colors.pink, colors.lime)--107,109,110,111,112,113
b7  = colors.combine(colors.yellow, colors.orange, colors.red)--107,108,111
b8  = colors.combine(colors.yellow, colors.orange, colors.green, colors.cyan, colors.red, colors.pink, colors.lime)--107,108,109,110,111,112,113
b9  = colors.combine(colors.yellow, colors.orange, colors.green, colors.cyan, colors.red, colors.lime)--107,108,109,110,111,113

c0  = colors.combine(colors.lightGray, colors.white, colors.black, colors.purple, colors.blue,colors.magenta)--114,115,116,118,119,120
c1  = colors.combine(colors.white, colors.purple)--115,118
c2  = colors.combine(colors.lightGray, colors.white, colors.gray, colors.blue, colors.magenta)--114,115,117,119,120
c3  = colors.combine(colors.lightGray, colors.white, colors.gray, colors.purple, colors.magenta)--114,115,117,118,120
c4  = colors.combine(colors.white, colors.black, colors.gray, colors.purple)--115,116,117,118
c5  = colors.combine(colors.lightGray, colors.black, colors.gray, colors.purple, colors.magenta)--114,116,117,118,120
c6  = colors.combine(colors.lightGray, colors.black, colors.gray, colors.purple, colors.blue, magenta)--114,116,117,118,119,120
c7  = colors.combine(colors.lightGray, colors.white, colors.purple)--114,115,118
c8  = colors.combine(colors.lightGray, colors.white, colors.black, colors.gray, colors.purple, colors.blue, colors.magenta)--114,115,116,117,118,119,120
c9  = colors.combine(colors.lightGray, colors.white, colors.black, colors.gray, colors.purple, colors.magenta)--114,115,116,117,118,120

d0  = colors.combine(colors.lightBlue, colors.yellow, colors.orange, colors.cyan, colors.red, colors.pink)--121,122,123,125,126,127
d1  = colors.combine(colors.yellow, colors.cyan)--122,125
d2  = colors.combine(colors.lightBlue, colors.yellow, colors.green, colors.red, colors.pink)--121,122,124,126,127
d3  = colors.combine(colors.lightBlue, colors.yellow, colors.green, colors.cyan, colors.pink)--121,122,124,125,127
d4  = colors.combine(colors.yellow, colors.orange, colors.green, colors.cyan)--122,123,124,125
d5  = colors.combine(colors.lightBlue, colors.orange, colors.green, colors.cyan, colors.pink)--121,123,124,125,127
d6  = colors.combine(colors.lightBlue, colors.orange, colors.green, colors.cyan, colors.red, colors.pink)--121,123,124,125,126,127
d7  = colors.combine(colors.lightBlue, colors.yellow, colors.cyan)--121,122,125
d8  = colors.combine(colors.lightBlue, colors.yellow, colors.orange, colors.green, colors.cyan, colors.red, colors.pink)--121,122,123,124,125,126,127
d9  = colors.combine(colors.lightBlue, colors.yellow, colors.orange, colors.green, colors.cyan, colors.pink)--121,122,123,124,125,127

while ex == 0 do
	shell.run("clear")
	write(" Enter a 4 digit number: ") --promt for numbers
	Number = tonumber(io.read())
	--"1234"
	
	WireTemp1 = {a, Number[3]} --assign "1"
	WireTemp2 = {b, Number[2]} --assign "2"
	rs.setBundledOutput(bundlea, colors.combine(WireTemp1, WireTemp2)) --output signals to display
	
	WireTemp1 = {c, Number[1]} --assign "3"
	WireTemp2 = {d, Number[0]} --assign "4"
	rs.setBundledOutput(bundleb, colors.combine(WireTemp1, WireTemp2)) --output signals to display
	
	print("")
	print(" Again? Y/N ") --promt to enter more numbrs
	input = io.read()
	if input == "N" or input == "n" then
		ex = 1
	else
		ex = 0
	end
end
Espen #5
Posted 16 March 2012 - 11:05 AM
In b4 there is this…
colos.cyan
… which should be this…
colors.cyan

Also, not to bash TheOnlyAnti, but his code won't work. There are two important reasons for that:
  • You can't index a number, only tables. So this…
    Number[1]
    … won't work.
  • colors.combine() takes numbers, not tables, so this…
    WireTemp1 = {a, Number[3]}
    WireTemp2 = {b, Number[2]}
    colors.combine(WireTemp1, WireTemp2)
    
    … won't work.
Haidy777 #6
Posted 16 March 2012 - 12:09 PM
In b4 there is this…
colos.cyan
… which should be this…
colors.cyan

Also, not to bash TheOnlyAnti, but his code won't work. There are two important reasons for that:
  • You can't index a number, only tables. So this…
    Number[1]
    … won't work.
  • colors.combine() takes numbers, not tables, so this…
    WireTemp1 = {a, Number[3]}
    WireTemp2 = {b, Number[2]}
    colors.combine(WireTemp1, WireTemp2)
    
    … won't work.
Thanks for the Tip in b4 :D/>/>

And how can I get the Numbers working?
Espen #7
Posted 16 March 2012 - 12:15 PM
Thanks for the Tip in b4 :D/>/>

And how can I get the Numbers working?
Take a look at my code, I tested it and it's fully functional.
The most important part is the function setDisplays( input ).
TheOnlyAnti #8
Posted 16 March 2012 - 05:49 PM
Yea, i was just speculating on what could work. Im no pro in the matter, as ive only been using lua for a week or so. Mainly program in C++, so not use to what works with lua yet
Haidy777 #9
Posted 16 March 2012 - 06:51 PM
Thanks for the Tip in b4 :D/>/>

And how can I get the Numbers working?
Take a look at my code, I tested it and it's fully functional.
The most important part is the function setDisplays( input ).

Now I used yours. It does what it shoulds :)/>/>. Thanks
Espen #10
Posted 17 March 2012 - 01:52 AM
@Haidy777: Glad it's working for you now. :)/>/>
@TheOnlyAnti: Hey no problem, I had the same problem in the beginning, trying to index strings etc. like I was used to with Java and some others. :D/>/>