Ok so in many occasions I felt like just putting up all my code but then I felt embarassed knowing how many people wil laugh...
But here it is.
This is what I am trying to do:
I have 6 switches SWx, 6 LEDs LEDx and 6 Optoisolator (which will trigger TRIACS) TRx
Normal mode:
if I press SW1, it will turn on LED1 and wait 10 seconds before turning on TR1.
The 10 seconds is just to wait in case I wanted to get in program mode.
Program mode:
Happens if I keep SW1 pressed for about 5 seconds. LED1 will blink 11 times and the program is waiting for user input on SW1 during 5 seconds. each press of SW1 will blink LED1 once. after 3 seconds of inactivity of SW1, LED1 wil blink the amount of time SW1 was pressed during program mode. if pressed 5 times during program mode LED1 will blink 5 times and it will save value 5 in EEPROM address 0. After this, going back to normal mode, if I press SW1, LED1 will go high, 10 seconds later TR1 will go high.
If I press SW1 again LED1 and TR1 will go low. If I don't press SW1, LED1 and TR1 will go low after 5 hours (value in EEPROM)
My default time value is 3 hours, minimum is 1 and max is 12 (720 min.)
At this point my code is (seams to be) working perfectly with the exception that I don't know how to test when the time is up. of course I need to keep track of 6 TR.
I ment to narrow down my code using indexes for all ports but my testing didn't go as planned so I have everything repeated 6 times.
PLease don't laugh
Code:
CLEAR
INCLUDE "AllDigital.pbp" ; Disable Analog functions
DEFINE OSC 8
OSCCON = %01110001
SW1 VAR PORTB.0
SW2 VAR PORTB.1
SW3 VAR PORTB.2
SW4 VAR PORTB.3
SW5 VAR PORTB.4
SW6 VAR PORTB.5
LED1 VAR PORTC.0
LED2 VAR PORTC.1
LED3 VAR PORTC.2
LED4 VAR PORTC.3
LED5 VAR PORTC.4
LED6 VAR PORTC.5
PZ VAR PORTC.6 ; piezo (beep)
TR1 VAR PORTA.0
TR2 VAR PORTA.1
TR3 VAR PORTA.2
TR4 VAR PORTA.3
TR5 VAR PORTA.4
TR6 VAR PORTA.5
SwStat Var bit ; used to control toggle of SWx
L var byte ; count SWz time hold
I VAR byte ; general use in for next
Counters VAR WORD[6] ; 6 words, for 6 buttons
ChargeTime VAR WORD[6] ; charging time in hours
DelayTime CON 10000 ; 10 seconds
T0IF VAR INTCON.2 ; TMR0 overflow flag
X VAR BYTE
Y var byte
OPTION_REG = %01010100 ; WPU on, TMR0 1:32 prescaler, internal clk
PAUSE 50 ; let everything settle
;------------------------------------------------------------------------/
; First time use, check if Charging time is configured. /
; if not, set defaults to 3 hours. (EEPROM 0 to 5 =3 and 6=0x25 /
;------------------------------------------------------------------------/
read 6,y ; if not 0x25 then it is the first time
if y <> $25 then
for I = 0 to 5: write i,3: next i: write 6,$25
endif
;------------------------------------------------------------------------/
Main:
l=0: y=0
WHILE !T0IF : WEND ; wait for timer to overflow
TMR0 = 193 ; load timer for 1ms
T0IF = 0 ; reset the overflow flag
IF (SW1=0) THEN Counters[0] = DelayTime : goto Channel1
IF (SW2=0) THEN Counters[1] = DelayTime : goto Channel2
IF (SW3=0) THEN Counters[2] = DelayTime : goto Channel3
IF (SW4=0) THEN Counters[3] = DelayTime : goto Channel4
IF (SW5=0) THEN Counters[4] = DelayTime : goto Channel5
IF (SW6=0) THEN Counters[5] = DelayTime : goto Channel6
FOR X = 0 to 5 ; cycle thru counters
IF Counters(X) > 0 THEN ; if counter is counting
Counters(X) = Counters(X) - 1 ; decrement counter
IF Counters(X) = 0 THEN ; if counter timed out
SELECT CASE X ; turn on the appropriate LED
CASE 0 : if led1 = 1 then gosub StartCharge1
CASE 1 : if led2 = 1 then gosub StartCharge2
CASE 2 : if led3 = 1 then gosub StartCharge3
CASE 3 : if led4 = 1 then gosub StartCharge4
CASE 4 : if led5 = 1 then gosub StartCharge5
CASE 5 : if led6 = 1 then gosub StartCharge6
END SELECT
ENDIF
ENDIF
NEXT X
GOTO Main
my code is too long, the rest is in the next message
Bookmarks