Need Help with a Loop Counter...

Technical support and scripting issues

Moderators: JRL, Dorian (MJT support)

Post Reply
rjw524
Pro Scripter
Posts: 104
Joined: Wed May 09, 2012 9:45 pm
Location: Michigan

Need Help with a Loop Counter...

Post by rjw524 » Mon May 23, 2016 3:58 am

Hi,

I'm trying to create a loop that does the following:

Compare the screen captured image of an area on the screen to later images of the same area.

If the images are similar enough then the loop counter would increase by 1.
If the images are not similar enough, the loop counter would start over at 0.
When the images were similar on say...20 consecutive iterations, the macro could move forward.

So, if %k% got to say...17 and then the images weren't the same, the counter (i.e. %k%) would go back to 0 and start over until the condition was satisfied 20 consecutive times.

Code: Select all

ScreenCapture>1168,755,1190,815,C:\MyDocuments\Image1.bmp

//...do some stuff...

//start the loop

Let>k=0
repeat>k
Let>k=k+1
ScreenCapture>1168,755,1190,815,C:\MyDocuments\Image2.bmp
wait>0.1
CompareBitmaps>C:\MyDocuments\Image1.bmp,C:\MyDocuments\Image2.bmp,nPercent
If>%nPercent%>90
wait 0.1
Add>k,1
Else
Let>k=0
EndIf
until>k=20
I've tried this a number of different ways with no luck. The code posted above is simply my latest attempt.

The usual problem I run into is that the counter runs 20 iterations regardless of whether the "if" condition has been satisfied or not.

Any help on this would be greatly appreciated!

Thanks!

R.J.

hagchr
Automation Wizard
Posts: 331
Joined: Mon Jul 05, 2010 7:53 am
Location: Stockholm, Sweden

Re: Need Help with a Loop Counter...

Post by hagchr » Mon May 23, 2016 8:07 am

Hi, one thing I note is that you are incrementing k in two places, ie. double, so one can be removed. I made a quick example based on your code and it seems to work on my machine. One thing I also added was SetFocus> before each ScreenCapture>. Hope it helps.

Code: Select all

//Open forum post and refresh
ExecuteFile>https://www.mjtnet.com/forum/viewtopic.php?f=2&t=9085
Wait>1
Let>WIN_USEHANDLE=1
GetActiveWindow>strTitle,nXPos,nYPos,,
SetFocus>strTitle
Let>WIN_USEHANDLE=0
Wait>0.1
Press>F5

Let>file0=%USERDOCUMENTS_DIR%\Image1.bmp
Let>file1=%USERDOCUMENTS_DIR%\Image2.bmp
ScreenCapture>1168,755,1190,815,file0

//...do some stuff...
//start the loop

Let>res=

Let>k=0
repeat>k
//    Let>k=k+1
    
    Let>WIN_USEHANDLE=1
    SetFocus>strTitle
    Let>WIN_USEHANDLE=0
    
    ScreenCapture>1168,755,1190,815,file1
    
    wait>0.1
    CompareBitmaps>file0,file1,nPercent

    If>%nPercent%>90
        wait 0.1
        Add>k,1
    Else
        Let>k=0
    EndIf
    
    Let>res=%res% %k%

until>k=20

MDL>Finished. k dev:%res%

rjw524
Pro Scripter
Posts: 104
Joined: Wed May 09, 2012 9:45 pm
Location: Michigan

Re: Need Help with a Loop Counter...

Post by rjw524 » Mon May 23, 2016 1:23 pm

hagchr,

hey, this is great! thanks so much!

I hope you don't mind me asking a question or two of you here in an attempt to understand what I did wrong...

So when I did the following:

Code: Select all

Let>k=k+1
and later followed up with the

Code: Select all

Add>k,1
that was essentially adding 2 to the counter instead of just 1? Is that correct?

Also, even if my code was doing that, why when the "If" condition failed wouldn't the counter revert back to zero?

Thanks!

hagchr
Automation Wizard
Posts: 331
Joined: Mon Jul 05, 2010 7:53 am
Location: Stockholm, Sweden

Re: Need Help with a Loop Counter...

Post by hagchr » Mon May 23, 2016 1:54 pm

Hi, yes you add one in the beginning of the big loop and then also in the small loop, ie twice so it increments 0,2,4, ... The other thing I changed was to make sure to set focus on the right window, otherwise there is always the risk that you are comparing different bitmaps than you think.

If you step through the code (pressing F8) then you can follow how the variables change at each step in the Watch List on the left side. You also see how I use the variable res to store the individual k values to see how they develop. You could also add the nPercent to see how it develops etc (if nPercent is always 100 then you are probably focusing on the wrong window). Hope it helps.

rjw524
Pro Scripter
Posts: 104
Joined: Wed May 09, 2012 9:45 pm
Location: Michigan

Re: Need Help with a Loop Counter...

Post by rjw524 » Mon May 23, 2016 4:30 pm

hagchr wrote:Hi, yes you add one in the beginning of the big loop and then also in the small loop, ie twice so it increments 0,2,4, ... The other thing I changed was to make sure to set focus on the right window, otherwise there is always the risk that you are comparing different bitmaps than you think.

If you step through the code (pressing F8) then you can follow how the variables change at each step in the Watch List on the left side. You also see how I use the variable res to store the individual k values to see how they develop. You could also add the nPercent to see how it develops etc (if nPercent is always 100 then you are probably focusing on the wrong window). Hope it helps.
hagchr, this was one of THE most educational posts I've read on here.

Your use of the variable "res" to store the incremental k values and how you explained the use and placement of the "k=k+1" is a "light bulb" moment. Also, the use of the watch list is just something I never knew about for some reason or didn't fully understand at the very least.

You didn't just simply list code that works, you explained WHY that code works and that's so awesome.

So many of my macros have to do these types of if/then type counter loops and I've never been able to structure them correctly until now. This kind of thing makes me want to a week to restructure all of my previous macros.

Between you, JRL and Marcus I have come light years ahead in the last year!

Thanks again!

RJ

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts