Hi PilotinControl,
You may apply hystersis. The state will be set to 1 if the value goes above three times the upper limit or it will be set to 0 if the value goes below lower limit three times.
#!/usr/bin/perl
use strict;
use warnings;
my $state = 0; # 0 - light off, 1 - light on
my $uplim = 102; # upper limit
my $lolim = 98; # lower limit
my $n = 3; #
my $counter = 0;
# Set initial state
# This part may need more work in order to avoid eventual starting gli
+tch.
my $value = <DATA>;
chomp $value;
if ($value > (($uplim + $lolim) / 2.0)) {
$state = 1;
} else {
$state = 0;
}
# work continuously
while ($value = <DATA>) {
chomp $value;
# Adjust counter
if ($value > $uplim) {
if ($counter < 0) {
$counter = 0;
} else {
$counter++;
}
} elsif ($value < $lolim) {
if ($counter > 0) {
$counter = 0;
} else {
$counter--;
}
} else {
$counter = 0;
}
# Set state
if ($counter > $n) {
$state = 1;
} elsif ($counter < -$n) {
$state = 0;
}
# Print
my ($below, $between, $above);
$below = $between = $above = '---';
if ($value > $uplim) {
$above = $value;
} elsif ($value < $lolim) {
$below = $value;
} else {
$between = $value;
}
printf "state: %d cnt: %+3d %3s<%3d>%3s<%3d>%3s\n", $state, $counter
+, $below, $lolim, $between, $uplim, $above;
}
__DATA__
100
101
102
103
104
105
106
105
104
103
102
101
100
99
98
97
96
95
94
101
102
103
103
102
103
103
104
104
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
|
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.
|
|