newbi trying to declare 3 variables

Coding and general discussion relating to the compiler

Moderators: David Barker, Jerry Messina

Post Reply
MrDEB
Posts: 12
Joined: Fri Apr 24, 2009 1:45 am
Location: Salmon, Idaho

newbi trying to declare 3 variables

Post by MrDEB » Tue Jun 30, 2009 2:00 pm

tried several different things but keep getting syntax errors like identifier expected??
what I want to do is have 3 variables (red, green, blue)
then in the prg the variables are inc or dec
this code is a PWM with all three LES on at same time but increase/decrease at different times but they all are on at same time.
trying to simulate a flickering candle other than turning the led off and on

Code: Select all

Device = 18F1320
Clock = 8

Include "IntOSC8.bas" 
Include "RandGen.bas"
Include "Utils.bas" 
Dim TMR2IE As PIE1.1,         // TMR2 interrupt enable
    TMR2IF As PIR1.1,         // TMR2 overflow flag
    TMR2ON As T2CON.2,        // Enables TMR2 to begin incrementing
    Signal_Pin As PORTA.0     // Signal output to frequency meter
Dim red As byte,
dim green As byte
dim blue as byte  
//dim mSb As Word 
//dim mSg As Word 
dim Cred as boolean
dim Cgreen as boolean
dim Cblue as boolean






Dim Red_Pin As PORTB.0,
    Green_Pin As PORTB.1,
    Blue_Pin As PORTB.2,
    Red_Duty As Byte,
    Green_Duty As Byte,
    Blue_Duty As Byte,
    Red_DutyVal As Byte,
    Green_DutyVal As Byte,
    Blue_DutyVal As Byte,
    RandomVal As Byte
 
//Dim red As Word,
//dim green As word
//dim blue as word  
//dim mSb As Word 
//dim mSg As Word
         
 
Interrupt TMR2_Interrupt()
    High(Signal_Pin)
    Save(0)                   // Back up system variables   
    If TMR2IF = 1 Then        // Check if the interrupt was from TMR2   
        TMR2IF = 0            // Clear the TMR2 interrupt flag
        red = red + 50
        green = green + 50
        blue = blue + 50
        If rd >= 1000 Then
            rd = rd - 1000
           Inc red
           endif
          
        If grn >= 1000 Then
            grn = grn - 1000
           Inc grn
           
           
        EndIf       
        Inc(Red_DutyVal)
        Inc(Green_DutyVal)
        Inc(Blue_DutyVal)
        If Red_DutyVal > Red_Duty Or Red_Duty = 0 Then
            Red_Pin = 0
        Else
            Red_Pin = 1
        EndIf
        If Green_DutyVal > Green_Duty Or Green_Duty = 0 Then
            Green_Pin = 0
        Else
            Green_Pin = 1
        EndIf
        If Blue_DutyVal > Blue_Duty Or Blue_Duty = 0 Then
            Blue_Pin = 0
        Else
            Blue_Pin = 1
        EndIf               
    EndIf                     //
    Restore                   // Restore system variables
    Low(Signal_Pin)
End Interrupt
 
Private Sub TMR2_Initialize()
    TMR2ON = 0                // Disable TMR2
    TMR2IE = 0                // Turn off TMR2 interrupts   
 
    PR2 = 149                 // TMR2 Period register PR2
    T2CON = %00000001         // T2CON 0:1 = Prescale
                              //        00 = Prescale is 1:1
                              //        01 = Prescale is 1:4
                              //        1x = Prescale is 1:16                                 
                              //      3:6 = Postscale              
                              //     0000 = 1:1 postscale
                              //     0001 = 1:2 postscale
                              //     0010 = 1:3 postscale...
                              //     1111 = 1:16 postscale
 
    TMR2 = 0                  // Reset TMR2 Value   
    TMR2IE = 1                // Enable TMR2 interrupts
    TMR2ON = 1                // Enable TMR2 to increment
    Enable(TMR2_Interrupt)
End Sub
 
// Start Of Program...
Low(Red_Pin)
Low(Green_Pin)
Low(Blue_Pin)
Red_Duty = 0
Green_Duty = 2
Blue_Duty = 0
Red_DutyVal = 0
Green_DutyVal = 4
Blue_DutyVal = 0
 
uS = 0
mS = 0

 
RandGen.Initialize(128)       // Initialize the Random Number generator
TMR2_Initialize               // Setup and enable TMR2
 
While (True)                    // Create an infinite loop
    //RandomVal = RandGen.Rand  // Grab a random number from 0 to 255 
    // Select RandomVal         // Find out what colour to increase/decrease
        //Case 0 To 42
            While Red_Duty < 255
              And Green_Duty < 255
              And Blue_Duty < 255
                mS(0) = 0
                mS(1) = 0
                mS(2) = 0
                Repeat
                Until mS(0) = 19
                And mS(1) = 19
                And mS(2) = 19
                
                Inc(Red_Duty)
                Inc(Green_Duty)
                
                Inc(Blue_Duty)
            
            Wend
       // Case 43 To 84
            While Red_Duty > 0
          And  Green_Duty > 0
         And Blue_Duty > 0
         mS(0) = 0
                mS(1) = 0
                mS(2) = 0
               
                Repeat
                Until mS(0) = 19
                And mS(1) = 19
                And mS(2) = 19
                Dec(Red_Duty)
                Dec(Green_Duty)
                Dec(Blue_Duty)
            Wend
          
Wend


Doj
Posts: 362
Joined: Wed Apr 11, 2007 10:18 pm
Location: East Sussex

Post by Doj » Wed Jul 01, 2009 8:53 am

Plenty of problems to solve!

The code does not have the include files declared so is unable to compile.

Include "IntOSC8.bas"
Include "RandGen.bas"

When these are commented out, issues that come up are:-

Variable "rd" is not declared
INC and DEC are routine calls so must have the parameter wrapped in ()
variable "grn" is not declared
variable "uS" not declared
variable "mS" not declared
RandGen.Initialize(128) is not found, I suppose its in one of the includes

Francis
Registered User
Registered User
Posts: 314
Joined: Sun Mar 25, 2007 9:40 am
Location: Devon

Post by Francis » Wed Jul 01, 2009 9:25 am

Lots to look at.

Also:
Dim red As byte,
dim green As byte

- either, remove that comma or the dim on next line.

MrDEB
Posts: 12
Joined: Fri Apr 24, 2009 1:45 am
Location: Salmon, Idaho

include is there??

Post by MrDEB » Wed Jul 01, 2009 11:59 am

the include files are declared in the beginning?
Include "IntOSC8.bas"
Include "RandGen.bas"

should they be located elsewhere??
like after the DIM statements??
I have tried several different ways to get ms as a 3 part variable but havn't got it right.
could be the include is in wrong place??

Doj
Posts: 362
Joined: Wed Apr 11, 2007 10:18 pm
Location: East Sussex

Post by Doj » Wed Jul 01, 2009 8:06 pm

you might have the include files on your computer but the code you posted does not so we can not tell what is inside the includes, they are parts of the code which would need to be posted separately.

In your code they are in the correct place.

An include is used to store other code in separate files which are "included" in the main code when compiled.
This is done to make code easier to read and transportable between different programs.

I do not know what you mean about "ms" being a three part variable?

Please explain the logic you are trying to achieve.

MrDEB
Posts: 12
Joined: Fri Apr 24, 2009 1:45 am
Location: Salmon, Idaho

here is a GREAT looking candle flicker

Post by MrDEB » Thu Jul 02, 2009 1:53 am

I toyed with several different things but after playing with RandomVal the code works very well.
It simulates a real candle flicker.
changing the subtraction value from RandomVal will alter the effect.
I had -5 for the red_duty but changed to 25. looks better.
I left all the orginal code but added // so I could go back and see what mistakes or ??

Code: Select all

Device = 18F1320
Clock = 8

Include "IntOSC8.bas" 
Include "RandGen.bas"
Include "Utils.bas" 
Dim TMR2IE As PIE1.1,         // TMR2 interrupt enable
    TMR2IF As PIR1.1,         // TMR2 overflow flag
    TMR2ON As T2CON.2,        // Enables TMR2 to begin incrementing
    Signal_Pin As PORTA.0     // Signal output to frequency meter
 
Dim Red_Pin As PORTB.0,
    Green_Pin As PORTB.1,
    Blue_Pin As PORTB.2,
    Red_Duty As Byte,
    Green_Duty As Byte,
    Blue_Duty As Byte,
    Red_DutyVal As Byte,
    Green_DutyVal As Byte,
    Blue_DutyVal As Byte,
    RandomVal As Byte
   
 
Dim uS As Word,
    mS As Word           
   // ranval(1)as word
Interrupt TMR2_Interrupt()
    High(Signal_Pin)
    Save(0)                   // Back up system variables   
    If TMR2IF = 1 Then        // Check if the interrupt was from TMR2   
        TMR2IF = 0            // Clear the TMR2 interrupt flag
        uS = uS + 50
        If uS >= 1000 Then
            uS = uS - 1000
            Inc(mS)
        EndIf       
        Inc(Red_DutyVal)
        Inc(Green_DutyVal)
        Inc(Blue_DutyVal)
        If Red_DutyVal > Red_Duty Or Red_Duty = 0 Then
            Red_Pin = 0
        Else
            Red_Pin = 1
        EndIf
        If Green_DutyVal > Green_Duty Or Green_Duty = 0 Then
            Green_Pin = 0
        Else
            Green_Pin = 1
        EndIf
        If Blue_DutyVal > Blue_Duty Or Blue_Duty = 0 Then
            Blue_Pin = 0
        Else
            Blue_Pin = 1
        EndIf               
    EndIf                     //
    Restore                   // Restore system variables
    Low(Signal_Pin)
End Interrupt
 
Private Sub TMR2_Initialize()
    TMR2ON = 0                // Disable TMR2
    TMR2IE = 0                // Turn off TMR2 interrupts   
 
    PR2 = 149                 // TMR2 Period register PR2
    T2CON = %00000001         // T2CON 0:1 = Prescale
                              //        00 = Prescale is 1:1
                              //        01 = Prescale is 1:4
                              //        1x = Prescale is 1:16                                 
                              //      3:6 = Postscale              
                              //     0000 = 1:1 postscale
                              //     0001 = 1:2 postscale
                              //     0010 = 1:3 postscale...
                              //     1111 = 1:16 postscale
 
    TMR2 = 0                  // Reset TMR2 Value   
    TMR2IE = 1                // Enable TMR2 interrupts
    TMR2ON = 1                // Enable TMR2 to increment
    Enable(TMR2_Interrupt)
End Sub
 
// Start Of Program...
Low(Red_Pin)
Low(Green_Pin)
Low(Blue_Pin)
Red_Duty = 30
Green_Duty = 0
Blue_Duty = 0
Red_DutyVal = 0
Green_DutyVal = 0
Blue_DutyVal =0
 
uS = 0
mS = 0

RandGen.Initialize(128)       // Initialize the Random Number generator
TMR2_Initialize               // Setup and enable TMR2
 
While True                    // Create an infinite loop
    RandomVal = RandGen.Rand  // Grab a random number from 0 to 255 
    // Select RandomVal         // Find out what colour to increase/decrease
        //Case 0 To 42
             Red_Duty =RandomVal-25 
                //mS = 0
             // (Red_Duty)=Rand(1)
               // Repeat
               // Until mS = 19
               // and Red_Duty>0
           // endif
           
            
        //Case 43 To 84
            If Red_Duty > 0
              Then  mS = 0
                Repeat
                Until mS = 19
                Dec(Red_Duty)
            EndIf
       // Case 84 To 127
             Green_Duty =RandomVal-20 
              //Then  mS = 0
              //  Repeat
              //  Until mS = 19
              //  Inc(Green_Duty)
           // EndIf
       // Case 128 To 170
            if Green_Duty > 0
              Then  mS = 0
                Repeat
                Until mS = 19
                Dec(Green_Duty)
            EndIf
      //  Case 170 To 212
             Blue_Duty = RandomVal-30
             // Then  mS = 0
              //  Repeat
              //  Until mS = 19
              //  Inc(Blue_Duty)
           // EndIf
       // Case 212 To 255
            If Blue_Duty > 0
              Then  mS = 0
                Repeat
                Until mS = 19
                Dec(Blue_Duty)
            EndIf
            Wend
          
         End       



Post Reply