My understanding of PixelSearch is it store one static value into the x/y variables even if that pixel is moving on screen. I don't know if PixelSearch "freezes" the screen into memory and then searches that, or if it will ask "in real time" "What is at pixel 1,1? Pixel 1,2? Pixel 1,3? Pixel 1,4? ... Pixel 1920,1080?"
You do have at least a 600 ms delay in your initial code before any pixels are sampled, which may be why you are getting undesirable results and thinking that the pixels aren't close enough, but when AHK samples, they may be close enough.
In your latest code in the reply above, you are using the
Continue command for reasons I'm not understanding. Continue will jump back to the top of the loop -- repeating the "P2x, P2y" PixelSearch command, if there was no Error in the previous one. The only way to ever search "P1x, P1y" (the second PixelSearch) is by getting
MsgBox, Error1 to trigger, which skips the else/continue lines.
Also note that if P2x happened to be ≤50, and then the second Pixelsearch reports an ErrorLevel, then P1x could be 0 or unchanged from last time giving a false result. You should consider an if statement:
if ((abs(P1x - P2x) <= 50) && !ErrorLevel) where the ! is the logical-not
operator and && is the AND operator, to make sure that that Pixelsearch did not result in an error.
I do admit I did not watch your recordings, so I likely have not given a complete answer.
Edit: I watched the videos. I'm not a fan of the PixelSearch logic of searching the same areas. I propose you search just one row of pixels (so same value for the y parameters in PixelSearch) for each of the moving and fixed (draining) bars. What I would do is search for the first dark pixel (background) of the fixed bar first, to indicate where the edge is. Then search for the moving bar's brighter color. The pixelsearch should move left to right, if you put the smaller X coordinate in the first pair of coordinates, so that you find the edge of the draining bar.
I don't know how fast PixelSearch runs, but I would actually consider making your search for the moving bar dynamic by using the result for the fixed bar. If you find the fixed bar at pixel X=200, then make it so your search for the moving bar only fits into X
1=150 to X
2=250. This can be dynamically done by using something like
Code: Select all
Loop
{
PixelSearch, P1x, P1y, *Fixed Bar Coordinates*, *Color*, *Variation*
If ErrorLevel
continue ; something went wrong, search again
PixelSearch, P2x, P2y, P1x-50, yCoord, P1x+50, yCoord, *Color*, *Variation*
If ErrorLevel
continue ; start from the top, we didn't find the moving bar in range
else
MsgBox The moving bar was close to the fixed bar!
}
We could actually reduce the code just a bit by reversing the logic at the end:
Code: Select all
Loop
{
PixelSearch, P1x, P1y, *Fixed Bar Coordinates*, *Color*, *Variation*
If ErrorLevel
continue ; something went wrong, search again
PixelSearch, P2x, P2y, P1x-50, yCoord, P1x+50, yCoord, *Color*, *Variation*
If !ErrorLevel
MsgBox The moving bar was close to the fixed bar!
}
The loop would have "Continued" if there was an ErrorLevel due to not finding the draining bar.
Code is untested. Diagnostics should include using MsgBox %ErrorLevel% to know what error happened according to
PixelSearch.