Swordfish will accept either style as a comment.Never knew that // was a comment in basic, thought it was a C thing. In basics I've used it's normally '
{
*****************************************************************************
* Name : UNTITLED.BAS *
* Author : [select VIEW...EDITOR OPTIONS] *
* Notice : Copyright (c) 2022 [select VIEW...EDITOR OPTIONS] *
* : All Rights Reserved *
* Date : 5/21/2022 *
* Version : 1.0 *
* Notes : copy n pasted from
Report
rjenkinsgb *
* : *
*****************************************************************************
}
Device = 18F43K22
Clock = 8
// int osc and IO pin libraries
Include "intosc.bas"
#option DIGITALIO_INIT = true // automatically call setalldigital
Include "setdigitalio.bas"
Include "convert.bas"
//LEDS
DIM LED1 AS PORTA.0
DIM LED2 AS PORTB.0
DIM LED3 AS PORTC.0
DIM LED4 AS PORTD.0
DIM Time1 AS BYTE
DIM Time2 AS BYTE
DIM Time3 AS BYTE
DIM Time4 AS BYTE
DIM INDEX AS LONGWORD
dim counter as LONGWORD
dim Bright as longword // a wider range of settings
dim Dimmer as longword
//LEDS ALL OFF AT START OF PROGRAM
led1 = 0
led2 = 0
led3 = 0
led4 = 0
Time1 = 25
Time2 = 50
Time3 = 75
Time4 = 100
//INCREMENT/ DECRIMINATE ON/OFF TIMES
INDEX = 1
//MAKE ALL OUTPUTS
trisa=0
'trisb=0
'trisc=0
'trisd=0
While TRUE
// LED One control
IF INDEX <=10000 THEN
TIME1 = (TIME1 + INDEX)
endif
led1=1
delayMs(Time1) //INCREMENT THIS FOR BRIGHTER
led1=0
DELAYMS(TIME1)
// LED One control
IF INDEX >=10000 THEN
TIME1 = (TIME1 - INDEX)
endif
led1=1
delayMs(Time1) // DECRIMENT FOR DIMMER
led1=0
DELAYMS(TIME1)
INDEX = (INDEX +1) //INCRIMENT INDEX EACH PASS THROUGH LOOP
IF INDEX >= 10000 THEN
INDEX = (INDEX - 1)
End If
Wend
BRIGHT_1 = 1
DIMMER_1 = 25000
BRIGHT_2 = 25000
DIMMER_2 = 1
INDEX = 1
//MAKE ALL OUTPUTS
trisa=0
'trisb=0
'trisc=0
'trisd=0
While TRUE
//this is a real smooth from dim to bright
repeat
led1 = 1
delayus(bright_1) // 1
led1 = 0 // dim
delayus (dimmer_1) //25000
bright_1 = (bright_1 +1)
dimmer_1 = (dimmer_1 - 1)
until
bright_1 = 2500
//this won't go back to dim??
repeat
led1 = 1
delayus(dimmer_2) // 1
led1 = 0 // full bright
delayus (bright_2) //255
bright_2 = (bright_2 -1)
dimmer_2 = (dimmer_2 + 1)
until
dimmer_2 = 2500
wend
{
*****************************************************************************
* Name : UNTITLED.BAS *
* Author : [select VIEW...EDITOR OPTIONS] *
* Notice : Copyright (c) 2022 [select VIEW...EDITOR OPTIONS] *
* : All Rights Reserved *
* Date : 5/21/2022 *
* Version : 1.0 *
* Notes : WORKS GREAT 5/23/22 *
* : *
*****************************************************************************
}
Device = 18F43K22
Clock = 8
// int osc and IO pin libraries
Include "intosc.bas"
#option DIGITALIO_INIT = true // automatically call setalldigital
Include "setdigitalio.bas"
Include "convert.bas"
//LEDS
DIM LED1 AS PORTA.0
DIM LED2 AS PORTE.1
DIM LED3 AS PORTC.0
DIM LED4 AS PORTD.0
dim counter as LONGWORD
dim Bright_1 as longword // a wider range of settings FOR A SMOOTHER TRANSITION
dim Dimmer_1 as longword
dim Bright_2 as longword // a wider range of settings
dim Dimmer_2 as longword
//LEDS ALL OFF AT START OF PROGRAM
led1 = 0
led2 = 0
led3 = 0
led4 = 0
//INCREMENT/ DECRIMINATE ON/OFF TIMES
BRIGHT_1 = 0
DIMMER_1 = 0
BRIGHT_2 = 0
DIMMER_2 = 0
INDEX = 1
//MAKE ALL OUTPUTS
'trisa=0
'trisb=0
'trisc=0
'trisd=0
OUTPUT (LED1)
OUTPUT(LED2)
// USING VALUE OF 5000 FOR A S M O O T H TRANSITION.
While TRUE
bright_1 = 1 // bright_1 is ON time variable
dimmer_1 = 5000 //dimmer_1 is OFF time variable
repeat
led1 = 1
delayus(bright_1)
led1 = 0 // dim
delayus (dimmer_1)
bright_1 = (bright_1 +1) // increase ON time BY 1
dimmer_1 = (dimmer_1 - 1) //decrease OFF time BY 1
until
bright_1 >= 5000 //HIGH BRIGHT
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
//reset values
bright_2 = 5000
dimmer_2 = 1
LED2 = 1 // LED2 COMES ON WHEN LED1 STARTS FROM HIGH BRIGHT TO FULL DIM
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
repeat
led1 = 1
delayus(BRIGHT_2) // LED is full brightness FIRST PASS IN LOOP
led1 = 0
delayus(DIMMER_2) // full dim FIRST PASS IN LOOP
bright_2 = (bright_2 - 1)
dimmer_2 = (dimmer_2 + 1)
until
bright_2 = 1
//reset valuesxxxxxxxxxxxxxx
bright_2 = 1
dimmer_2 = 5000
LED2 = 0 // LED2 GOES OFF WHEN LED1 STARTS FROM FULL DIM TO HIGH BRIGHT
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxx
wend
{
//increase brghtness
REPEAT
// LED One control
IF INDEX <=255 THEN
BRIGHT = (INDEX + 1)
endif
led1=1
delayMs(DIMMER) //INCREMENT THIS FOR BRIGHTER
led1=0
DELAYMS(BRIGHT)
BRIGHT = (BRIGHT + 1)
UNTIL
BRIGHT >= INDEX
// decrease brightness
REPEAT
// LED One control
IF INDEX >= 1 THEN
DIMMER = (INDEX)
endif
led1=1
delayMs(BRIGHT) // DECRIMENT FOR DIMMER
led1=0
DELAYMS(DIMMER)
UNTIL
DIMMER <= INDEX
IF INDEX <= 255 THEN
BRIGHT = (INDEX + 1) // increase BRIGHT until it hits 100000
End If
IF DIMMER >= 100000 THEN //decrease DIMMER until it hits 0
DIMMER = (INDEX - 1)
End If
' if INDEX = 0 then index = (INDEX + 1) end if
'if index = 1000000 then
Wend
Sub Fade()
Bright_1 = 1 // bright_1 is ON time variable
Dimmer_1 = 5000 //dimmer_1 is OFF time variable
Repeat
PORTA = %11100111
DelayUS(Bright_1)
PORTA = %00000000 // dim
DelayUS (Dimmer_1)
Bright_1 = (Bright_1 +1) // increase ON time BY 1
Dimmer_1 = (Dimmer_1 - 1) //decrease OFF time BY 1
Until
Bright_1 >= 5000 //HIGH BRIGHT
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
//reset values
Bright_2 = 5000
Dimmer_2 = 1
' LED2 = 1 // LED2 COMES ON WHEN LED1 STARTS FROM HIGH BRIGHT TO FULL DIM
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Repeat
PORTA = %11100111
DelayUS(Bright_2) // LED is full brightness FIRST PASS IN LOOP
PORTA =%00000000
DelayUS(Dimmer_2) // full dim FIRST PASS IN LOOP
Bright_2 = (Bright_2 - 1)
Dimmer_2 = (Dimmer_2 + 1)
Until
Bright_2 = 1
//reset valuesxxxxxxxxxxxxxx
Bright_2 = 1
Dimmer_2 = 5000
' LED2 = 0 // LED2 GOES OFF WHEN LED1 STARTS FROM FULL DIM TO HIGH BRIGHT
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Dim Counter as Longword
Counter = 0
While 1 = 1
// some code will go here
Inc (Counter)
If Counter > 4000000000 then
Counter = 0
End If
Wend
Dim Counter as Longword
Dim LED1On as Longword
Dim LED1Off as Longword
LED1On = 0
LED1Off = 0
Counter = 0
While 1 = 1
// some code will go here
If Counter = LED1On then
LED1 = 1
LED1On = Counter + 20
LED1Off = Counter + 10
End If
If Counter = LED1Off then
LED1 = 0
End If
Inc (Counter)
If Counter > 4000000000 then
Counter = 0
End If
Wend
Dim Counter as Longword
Dim LED1On as Longword
Dim LED1Off as Longword
Dim LED2On as Longword
Dim LED2Off as Longword
LED1On = 0
LED1Off = 0
LED2On = 0
LED2Off = 0
Counter = 0
While 1 = 1
// some code will go here
If Counter = LED1On then
LED1 = 1
LED1On = Counter + 20
LED1Off = Counter + 10
End If
If Counter = LED1Off then
LED1 = 0
End If
If Counter = LED2On then
LED2 = 1
LED2Off = 20
LED2On = 500
End If
If Counter = LED2Off then
LED2 = 0
End If
Inc (Counter)
If Counter > 4000000000 then
Counter = 0
End If
Wend
You put a lot of effort into that explanation let's hope it's not wasted.Since MrDEB has been silent today, he may have figured out that a whole bunch of serial routines with delay statements aren't going to accomplish what he wants at least not very well.
One way to do what he wants is to base all actions on a loop counter, with at most exactly ONE delay statement. Ok, let's start with the basic program loop.
Code:Dim Counter as Longword Counter = 0 While 1 = 1 // some code will go here Inc (Counter) If Counter > 4000000000 then Counter = 0 End If Wend
What happens in this program loop? The variable Counter counts from 0 to 4,000,000,000. This number can actually be 2^32, but rounding it off won't matter much. The IF statement may not be strictly necessary. The Counter value should automatically wrap back to zero.
Pretty neat, huh? Well, let's work on that. Let's add an LED, say at 50% brightness .
Code:Dim Counter as Longword Dim LED1On as Longword Dim LED1Off as Longword LED1On = 0 LED1Off = 0 Counter = 0 While 1 = 1 // some code will go here If Counter = LED1On then LED1 = 1 LED1On = Counter + 20 LED1Off = Counter + 10 End If If Counter = LED1Off then LED1 = 0 End If Inc (Counter) If Counter > 4000000000 then Counter = 0 End If Wend
Ok, so what happens here? Led1On starts at 0. When the Counter value equals 0, three things happen:
¤ LED1 is turned on
¤ LED1On is set to 20
¤ LED1Off is set to 10
When the Counter = 10, LED1 will be turned off.
When the Counter = 20, the sequence repeats.
LED1 is on half the time, off half the time, so the brightness is 50%. This is kind of crude; making it a subroutine (do not call this a sub route. That is gibberish.) would be cleaner.
So now let's add a second LED, operating at the same time the first does it's thing.
Code:Dim Counter as Longword Dim LED1On as Longword Dim LED1Off as Longword Dim LED2On as Longword Dim LED2Off as Longword LED1On = 0 LED1Off = 0 LED2On = 0 LED2Off = 0 Counter = 0 While 1 = 1 // some code will go here If Counter = LED1On then LED1 = 1 LED1On = Counter + 20 LED1Off = Counter + 10 End If If Counter = LED1Off then LED1 = 0 End If If Counter = LED2On then LED2 = 1 LED2Off = 20 LED2On = 500 End If If Counter = LED2Off then LED2 = 0 End If Inc (Counter) If Counter > 4000000000 then Counter = 0 End If Wend
So now, while LED1 is flashing rapidly on and off to be 50% bright, LED2 is flashing very briefly on and then off for a long period, resulting in a twinkling effect. Two LEDs operating independently in the same loop.
This definitely is not polished code, but will show anybody willing to take a few minutes to understand the code how to use this technique. This loop incrementing up to 4,000,000,000 is going to execute very quickly. The on/off Counter times need to be adjusted to suit the effect desired, and a short delay may need to added at the end of the loop. Also, at the end of the loop, initial values will need to be reset to zero.
The picture shows the process. On and off periods might be set to random numbers. But remember, all actions are based on Counter values. Exactly one delay statement might be added. MrDEB's fading routine can also be adapted to this technique.
View attachment 137243
You put a lot of effort into that explanation let's hope it's not wasted.
Mike.
{
*****************************************************************************
* Name : UNTITLED.BAS *
* Author : [select VIEW...EDITOR OPTIONS] *
* Notice : Copyright (c) 2022 [select VIEW...EDITOR OPTIONS] *
* : All Rights Reserved *
* Date : 5/25/2022 *
* Version : 1.0 *
* Notes : *
* : *
*****************************************************************************
}
Device = 18F43K22
Clock = 8
// int osc and IO pin libraries
Include "intosc.bas"
#option DIGITALIO_INIT = true // automatically call setalldigital
Include "setdigitalio.bas"
// hardware
Dim INDEX As LongWord //4294967295
Dim m As Byte
Dim w As Byte
Dim x As Byte
Dim y As Byte
Dim z As Byte
Dim Bright_1 As LongWord // a wider range of settings FOR A SMOOTHER TRANSITION
Dim Dimmer_1 As LongWord
Dim Bright_2 As LongWord // a wider range of settings
Dim Dimmer_2 As LongWord
Dim Counter As LongWord
Dim LED1On As LongWord
Dim LED1Off As LongWord
dim led1 as porta.0
main:
// init hdw
// LED port - all outputs 2 LEDs per port in parallel 330 ohm resistor
TRISA = 0 // SET ALL PORTS AS OUTPUTS
TRISB = 0
TRISC = 0
TRISD = 0
TRISE = 0
PORTE=%000 //TURN OFF ALL PORTS
PORTD=%00000000
PORTC=%00000000
PORTA=%00000000
PORTB=%00000000
LED1On = 0
LED1Off = 0
Counter = 0
While 1 = 1
// some code will go here
If Counter = LED1On Then //first pass led1 = 0
LED1 = 1 // led1 is on
LED1On = Counter + 20
LED1Off = Counter + 10 //first pas led1 = 10
End If
If Counter = LED1Off Then
LED1 = 0
End If
Inc (Counter)
If Counter > 4000000000 Then // 4000000000
Counter = 0
End If
Wend
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?