0

Here is something I am trying to do:

A is a value that is constantly changing, and B is a fixed value

  1. compare A and B every 5 seconds
  2. if A > B, do something
  3. if A < B, do something else
  4. but if the current compared result is the same as the previous one (like if the current result is A > B, and the previous result is also A > B), do nothing until the result changes.
  5. repeat

I really don’t know what to do with the 4th one, could somebody give me a hint?

Huge thanks

ckx_9199
  • 3
  • 1
  • Welcome to [SO](https://stackoverflow.com). Check [\[SO\]: How do I ask a good question?](https://stackoverflow.com/help/how-to-ask) or [\[SO\]: How to create a Minimal, Reproducible Example (reprex (mcve))](https://stackoverflow.com/help/minimal-reproducible-example) for more asking related details. Also, [\[JonSkeet.CodeBlog\]: WRITING THE PERFECT QUESTION](https://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question) might be a good point to start. – CristiFati Aug 23 '22 at 07:32
  • Use threading in python, https://docs.python.org/3/library/threading.html#timer-objects – Sidharth Mudgil Aug 23 '22 at 07:37
  • Does this answer your question? [What is the best way to repeatedly execute a function every x seconds?](https://stackoverflow.com/questions/474528/what-is-the-best-way-to-repeatedly-execute-a-function-every-x-seconds) – Sidharth Mudgil Aug 23 '22 at 07:39
  • I think https://stackoverflow.com/a/474543/16177121 is the answer you're looking for – Sidharth Mudgil Aug 23 '22 at 07:40

4 Answers4

1

As you have not mentioned any language preference, I am using python here.

import time
c = time.time()

if A>B:
    state = True
    ''' Do the initial action for this state'''
elif A<B:
    state = False
    ''' Do the initial action for this state'''
while True:
    if (time.time() - c) >= 5:
        c = time.time()
        A = check_A() # some dummy function for illustration purpose
        B = check_B() # some dummy function for illustration purpose
        if A>B:
            if not state:
                state = True
                ''' Do something that you like '''
        elif A<B:
            if state:
                state = False
                ''' Do something that you like '''

Here I have assumed that you do not want anything to happen when when A==B. The logic here that unless the state changes there will be no action for that particular state.

If here you do not want your code to be continuously running then you can use time.sleep(SLEEP_TIME).

import time

if A>B:
    state = True
    ''' Do the initial action for this state'''
elif A<B:
    state = False
    ''' Do the initial action for this state'''
while True:
    time.sleep(5)
    A = check_A() # some dummy function for illustration purpose
    B = check_B() # some dummy function for illustration purpose
    if A>B:
        if not state:
            state = True
            ''' Do something that you like '''
    elif A<B:
        if state:
            state = False
            ''' Do something that you like '''
0

Store the previous comparision in a variable.

previousComparison = '';
    while True:
     time.sleep(5) # Sleep for 5 seconds
     if a > b and previousComparision != 'AIsGreater':
      #do 2.
      previousComparision = 'AIsGreater'
     if b > a and previousComparision = 'BIsGreater':
      #do 3.
      #previousComparision = 'BIsGreater'
S4RUUL
  • 141
  • 1
  • 3
0

You can just remember the previous value of B and compare it as well to determine if you need to do something

def update(val):
   return (val + 7) % 1000 # example

A = 500
B = 0
previous_B = 100000

while True:
    previous_B = B
    B = update(B)
    if previous_B < A and B > A:
        print(B, 'Do something')
    elif previous_B > A and B < A:
        print(B, 'Do something else')
    
Alexey S. Larionov
  • 6,555
  • 1
  • 18
  • 37
0

In order to do this, you can keep track of the previous result in an additional variable

import random
previous = None
a = 10
b = 2

i = 0
while(i < 20):
    print (a, b)
    if (a > b and previous != False):
        print('do something')
    elif (a < b and previous == False):
        print('do something else')
    previous = a < b
    b = random.randint(0, 20)

    i += 1
Bram
  • 21
  • 5