You didn't set the state to 2 after extinguishing the LED.
However, using a delay is normally not a good idea as nothing else can happen like going back to state zero if key 0 is released. Better to let the interrupt preform the timing and just change the state when it times out.
This does require that the interrupt code is changed and so here is the full code,
Have fun.
Mike.
However, using a delay is normally not a good idea as nothing else can happen like going back to state zero if key 0 is released. Better to let the interrupt preform the timing and just change the state when it times out.
Code:
Case 3
Lit.2=1
TimeUp=0 //reset flag
Count=500*5 //wait 5 seconds
state=4
Case 4
if TimeUp=1 then
state=2 //if time up then go to state 2
endif
End Select
This does require that the interrupt code is changed and so here is the full code,
Code:
Device = 18F1320
Clock = 8 // 8MHz clock
Config OSC = INTIO2, WDT = OFF, LVP = OFF
Dim NOT_RBPU As INTCON2.7
Dim TMR2IE As PIE1.1
Dim TMR2IF As PIR1.1
//global variables
Dim Lit As Byte
Dim IntFlag As Bit
Dim Count As Integer
Dim TimeUp as bit
Dim Row As Byte
//interrupt routine gets called 500 times per second
//and multiplexes the LEDs Aslo sets a flag after 0.5S.
Interrupt MyInt()
TMR2IF=0
Row=Row+1
If Row=3 Then
Row=0
EndIf
TRISA = TRISA Or %11000001 //turn all LEDs off
PORTA = PORTA And %00111110
Select Row
Case 0
TRISA.0=0
PORTA.0=1
If Lit.0=1 Then
TRISA.6=0
EndIf
If Lit.5=1 Then
TRISA.7=0
EndIf
Case 1
TRISA.6=0
PORTA.6=1
If Lit.1=1 Then
TRISA.0=0
EndIf
If Lit.2=1 Then
TRISA.7=0
EndIf
Case 2
TRISA.7=0
PORTA.7=1
If Lit.4=1 Then
TRISA.0=0
EndIf
If Lit.3=1 Then
TRISA.6=0
EndIf
EndSelect
if Count>0 then
Count=Count-1
If Count=0 Then
TimeUp=1
endif
EndIf
End Interrupt
//main code starts here
Dim Keys As Byte
Dim Previous As Byte
Dim NewKeys As Byte
Dim State As Byte
OSCCON = $72 // select 8MHz internal clock
ADCON1 = $7f //all digital
NOT_RBPU=0 //WPUs on port B
Lit=0 //turn all LEDs off
T2CON = %01001110 //pre=16 post=10
PR2 = 25 //=8000000/4/16/10/25 = 500Hz
TMR2IE=1 //enable timer 2 interrupts
Enable(MyInt) //set interrupt going
State=0
While true
DelayMS(10) //required for debounce
Previous=Keys //keep copy of previous key state
Keys=PORTB Xor $ff //get new keys and invert
NewKeys=(Keys Xor Previous) And Keys //keep only new presses
If Keys.0=0 Then //if key 0 is released then reset
State=0
EndIf
Select State
Case 0
Lit=0 //if key1 pressed then light first LED
If NewKeys.0=1 Then
State=1
EndIf
Case 1
Lit.0=1
If NewKeys.2=1 Then
State=2
EndIf
Case 2
Lit.1=1
Lit.2=0 //extinguish LED 2 incase we came from state 4
If NewKeys.2=1 Then
State=3
EndIf
Case 3
Lit.2=1
TimeUp=0 //reset flag
Count=500*5 //wait 5 seconds
state=4
Case 4
if TimeUp=1 then
state=2 //if time up then go to state 2
endif
End Select
Wend
Lit=1
While true
DelayMS(10) //required for debounce
Previous=Keys //keep copy of previous key state
Keys=PORTB Xor $ff //get new keys and invert
NewKeys=(Keys Xor Previous) And Keys //keep only new presses
If(NewKeys.0=1 And Lit.0=1) Then //is button 1 pressed
Lit.0=0
Lit.1=1
ElseIf(NewKeys.0=1 And Lit.1=1) Then
Lit.0=1
Lit.1=0
EndIf
Wend
While true
DelayMS(10) //reqired for debounce
Previous=Keys //keep copy of previous key state
Keys=PORTB Xor $ff //get new keys and invert
NewKeys=(Keys Xor Previous) And Keys //keep only new presses
If(NewKeys.0=1) Then //is button 1 pressed
Lit=Lit*2 //shift it left
If(Lit=64) Then //got to end
Lit=1 //yes so wrap around
EndIf
EndIf
If(NewKeys.2=1) Then //is button 2 pressed
Lit=Lit/2 //shift it right
If(Lit=0) Then //got to end
Lit=32 //yes so wrap around
EndIf
EndIf
Wend
End
Have fun.
Mike.