Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

WEIRD SNIPPET OF CODE

Status
Not open for further replies.

MrDEB

Well-Known Member
Been experimenting with my Swordfish code for my art project and this is what I came up with.
WHY IT WORKS?
the head_light LED goes dim then bright and repeats

ticks = (ticks + 1)
if ticks >=4
then
ticks = 0
end if

While true
//LOOKS WEIRD CODE BUT THE HEADLIGHT GOES DIM THEN BRIGHT
if ticks <= 3 then
head_light = 1
end if
if ticks >= 2 then
head_light = 0
endif
 
When ticks is 0 or 1, light is on steady. When ticks is 2 or 3, it first goes on and next if it gets turned off, hence it is flashed briefly and appears dim. Not sure the reasoning behind that logic… Depends also on how fast ticks is incremented, that is not shown.
 
Sure you dont want first code group inside second group, the while loop ?

Is ticks signed variable ? Watch out for that, could wind up in loop long time
effectively doing nothing.


Regards, Dana,
 
here is the entire code
Code:
{
*****************************************************************************
*  Name    : UNTITLED.BAS                                                   *
*  Author  : [select VIEW...EDITOR OPTIONS]                                 *
*  Notice  : Copyright (c) 2023 [select VIEW...EDITOR OPTIONS]              *
*          : All Rights Reserved                                            *
*  Date    : 3/2/2023                                                       *
*  Version : 1.0                                                            *
*  Notes   : art sign project for town centienal                                                               *
*          :                                                                *
*****************************************************************************
}
Device = 18F2221
Clock = 8

Include "intosc.bas"   
#option DIGITALIO_INIT = true
Include "setdigitalio.bas"

//12 VOLT LED STRIPS
Dim R    As PORTA.0     // channel_1
Dim Y    As PORTA.1     // channel_2
Dim D    As PORTA.2     // channel_3
Dim E    As PORTA.3     // channel_4
Dim R_1  As PORTA.4     // channel_5
Dim W    As PORTA.5     // channel_6
Dim O    As PORTA.7     // channel_8
Dim D_1  As PORTA.6     // channel_7

Dim Red_Candle_STRIPS As PORTB.0    // turn on mosfet Q10
Dim Flames_ENABLE As PORTB.1        // TOGGLE SWITCH PORT, input with external pullup

//CAKE SPRINKLES CATHODES CONNECTED TO PORT PINS
Dim Row_4 As PORTB.2
Dim Row_3 As PORTB.3
Dim Row_2 As PORTB.4
Dim Row_1 As PORTB.5

//CANDLE FLAMES
Dim Flame1 As PORTC.0
Dim Flame2 As PORTC.1
Dim Flame3 As PORTC.2

// SPRINKLES ANODES
Dim RED     As PORTC.3
Dim WHITE   As PORTC.4
Dim BLUE    As PORTC.5
Dim GREEN   As PORTC.6

//HEADLIGHT ON TRAIN ENGINE
Dim HEAD_LIGHT As PORTC.7   // HEAD LIGHT

// isr timer module
// default is TMR1, 1ms refresh interval
Include "isrtimer.bas"
Const TIMER1 = 0
Const TIMER2 = 1


dim Candle_flames as byte
Dim led_step As Byte
Dim Sprinkles As Byte
dim Ticks as byte
// timer 1 event handler
// this routine will be called every timer1 Interval
Sub OnTimer1()
    led_step = led_step + 1
    Select (led_step)
        Case 1: R = 1 
        Case 2: Y = 1 
        Case 3: D = 1
        Case 4: E = 1 
        Case 5: R_1 = 1
        Case 6: W = 1
        Case 7: O = 1
        Case 8: D_1 = 1 
        Case >12:  // turn them all off
            R = 0
            Y = 0
            D = 0
            E = 0
            R_1 = 0
            W = 0
            O = 0
            D_1 = 0
            // start over next time
            led_step = 0
    End Select
    Red_Candle_STRIPS = 1//TURN ON RED "100"
    ticks = (ticks + 1)
    if ticks >=4
    then
    ticks = 0
    end if     
End Sub

Sub OnTimer2()  //SECOND TIMER
  

    Sprinkles = Sprinkles + 1
    Select (Sprinkles)
        Case 1: Row_4=1 Row_1=0     // turn off previous, turn on next
        Case 2: Row_1=1 Row_2=0
        Case 3: Row_2=1 Row_3=0
        Case 4: Row_3=1 Row_4=0
        Case 5: Row_4=1 Row_1=0
        Case 6: Row_1=1 Row_2=0
        Case 7: Row_2=1 Row_3=0
        Case 8: Row_3=1 Row_4=0
        Case 9: Row_4=1 row_1=0
        Case >12:
        Sprinkles = 0
    End Select     
End Sub

// set all IO pin directions and initial settings
Sub InitIO()
    // before enabling the outputs, init all LAT registers low
    LATA = 0
    LATB = 0
    LATC = 0
    
    // set specific output states
    RED = 1
    WHITE = 1
    BLUE = 1
    GREEN = 1

    Row_1 = 1
    Row_2 = 1
    Row_3 = 1
    Row_4 = 1
 
    Flame1 = 0
    Flame2 = 0
    Flame3 = 0
 
    // enable outputs, except for PORTB.1  Portb.1 is input
    TRISA = 0
    TRISB = 0
    TRISC = 0
    Input(Flames_ENABLE)
End Sub

main:
InitIO()
led_step = 0
Sprinkles = 0
HEAD_LIGHT = 0
Candle_flames = 0
ticks = 0
// initialise the timer module for 2 timers...
Timer.Initialize(2)

// initialise each timer - refresh is every 1ms
Timer.Items(TIMER1).Interval = 1000       // 1000 x 1ms = 1 sec
Timer.Items(TIMER1).OnTimer = @OnTimer1   // timer event handler

Timer.Items(TIMER2).Interval = 1000       // 1000 x 1ms = 1 sec
Timer.Items(TIMER2).OnTimer = @OnTimer2   // timer event handler

 // enable the timers...
Timer.Items(TIMER1).Enabled = true
Timer.Items(TIMER2).Enabled = true // timer2 is enabled true when flame_enable = 0

Timer.Start()
 setalldigital
While true
//LOOKS WEIRD CODE BUT THE HEADLIGHT GOES DIM THEN BRIGHT
      if ticks <= 3 then
      head_light = 1
    end if
            if ticks >= 2 then
            head_light = 0
            endif

    If Flames_ENABLE = 1   // TOGGLE SWITCH GROUNDED = HIGH (OFF)
           Then
           flame1 = 0
           flame2 = 0
           flame3 = 0
           RED = 0
           WHITE =0
           BLUE = 0
           GREEN = 0
            
        end if
  
    if flames_enable = 0 then   //enable portC.3-6 TOGGLE SWITCH = LOW(ON)
            RED = 1
            WHITE =1
            BLUE = 1
            GREEN =1
    Candle_flames = Candle_flames + 1
         select(candle_flames)
        
          case 1: flame1 = 1
          case 2: flame2 = 1
          case 3: flame3 = 1
          case >5:
     end select   
    end if
        
          
 
  wend
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top