Writing code for a feedback loop


Closed Thread
Results 1 to 2 of 2
  1. #1
    Join Date
    Oct 2005
    Location
    New Jersey
    Posts
    425

    Default Writing code for a feedback loop

    I'm having some difficulty writing a routine for a feedback loop. What I need to do is read a value taken from the network. Then I adjust the PWM based upon the value. In laymans terms, it would look like this:
    Code:
    IF TEMP > 105 AND TEMP < 170 THEN        'IF TEMP IS IN THE NORMAL RANGE, WE FORCE IT. OUTSIDE NORMAL RANGE, WE DON'T TOUCH IT.
                FORCE_FLAG=1                          'THE FIRST TIME THIS ROUTINE IS ENTERED
                DUTYCYCLE=(170-TEMP/3)+222    'THIS GETS ME IN THE BALLPARK
                FORCE_FLAG=0                           'ROUTINE HAS BEEN ENTERED SO THE FLAG IS NOW RESET AND 
                                                               'THIS ROUTINE WON'T BE ENTERED AGAIN UNTIL THE TEMP IS OUTSIDE THE NORMAL RANGE
            ELSE
                FORCE_FLAG=1                          'THE NEXT TIME THROUGH, WE NEED TO CHECK THE INITIAL ROUTINE    
                DUTYCYCLE=0                           'THE TEMP IS OUTSIDE NORMAL VALUES SO WE DON'T TOUCH IT. 
            ENDIF
            
            IF FORCE_FLAG=0 THEN                  'THE INITAL ROUTINE WAS ENTERED SO NOW WE NEED TO FINE TUNE IT
    This is where I'm stuck. Between 105 and 170, the PWM range will be approximately 22 steps (222 at 170 and 244 at 105). In the first routine I got the value close but how do I adjust the PWM signal without over or undershooting the perfect value?

  2. #2
    Join Date
    Nov 2007
    Location
    West Covina, CA
    Posts
    219


    Did you find this post helpful? Yes | No

    Default Re: Writing code for a feedback loop

    I may be over simplifying it but, if the TEMP value is within the adjustment range of 105 and 170, it seems that the IF/THEN condition would fine tune your PWM signal already.
    I also notice the DUTYCYCLE math needed the parentheses moved.

    Code:
    IF TEMP > 105 AND TEMP < 170 THEN
        DUTYCYCLE = (170-TEMP)/3 + 222
    ELSE 
        DUTYCYCLE = 0
    ENDIF
    Louie

Similar Threads

  1. Issues with a feedback loop.
    By jmgelba in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 11th June 2012, 18:54
  2. Writing good/proper code
    By Bill Legge in forum mel PIC BASIC Pro
    Replies: 11
    Last Post: - 22nd July 2009, 00:55
  3. Code entering endless loop
    By Blackhawk in forum mel PIC BASIC
    Replies: 11
    Last Post: - 26th November 2006, 09:12
  4. Re-Writing IF-THEN-AND-ENDIF code?
    By jessey in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 18th August 2006, 17:23
  5. Writing code for battery operated projects
    By jessey in forum mel PIC BASIC Pro
    Replies: 15
    Last Post: - 16th June 2005, 03:39

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