CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Hysteresis for two thresholds
Goto page Previous  1, 2, 3
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
SuperDave



Joined: 22 May 2008
Posts: 63
Location: Madison, TN

View user's profile Send private message Visit poster's website

PostPosted: Sun Oct 26, 2014 6:02 am     Reply with quote

myAdc = reading of adc
myState is remembered variable for the current condition (only three values)
myPins is pic pins to change to set the current condition
(ps. be sure you have diodes across the relay coils!!!)
done without lots of elses for reading clarity

const myLow = desired value (eg. 1)
hysteresis is hidden here
const myMidlow = desired value (eg. 2)
const myMidhigh = desired value(eg. 9)
and here
const myHigh = desired value (eg. 10)
const stHigh = 1
const stMid = 2
const stLow = 3

Be sure to initialize myPins and myState to your desired starting condition.

If myAdc is greater than myHigh and myState is not stHigh
set myPins to the stHigh condition
change myState to stHigh
end

if ADC is greater than myMidlow and ADC is less than myMidhigh and myState is not myMid
set pins to the stMid condition
change myState to myMid
end

if ADC is less than myLow and myState is not stLow
set pins to the stLow condition
change myState to stLow
end

no need for individual hysteresis bands. adc values in between myHigh and myMidhigh as well between myLow and myMidlow will cause no change.
MDM1985



Joined: 16 Oct 2014
Posts: 14

View user's profile Send private message

PostPosted: Sun Oct 26, 2014 11:03 am     Reply with quote

SuperDave wrote:
myAdc = reading of adc
myState is remembered variable for the current condition (only three values)
myPins is pic pins to change to set the current condition
(ps. be sure you have diodes across the relay coils!!!)
done without lots of elses for reading clarity

const myLow = desired value (eg. 1)
hysteresis is hidden here
const myMidlow = desired value (eg. 2)
const myMidhigh = desired value(eg. 9)
and here
const myHigh = desired value (eg. 10)
const stHigh = 1
const stMid = 2
const stLow = 3

Be sure to initialize myPins and myState to your desired starting condition.

If myAdc is greater than myHigh and myState is not stHigh
set myPins to the stHigh condition
change myState to stHigh
end

if ADC is greater than myMidlow and ADC is less than myMidhigh and myState is not myMid
set pins to the stMid condition
change myState to myMid
end

if ADC is less than myLow and myState is not stLow
set pins to the stLow condition
change myState to stLow
end

no need for individual hysteresis bands. adc values in between myHigh and myMidhigh as well between myLow and myMidlow will cause no change.


Thank you for your pseudo code. I will try it. But could you tell me if my code is correct? In this way I can fix my mistakes. Thank you.
MDM
SuperDave



Joined: 22 May 2008
Posts: 63
Location: Madison, TN

View user's profile Send private message Visit poster's website

PostPosted: Sun Oct 26, 2014 8:17 pm     Reply with quote

I suppose your code could work though I don't have the patience to work out all the possibilities. It seems to me to be needlessly complex and very difficult to understand. More importantly it would be very difficult to debug and maintain. I especially don't understand why having made a measurement and decoded a state you then make more measurements when implementing a state.

The code will execute far faster than a mechanical relay will change and settle. If nothing else is going on then a delay statement is probably a good idea. If other stuff is going on then this process should be called by a timer based interrupt. How fast depends on what you are trying to do and what else needs to be done. For example if the relays are turning on a heater and the temperature is being measured by the ADC then calling this process more than once a second is silly.

BTW, my pseudo code, created very early in the morning is not consistent or clear about the ADC measurement which should occur only once and be stored for all the subsequent comparisons. Otherwise noise or a rapidly changing reading could cause some confusion. Any comparison should ideally compare stable values. In addition, in the code the ADC measurement will almost certainly take the bulk of the execution time and thus should be done a minimum number of times, like once.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page Previous  1, 2, 3
Page 3 of 3

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group