Loops 在具有多个if条件的循环中更改布尔值

Loops 在具有多个if条件的循环中更改布尔值,loops,if-statement,boolean,state-machine,Loops,If Statement,Boolean,State Machine,在每秒执行的程序循环中,我有4个与名为ValveActive(从1变为4)的变量相关联的if条件。 如果条件为真,则每个持续5分钟。在每个if条件中,我需要在定义的时间内将布尔值PortSet设置为true,然后将其设置为false。我希望当循环重复时,打开布尔值的过程不要重复。布尔值表示打开一个继电器,然后关闭它,我只希望在每个唯一的ValveActive状态期间发生一次 Start of loop If ValveActive=1 PortSet(9

在每秒执行的程序循环中,我有4个与名为
ValveActive
(从1变为4)的变量相关联的
if
条件。 如果条件为真,则每个
持续5分钟。在每个
if
条件中,我需要在定义的时间内将布尔值
PortSet
设置为
true
,然后将其设置为
false
。我希望当循环重复时,打开布尔值的过程不要重复。布尔值表示打开一个继电器,然后关闭它,我只希望在每个唯一的
ValveActive
状态期间发生一次

Start of loop

If ValveActive=1
                    PortSet(9,1) 'Activate port
            'Do something 
                    Delay (1,25,mSec)
            PortSet(9,0)          'Deactivate port

ElseIf ValveActive=2
              PortSet(9,1)
            'Do something 
            Delay (1,25,mSec)
            PortSet(9,0)

ElseIf ValveActive=3
              PortSet(9,1)
            'Do something
            Delay (1,25,mSec)
             PortSet(9,0)

Else
              PortSet(9,1)
            'Do something  
            Delay (1,25,mSec)
             PortSet(9,0)

EndIf

Loop

我曾尝试将循环外的布尔值设置为false,然后在循环内将其设置为true,但这不适用于多个if条件。如何实现这一点?

创建一个新变量,例如
PreviousValveActive
,它通过循环记住上一次的
ValveActive
值。然后使用
PreviousValveActive
作为测试,以确定是否执行在每个状态下只应发生一次的操作

Start of loop

If ValveActive=1
    If PreviousValveActive != ValveActive
            PreviousValveActive = ValveActive
            PortSet(9,1) 'Activate port
            'Do something 
            Delay (1,25,mSec)
            PortSet(9,0)          'Deactivate port
    EndIf

ElseIf ValveActive=2
    If PreviousValveActive != ValveActive
            PreviousValveActive = ValveActive
            PortSet(9,1)
            'Do something 
            Delay (1,25,mSec)
            PortSet(9,0)
    EndIf

ElseIf ValveActive=3
    If PreviousValveActive != ValveActive
            PreviousValveActive = ValveActive
            PortSet(9,1)
            'Do something
            Delay (1,25,mSec)
            PortSet(9,0)
    EndIf

Else
    If PreviousValveActive != ValveActive
            PreviousValveActive = ValveActive
            PortSet(9,1)
            'Do something  
            Delay (1,25,mSec)
            PortSet(9,0)
    EndIf

EndIf

Loop

这很有效。我在循环外声明了PreviousValveActive并将其设置为0。谢谢。