Jump to content

Detect variable state change 1 to 0 and from 0 to 1


Recommended Posts

Hello, 

I have the following code:

#include <CommMG.au3>
#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>


; -- Created with ISN Form Studio 2 for ISN AutoIt Studio -- ;
#include <StaticConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#Include <GuiButton.au3>


Global $bs
Global $a
Global $b
Global $CMPort = 3
Global $CmBoBaud = 9600
Global $sportSetError = ''
Global $CmboDataBits = 8
Global $CmBoParity = "none"
Global $CmBoStop = 1
Global $setflow = 2
_CommSetPort($CMPort, $sportSetError, $CmBoBaud, $CmboDataBits, $CmBoParity, $CmBoStop, $setflow)
If @error Then
MsgBox(16,"Error!","Can't connect to Arduino on port - "&$CMPort)
Exit
EndIf
_CommSetRTS(0)
_CommSetDTR(0)

While 1

global $msg = _Commgetstring()



ConsoleWrite ($msg)
Wend

 

$msg received from serial port, can be 81 or 80. In default mode is 80.

 

 I want that when the value goes from 80 to 81 run a function and when the value goes from 81 to 80 run another function.

How I can do this please?

Link to comment
Share on other sites

You already have an If statement in your code, do the same thing with your$msg variable. Read the help file for  If/Then/Elseif/Else

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

I'd do something like this:

$LastMsg = 0

While 1

    $CurrentMsg = UreFunction()

    If $CurrentMsg <> $LastMsg Then
        If $CurrentMsg = 80 Then
            ; do something
            $LastMsg = $CurrentMsg
        ElseIf $CurrentMsg = 81 Then
            ; do something else
            $LastMsg = $CurrentMsg
        EndIf
    EndIf
WEnd

 

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

Hello,

 

Tried 

While 1

$CurrentMsg = _Commgetstring()
   If $CurrentMsg <> $LastMsg Then
        If $CurrentMsg = 80 Then
           beep (1500, 500) ;first function
            $LastMsg = $CurrentMsg
        ElseIf $CurrentMsg = 81 Then
            beep (1200, 700)
            $LastMsg = $CurrentMsg
        EndIf
    EndIf
WEnd

And is not working so good. By default currentmsg is 80 so all the time first function is made beep (1500, 500).

I want to call first function only once when there is a change from 80 to 81.

Also when there is a change from 81 to 80, to call only once the second function.

 

How I can do this please?

Link to comment
Share on other sites

The way it is written, it will only call the first function if $CurrentMsg changes from its last value, so it can't be calling the first function all the time as written.

It will only run the first function when $CurrentMsg and $LastMsg are different, and then only when $CurrentMsg is equal to 80, it will only run the second function when $CurrentMsg and $LastMsg are different, and then only when $CurrentMsg is equal to 81. If it runs the first function, then lastmsg is going to equal 80, so if currentmsg is also 80, it skips the functions all together.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

Rony asked specifically for changes from 80 to 81 or from 81 to 80, Brewman's code looks for changes from anything to 81 or from anything to 80. So Brewmans code would only work as expected if the message is always either 80 or 81 and never something else. Synapsee's code should work as requested.

Roses are FF0000, violets are 0000FF... All my base are belong to you.

Link to comment
Share on other sites

tried this from synapsee but still not working ok

While 1

$CurrentMsg = _Commgetstring()
consolewrite ($CurrentMsg &@CRLF  )
 If $CurrentMsg = 81 and $LastMsg = 80 Then
        beep (500, 500)
    Else
 beep (800, 300)
    EndIf

    If $CurrentMsg = 80 and $LastMsg = 81 Then
        beep (1500, 500)
    Else
 beep (1800, 300)
    EndIf

    $LastMsg = $CurrentMsg

WEnd

Default value of currentmsg is 80. When currentmsg changes to 81 I want to run first function once. When 81 changes back to 80 I want to run second function once and so one.

What is the problem?

Link to comment
Share on other sites

; for simule commmsg
Global $Array[] = [80,80,80,80,80,81,81,81,81,81,81,80,80,80,80,80,80,80,81,80,81,80,81,80]

; important
Global $LastMsg

; for simule commmsg
$i = 0

While 1

    ; for simule commmsg
    If $i > Ubound($Array-1) Then Exit
    $i += 1

    $CurrentMsg = $Array[$i]; for simule commmsg
    ;$CurrentMsg = _Commgetstring()

    consolewrite($CurrentMsg & @CRLF)

    If $CurrentMsg = 81 and $LastMsg = 80 Then
        consolewrite("Now81Before80()" & @CRLF)
        ;Now81Before80()
    EndIf

    If $CurrentMsg = 80 and $LastMsg = 81 Then
        consolewrite("Now80Before81()" & @CRLF)
        ;Now80Before81()
    EndIf

    $LastMsg = $CurrentMsg

    Sleep(1000); for easy look result on scite console

WEnd

I have do my best ^^

Link to comment
Share on other sites

Rony: the problem in your last example is that the two "else" parts are ALWAYS run, in EVERY iteration of the loop, if the state did not change from 80 to 81 or from 81 to 80:

While 1

    $CurrentMsg = _Commgetstring()
    ConsoleWrite($CurrentMsg & @CRLF)
    If $CurrentMsg = 81 And $LastMsg = 80 Then
        Beep(500, 500)
    Else
        ; note: this "else" block will be executed EVERY time that the state did not change from 80 to 81, i.e. almost always
        Beep(800, 300)
    EndIf

    If $CurrentMsg = 80 And $LastMsg = 81 Then
        Beep(1500, 500)
    Else
        ; note: this "else" block will be executed EVERY time that the state did not change from 81 to 80, i.e. almost always
        Beep(1800, 300)
    EndIf

    $LastMsg = $CurrentMsg

WEnd

Your example fixed would look more like:

While 1

    $CurrentMsg = _Commgetstring()
    ConsoleWrite($CurrentMsg & @CRLF)
    If $CurrentMsg = 81 And $LastMsg = 80 Then
        Beep(500, 500)
    ElseIf $CurrentMsg = 80 And $LastMsg = 81 Then
        Beep(1500, 500)
    Else
        ; do nothing, because neither one of your specific state changes happened this loop iteration
    EndIf

    $LastMsg = $CurrentMsg
WEnd

 

Roses are FF0000, violets are 0000FF... All my base are belong to you.

Link to comment
Share on other sites

19 hours ago, rony2006 said:

$msg received from serial port, can be 81 or 80. In default mode is 80.

 

1 hour ago, SadBunny said:

Rony asked specifically for changes from 80 to 81 or from 81 to 80, Brewman's code looks for changes from anything to 81 or from anything to 80.

As stated, the default value is 80, so the code will work as requested. It was purpose built for the requested actions, with the information provided.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

5 minutes ago, BrewManNH said:

As stated, the default value is 80, so the code will work as requested. It was purpose built for the requested actions, with the information provided.

I get that, I just have trouble "believing" the information provided about that given the rest of the story. I.e. it's probably better to make sure that the code does some extra checking (as in check both the previous and the new value) even though that should not be necessary if the description is accurate.

Roses are FF0000, violets are 0000FF... All my base are belong to you.

Link to comment
Share on other sites

Thanks to all!

 

After putting a sleep in the code from SadBunny, everything is working ok.

 

Global $LastMsg
While 1
sleep (100)
    $CurrentMsg = _Commgetstring()
    ConsoleWrite($CurrentMsg & @CRLF)
    If $CurrentMsg = 1 And $LastMsg = 0 Then
        Beep(500, 500)
    ElseIf $CurrentMsg = 0 And $LastMsg = 1 Then
        Beep(1500, 500)
    Else
        ; do nothing, because neither one of your specific state changes happened this loop iteration
    EndIf

    $LastMsg = $CurrentMsg
WEnd

 

Link to comment
Share on other sites

rony2006,

An alternative way of looking at the problem (albeit late to the party).  This technique offers the advantage of being easily extensible without logic changes and eliminates the spaghetti that nested if...then constructs can become...

#include <date.au3>

Global $curmsg, $prevmsg

;
; Driver loop for function calls.
; Function name must be an underscore followed by whatever code you wish to act on.
;

While 1

    $curmsg = _GetNum()

    If $curmsg = $prevmsg Then ContinueLoop

    Call('_' & $curmsg)
    If @error <> 0xdead Then $prevmsg = $curmsg

    Sleep(100)

WEnd

;
; Function calls for codes to act on
;

Func _0()
    ConsoleWrite('!Firing _' & $curmsg & ' ' & _Now() & @CRLF)
EndFunc   ;==>_0

Func _1()
    ConsoleWrite('!Firing _' & $curmsg & ' ' & _Now() & @CRLF)
EndFunc   ;==>_1

Func _5()
    ConsoleWrite('!Firing _' & $curmsg & ' ' & _Now() & @CRLF)
EndFunc   ;==>_5

Func _9()
    ConsoleWrite('!Firing _' & $curmsg & ' ' & _Now() & @CRLF)
EndFunc   ;==>_9

;
; test function
;

Func _GetNum()
    Return Random(1, 9, 1)
EndFunc   ;==>_GetNum

Note - I had to use the dreaded "call" statement because I could not figure out how to create a function name without the variable turning into a string.

kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...