Does my code look alright?


Closed Thread
Results 1 to 10 of 10
  1. #1
    emtark's Avatar
    emtark Guest

    Default Does my code look alright?

    Hi all,

    Quick run down of what im trying to accomplish. New fishtank, want to simulate lighting patterns. I have blue and white LED strips lighting the tank, need to dim these at certain times of the day.

    I break the day into 8 sections (3 hrs each obviously)
    The idea is for the pic to start, then count up seconds, minutes, hours, then hour sections, and adjust the hpwm output to suit the particular section.

    This is what I have come up with so far, if you could comment that would be appreciated. I'm only very new to all of this so if i can learn tips that would be great.

    btw: I have listed 2 colours, i'll simply use 2 pics, or try to find a small say 18pin pic with 2 hpwm.


    '===================================
    '************************************************* ***************
    '* Name : UNTITLED.BAS *
    '* Author : [select VIEW...EDITOR OPTIONS] *
    '* Notice : Copyright (c) 2006 [select VIEW...EDITOR OPTIONS] *
    '* : All Rights Reserved *
    '* Date : 3/03/2006 *
    '* Version : 1.0 *
    '* Notes : *
    '* : *
    '************************************************* ***************

    'define dutycycle variable
    dutyCycle var byte


    'define clock variables
    minute var byte
    hour var byte
    second var byte
    hs var byte
    ledblue var byte
    ledwhite var byte

    '================================================= =====
    'start the main segment
    main:


    'add a second to the counter
    second = second + 1
    pause 1000

    'check to see if 60 seconds have passed
    if second = 60 then endsecond


    'HPWM Channel,Dutycycle,Frequency
    hpwm 1,ledblue,1000

    'end of section, repeat main code.
    goto main
    '================================================= ======

    'click 60 seconds to 1 minute
    endsecond:
    minute = minute + 1
    second = 0
    if minute = 60 then endminute
    return

    'click 60 minutes to 1 hour
    'also calculates which program section of the day it is
    endminute:
    hour = hour + 1
    minute = 0
    if hour = 24 then
    hour = 0
    end if

    if hour = 0 then hs = 0
    if hour = 3 then hs = 1
    if hour = 6 then hs = 2
    if hour = 9 then hs = 3
    if hour = 12 then hs = 4
    if hour = 15 then hs = 5
    if hour = 18 then hs = 6
    if hour = 21 then hs = 7

    goto selectpwm

    return

    selectpwm:
    select case hs
    case 0
    ledwhite = 128
    ledblue = 75
    case 1
    ledwhite = 75
    ledblue = 128
    case 2
    ledwhite = 25
    ledblue = 50
    case 3
    ledwhite = 75
    ledblue = 50
    case 4
    ledwhite = 192
    ledblue = 50
    case 5
    ledwhite = 255
    ledblue = 36
    case 6
    ledwhite = 255
    ledblue = 25
    case 7
    ledwhite = 224
    ledblue = 50
    end select
    return
    '====================================
    '====================================

    also with the hpwm, what is the ideal rate for the frequency, that 3rd variable.

    Cheers guys, let me know if I need to fix anything

    Tim.

  2. #2
    Join Date
    Mar 2003
    Location
    Commerce Michigan USA
    Posts
    1,166


    Did you find this post helpful? Yes | No

    Default

    emtark, instead of:

    if hour = 0 then hs = 0
    if hour = 3 then hs = 1
    if hour = 6 then hs = 2
    if hour = 9 then hs = 3
    if hour = 12 then hs = 4
    if hour = 15 then hs = 5
    if hour = 18 then hs = 6
    if hour = 21 then hs = 7

    why not:
    hs = hour / 3

    Dave Purola,
    N8NTA

  3. #3
    emtark's Avatar
    emtark Guest


    Did you find this post helpful? Yes | No

    Thumbs up Cheers,

    I want it to keep hs as a whole number.. i.e, for hours 0, 1 and 2, hs should be 1.. then for hours 3,4,5 hs will be 2. If this will work the same way i will change that.

    The two things im really worried about are the pause command and the hpwm.

    Will that set the pwm output on a specific port (do i have to define the port) to make my led's appear brighter/dimmer?

    Thanks again,

    Tim.

  4. #4
    Join Date
    Jul 2003
    Posts
    2,358


    Did you find this post helpful? Yes | No

    Default

    Your code is shot Tim.

    Your main loop is fine but... every second you GOTO (not GOSUB) to your endsecond section, which you have terminated with a RETURN.

    What's your worry with the PAUSE?

    A better way of keeping time would be...

    Pause 999
    PauseUS 1000

    That way, when you've built your program and you time it, and discover you're losing five minutes a day, you'll have a 1uS step per loop adjustment (giving adjustment steps of 86.4mS per day), rather than 1mS (giving adjustment steps of 86.4 seconds per day).

  5. #5
    emtark's Avatar
    emtark Guest


    Did you find this post helpful? Yes | No

    Default

    Hi all, thanks for the quick replies.

    I have made some changes, hopefully this looks better. I was also planning on developing this for the pic16f627a, not too worried if i use 2 pics, one for white, one for blue. Just need something that my programmer is compatible with. That specific pic isn't listed under the drop box near the menu, does that mean that it won't work?

    Also here is my updated code:
    [html]

    'define oscillator speed
    DEFINE osc 20


    'define dutycycle variable
    dutyCycle var byte


    'define clock variables
    minute var byte
    hour var byte
    second var byte
    hs var byte
    ledblue var byte
    ledwhite var byte

    '================================================= =====
    'start the main segment
    main:


    'add a second to the counter, and pause
    second = second + 1
    pause 999
    pauseus 1000

    'check to see if 60 seconds have passed
    if second = 60 then endsecond


    'HPWM Channel,Dutycycle,Frequency
    hpwm 1,ledblue, 5000

    'end of section, repeat main code.
    goto main
    '================================================= ======

    'click 60 seconds to 1 minute
    endsecond:
    minute = minute + 1
    second = 0
    if minute = 60 then endminute
    goto main


    'click 60 minutes to 1 hour
    'also calculates which program section of the day it is
    endminute:
    hour = hour + 1
    minute = 0
    if hour = 24 then
    hour = 0
    end if

    if hour = 0 then hs = 0
    if hour = 3 then hs = 1
    if hour = 6 then hs = 2
    if hour = 9 then hs = 3
    if hour = 12 then hs = 4
    if hour = 15 then hs = 5
    if hour = 18 then hs = 6
    if hour = 21 then hs = 7

    goto selectpwm

    selectpwm:
    select case hs
    case 0
    ledwhite = 128
    ledblue = 75
    case 1
    ledwhite = 75
    ledblue = 128
    case 2
    ledwhite = 25
    ledblue = 50
    case 3
    ledwhite = 75
    ledblue = 50
    case 4
    ledwhite = 192
    ledblue = 50
    case 5
    ledwhite = 255
    ledblue = 36
    case 6
    ledwhite = 255
    ledblue = 25
    case 7
    ledwhite = 224
    ledblue = 50
    end select

    goto main
    [/html]

    Hopefully it looks like it will work.

    Thanks again,

    Tim.

  6. #6
    Join Date
    Jan 2006
    Location
    Istanbul
    Posts
    1,185


    Did you find this post helpful? Yes | No

    Cool What about temp and OSC tolerance?

    Hi emtark,

    Make sure you do not put your pic circuit somewhere near the heater of your tank. If the temp changes couple of degrees per half an hour (or per hour), then tolerance (precision) of your OSC will change a lot and will provide a very fast running timer (or vice versa).

    May be you can give a long pause to every hour.

    Just a thought.
    "If the Earth were a single state, Istanbul would be its capital." Napoleon Bonaparte

  7. #7
    emtark's Avatar
    emtark Guest


    Did you find this post helpful? Yes | No

    Default

    Hi,

    the pic and oscillator will be sitting in a project box under the tank, in the drawer. In terms of coding, I can't seem to get the microcode studio to compile this for me. Can someone do a compilation, into ASM for a pic 16f627a, just so i can make sure the programming works. I'll spend tonight trying to figure out why it isn't compiling.

    Hopefully the code will be ok, and I can get the circuit going

    Cheers guys,

    Tim.

    btw: for those interested I have just added the water to the tank and it looks sweet!

  8. #8
    emtark's Avatar
    emtark Guest


    Did you find this post helpful? Yes | No

    Default

    OK I still can't get this to work.. I'm probably going to have to uninstall and reinstall these programs, what exactly do i need to compile this..

    MicroCode Studio complains about ICD or something, and not have PicBasic Pro 2.20 or higher.. where can I get these.

    Also if anyone has another quick once-over of the code can they see any errors or why it may not work.

    Thanks in advance,

    Tim.

  9. #9
    Join Date
    Jul 2003
    Posts
    2,358


    Did you find this post helpful? Yes | No

    Default

    Programs and Upgrades can be obtained from MeLabs... www.melabs.com

    Your compiler version is around five years or so old, it would be prudent to upgrade before posting any "my program doesn't work" messages as it's unlikely anyone else has versions that old still installed on their computer to compare against.

  10. #10
    emtark's Avatar
    emtark Guest


    Did you find this post helpful? Yes | No

    Default

    OK I deleted everything and i've completely started again. When I attempt to compile my code, im getting "Macro HPWM?TBW not found in macro file."

    Any help on this is appreciated

    oh btw: I'm using pic 16f627A

Similar Threads

  1. Reading in Manchester code
    By brid0030 in forum Code Examples
    Replies: 0
    Last Post: - 10th March 2009, 21:55
  2. How much code space do PBP statements use.
    By Darrel Taylor in forum Code Examples
    Replies: 5
    Last Post: - 13th February 2009, 21:31
  3. Loop with two motor and 2 sensors
    By MrRoboto in forum mel PIC BASIC
    Replies: 4
    Last Post: - 8th December 2008, 23:40
  4. Making Program Code Space your playground...
    By Melanie in forum Code Examples
    Replies: 15
    Last Post: - 19th July 2008, 08:26
  5. Re-Writing IF-THEN-AND-ENDIF code?
    By jessey in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 18th August 2006, 17:23

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts