Photocell + 8×8 LED Matrix
This is an example of using 2 photocells and a variable resistor as analog inputs to control 8×8 LED Matrix with PIC18F452, UDN2981, and ULN2803. The input values of the two photocells are compared. When the values are the same, LED lights up randomly within the entire board. Depending on how much darker a photocell is compared to the other, LED lights up within the range that is more focused towards a corner. The variable resistor controls how fast the LEDs blink. This is how the board was set up:
The following is the code:
DEFINE LOADER_USED 1
DEFINE OSC 20
INCLUDE “modedefs.bas”
DEFINE ADC_BITS 10
DEFINE ADC_CLOCK 3
DEFINE ADC_SAMPLEUS 20
ADCON1 = %10000010
trisb =%00000000
trisd =%00000000
trisa =%11111111
trisc =%10000000
an0 var word
an1 var word
an2 var word
an0b var byte
an1b var byte
an2b var byte
portb = 0
portd = 0
rand_max var byte
rand_min var byte
random_word var word
randomdig var byte
main:
GOSUB getadc
IF an1b < an2b THEN
rand_max=an1b
rand_min=0
ENDIF
IF an1b > an2b THEN
rand_max=7
rand_min=7-an2b
ENDIF
IF an2b = an1b THEN
rand_max=7
rand_min=0
ENDIF
GOSUB generate_Random
portb = 1<<randomdig
GOSUB generate_Random
portd = 1<<randomdig
PAUSE 10*an0b
gosub bd_off
PAUSE 1*an0b
GOTO main
bd_off:
portb=0 : portd=0
RETURN
getadc:
ADCIN 0,an0
ADCIN 1,an1
ADCIN 2,an2
an0b = (an0*8)>>10
an1b = (an1*8)>>10
an2b = (an2*8)>>10
RETURN
generate_random:
RANDOM random_word
randomdig = random_word DIG 1
IF randomdig > rand_max THEN GOTO generate_random
IF randomdig < rand_min THEN GOTO generate_random
RETURN
