... but after some minutes and some $42, I had another bx24 working fine! So I could finally start working in something.
I decided to go on with the street light thing and integrate in one program last week's idea and the potentiometer version, so there would be two possibilities to run the same circuit.
So it finally ended as a street light that can both work automatically or manually, depending on the state of a switch. If automatic, the light and buzzer would go on as on the week3 project,
and in the manual the potentiometer would be used to determine 5 states of lighting and beeping.
It was quite easy to set up the two manual and automatic routines.
The difficulty was in make the chip being constantly cheking the state of the switch and at the same time running the routines properly, so at any time it would notice the state of the switch and change the mode.
That was hours of work, rewriting once and again the code.
And when it all should have been ok, the automatic routine started to get stopped at it's thenth step.
After debuging all possible and not understanding what was happening, I posted and e-mail on the p-comp list. Tom Igoe told me to do some stuff to make the code look better (I needed that, too) and try this way to fix it.
It didn't... so I kept on trying untill I finally fixed it getting rid of a function that didn't seem necessary.
Suddently it all was working just as I expected it to!
And after a while, Tom posted another e-mail telling that the problem probably had been that the bx24 was going under more than 10 levels, which happens to be it's limit.
That made sense! I knew the clue had to be this 10 but I didn't know any reason for it to be a problem.
Now I know: the bx24 can only go 10 levels deep in subroutines of if statements.
And another really helpful tip: the startAgain subroutine in my program made me be aware of every time the chip was being reset, and having sound on this message was a really good thing.
Finally, I added a loudspeaker in pin 17 and called freqout in the manual mode (thanks to Jared, Maya and Mina for the research group presentation). |
Option Explicit
dim potVar as integer
dim switchState as byte
dim autoContador as byte
Sub Main()
call startAgain()
autocontador = 0
do
call Check()
loop
end sub
'check if the swich is on or off and go to one mode or the other
sub Check()
switchState = getpin(8)
debug.print "swithcstate=" ; cStr(switchState)
if switchState = 0 then
debug.print "CALL manual"
call manual()
else
debug.print "CALL automatic"
call automatic()
end if
end sub
' declare as subroutines the 4 possible states of the traffic light
sub stateLight1()
call putpin(9,1)
call putpin(10,0)
call putpin(11,0)
call putpin(12,0)
debug.print "stateLight is 1"
call delay(0.5)
end sub
sub statelight2()
call putpin(9,1)
call putpin(10,0)
call putpin(11,0)
call putpin(12,1)
debug.print "stateLight is 2"
call delay(0.5)
end sub
sub statelight3()
call putpin(9,0)
call putpin(10,1)
call putpin(11,0)
call putpin(12,0)
debug.print "stateLight is 3"
call delay(0.5)
end sub
sub statelight4()
call putpin(9,0)
call putpin(10,0)
call putpin(11,1)
call putpin(12,0)
debug.print "stateLight is 4"
call delay(0.5)
end sub
' automatic street light code
Sub automatic()
autoContador = autoContador + 1
debug.print "autoCounter=" ; cStr(autoContador)
Select Case autocontador
case 1
debug.print "case1"
call Statelight1()
case 2
debug.print "case2"
call statelight2()
case 3
debug.print "case3"
call statelight1()
case 4
debug.print "case4"
call statelight2()
case 5
debug.print "case5"
call statelight1()
case 6
debug.print "case6"
call statelight2()
case 7
debug.print "case7"
call statelight1()
case 8
debug.print "case8"
call statelight2()
case 9
debug.print "case9"
call statelight1()
case 10
debug.print "case10"
call statelight2()
call delay (0.5)
case 11 to 14
call statelight3()
case 15 to 22
call stateLight4()
case 23 to 24
call stateLight3()
case 25
autoContador = 0
end select
end sub
'more elegant code for fancy street light
sub manual()
dim lightOff as byte
dim potVar as integer
dim thingOn as byte
dim lightOn as byte
' turn counter for street light to zero
autoContador = 0
'turn off all the leds
for lightOff = 8 to 11
call putPin(lightOff, 0)
next
'set a variable and divide the potentiometer range in 5 states
potVar = getADC(13)
thingOn = cbyte(potVar\200)
debug.print "thinOn = " ; cstr(thingOn)
'thingOn is 0
if thingOn = 0 then
call freqout(17, 1000,1000,100)
delay 0.4
for lightOff = 9 to 12
call putPin(lightOff, 0)
next
end if
'thingOn is 1
if thingOn = 1 then
call freqout(17, 1100,1100,80)
for lightOff = 10 to 12
call putPin(lightOff, 0)
next
for lightOn = 9 to 9
call delay(0.2)
call putpin(lightOn,1)
call delay(0.2)
call putpin(lightOn,0)
next
end if
'thingOn is 2
if thingOn = 2 then
call freqout(17, 1200,1200,60)
for lightOff = 11 to 12
call putPin(lightOff, 0)
next
for lightOn = 9 to 10
call putpin(lightOn,1)
call delay(0.1)
call putpin(lightOn,0)
next
end if
'thingOn is 3
if thingOn = 3 then
call freqout(17, 1300,1300,40)
for lightOff = 12 to 12
call putPin(lightOff, 0)
next
for lightOn = 9 to 11
call putpin(lightOn,1)
call delay(0.05)
call putpin(lightOn,0)
next
end if
'thingOn is 4
if thingOn = 4 then
call freqout(17, 1400,1400,20)
for lightOn = 9 to 12
call putpin(lightOn,1)
call delay(0.025)
call putpin(lightOn,0)
next
end if
'thingOn is 5
if thingOn = 5 then
call freqout(17, 1500,1500,10)
for lightOn = 9 to 12
call putpin(lightOn,1)
call delay(0.0125)
call putpin(lightOn,0)
next
end if
end sub
'light up and beep when starting
Sub startAgain()
call delay(0.5)
dim nextLightVar as byte
dim timesResetLight as byte
for timesResetLight = 1 to 4
for nextLightVar = 9 to 12
call putPin(nextLightVar, 1)
call putPin((nextLightVar - 1), 0)
call delay(0.04)
if nextLightVar = 12 then
call putPin(12,0)
end if
next
next
call delay(0.5)
end sub
journal index
|