While waiting for a programmer to be delivered that may actually work with slightly modern hardware (modern hardware being a computer with no parallel or serial ports) I thought it may be a good idea to try and use up some of the leftover components from making the $50 robot to make a little 6-bit display (6 LEDs, each one representing 1 bit). As I'm fairly unexperienced in electronics (studied it in high-school, made some guitar FX pedals that didn't work and some audio cables) and programming (I think I have a half-finished C++ battleships program lying around somewhere) I thought I'd make sure what I'm wanting to do may be capable of doing something.
I've started to make a connector that would go from the 6 servo outputs (and ground) to a spare piece of perfboard, each out hooking up to a 330 ohm resistor and LED. Assuming this will work I'd like it to be able to display various things, such as the photoresistor readings, and comparisons of them. In the future I think it would be a good way to test some ideas.
Software wise my programming skills have gone rusty, but I've managed to do a basic prototype of the functions in python3k (probably backward compatable though). I guess I'll need to port it over to C, any help to do this would be super-appreciated, especially the outputting parts, and conversion to binary (last time I encountered binary conversion in C it was using bitwise logic operators, which I'm not exactly the best at using...or understanding even).
The way I've set it to work is so that the display functions return a 6 bit code, that code would then be interpreted in binary as: 1 = light on, 0 = light off
So here's the python code:
def strengthdisplay (data):
"""turn 8 bit data into a number between 0 and 6"""
data = int(data / 37)
led = 0
if data == 0: led = 0b000000
elif data == 1: led = 0b000001
elif data == 2: led = 0b000011
elif data == 3: led = 0b000111
elif data == 4: led = 0b001111
elif data == 5: led = 0b011111
else: led = 0b111111
def doublestrengthdisplay (data1, data2):
"""turn two lots of 8 bit data into 2 numbers between 0 and 3"""
data1 = int(data1/64)
data2 = int(data2/64)
led = 0
#process the first signal
if data1 == 0: led = 0b000000
elif data1 == 1: led = 0b000001
elif data1 == 2: led = 0b000011
else: led = 0b000111
#now append the second signal
if data2 == 0: led += 0b000000
elif data2 ==1: led += 0b100000
elif data2 ==2: led += 0b110000
else: led += 0b111000
def sigdifference (data1, data2):
"""compares data1 to data2 and plots the difference"""
#subtracting an 8 bit number from another 8 bit number can have a
#difference of 511, we need to create a number between -3 and 3
number = (int(data1 / 64) - int(data2 / 64))
led = 0
if number ==-3: led = 0b000001
elif number ==-2: led = 0b000010
elif number ==-1: led = 0b000100
elif number == 0: led = 0b000000
elif number == 1: led = 0b001000
elif number == 2: led = 0b010000
elif number == 3: led = 0b100000
def display (ledout):
"""switch on the lights"""
#create a binary listing of ledout
ledout = bin(ledout)
# something to remove the "0b" prefix should go here, this may just be python
#also it should make sure that all the '0's are there
#ledno = 0 #used to chose which led should be switched on
for x in (bin(5)):
if x > "0": print ("x") #portoff(ledno)
elif x < "1": print ("O")#proton(ledno)
#ledno += 1
#a bit of code here to give an example of the functions
for x in range(20): print (bin((strengthdisplay(random.randint(0, 255)))))
for x in range(20): print (bin(doublestrengthdisplay(random.randint(0,255),random.randint(0,255))))
for x in range(20): print (bin(sigdifference(random.randint(0,255),random.randint(0,255))))
Does anyone think this may work?
Is there a better design I could use for the code?