Loop problem ? Topic is solved
Posted: 19 Apr 2018, 19:57
Hello to all,
I have a new project. My lack of knowledge prevents me from completing it.
So I need you!
PS: I did a lot of research, I do not come here without having done anything and wait for you to do it for me,
i'm here to learn / understand.
The project :
I will try to be clear. Imagine, you're facing your screen, and there is a letter or a number
displayed. This number (or letter) changes quickly for another. And you want every time
as this number changes, a precise action is taken.
Constraints :
Well yes, there is. Otherwise, it would not even be a challenge for a beginner like me
1) We know the color of this number: white. BUT the background constantly changes color!
2) The time:
- The number changes quickly and the interval is random. To give yourself a point of reference: let's say that it changes every 50ms on average.
- My program should detect the number change and start the action in less than 11ms. (It can be a little more)
What I did:
- after studying:
I can forget the "search for images" since the background changes constantly, my image on the screen is never the same;
I can forget the "image recognition", already because it is not very good and because it is too slow for it to recognize I think,
So we go for a good handmade script.
Well, I have the idea to make the change of number detection by looking at certain pixels. Say 3 pixels, arranged at key locations.
On all the possible numbers, there will ALWAYS be at least 1 pixel that will change to white or lose its white color!
If I had to explain to you what my program should do:
1) I'm looking at 3 pixels
2) I keep in mind who are white, who are not white.
3) I look at my 3 pixels, and I constantly compare their current colors to those I have in memory.
4) As soon as I notice that one of them (or more) has lost its white or recovered a white value
5) I keep in memory the new colors for these 3 pixels (related to the white always).
5) I'm doing a specific action !!
6) we go back on the 3)
Well, here is the script that I could perform for now: (ie, for the need of the test, the color listened is the black, and the reaction is to write "ab")
///
#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn ; Enable warnings to assist with detecting common errors.
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.
VarPix1 := false
VarPix2 := false
VarPix3 := false
ActionReadyTo := false
MyRoutine:
PixelGetColor, Pixel1, 1060, 448
PixelGetColor, Pixel2, 1076, 361
PixelGetColor, Pixel3, 1087, 454
if (Pixel1 = 0x000000) && (VarPix1 = false)
{
ActionReadyTo := true
VarPix1 := !VarPix1
}
else if (Pixel1 != 0x000000) && (VarPix1 = true)
{
ActionReadyTo := true
VarPix1 := !VarPix1
}
else if (Pixel2 = 0x000000) && (VarPix2 = false)
{
ActionReadyTo := true
VarPix2 := !VarPix2
}
else if (Pixel2 != 0x000000) && (VarPix2 = true)
{
ActionReadyTo := true
VarPix2 := !VarPix2
}
else if (Pixel3 = 0x000000) && (VarPix3 = false)
{
ActionReadyTo := true
VarPix3 := !VarPix3
}
else if (Pixel3 != 0x000000) && (VarPix3 = true)
{
ActionReadyTo := true
VarPix3 := !VarPix3
}
else if ActionReadyTo
{
send, ab
ActionReadyTo := false
}
Goto, MyRoutine
///
The problem with this script is that:
1) It looks like, from time to time, between 2 numbers, the script sends to much "ab".
So :
- either because it sends me as much as "ab" that pixels change by number.
- or if my script seems to you clean, it is that the color of the screen changes, briefly, without I realize it, between 2 numbers from time to time.
2) It is surely not optimized; there must be simpler and faster for the program to run, but with my little knowledge : that's all I can do.
Can someone tell me if my code is "ok"? And also tell me how it can be optimized ?
If you would have adopted another strategy for the basic project: which one?
I have a new project. My lack of knowledge prevents me from completing it.
So I need you!
PS: I did a lot of research, I do not come here without having done anything and wait for you to do it for me,
i'm here to learn / understand.
The project :
I will try to be clear. Imagine, you're facing your screen, and there is a letter or a number
displayed. This number (or letter) changes quickly for another. And you want every time
as this number changes, a precise action is taken.
Constraints :
Well yes, there is. Otherwise, it would not even be a challenge for a beginner like me
1) We know the color of this number: white. BUT the background constantly changes color!
2) The time:
- The number changes quickly and the interval is random. To give yourself a point of reference: let's say that it changes every 50ms on average.
- My program should detect the number change and start the action in less than 11ms. (It can be a little more)
What I did:
- after studying:
I can forget the "search for images" since the background changes constantly, my image on the screen is never the same;
I can forget the "image recognition", already because it is not very good and because it is too slow for it to recognize I think,
So we go for a good handmade script.
Well, I have the idea to make the change of number detection by looking at certain pixels. Say 3 pixels, arranged at key locations.
On all the possible numbers, there will ALWAYS be at least 1 pixel that will change to white or lose its white color!
If I had to explain to you what my program should do:
1) I'm looking at 3 pixels
2) I keep in mind who are white, who are not white.
3) I look at my 3 pixels, and I constantly compare their current colors to those I have in memory.
4) As soon as I notice that one of them (or more) has lost its white or recovered a white value
5) I keep in memory the new colors for these 3 pixels (related to the white always).
5) I'm doing a specific action !!
6) we go back on the 3)
Well, here is the script that I could perform for now: (ie, for the need of the test, the color listened is the black, and the reaction is to write "ab")
///
#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn ; Enable warnings to assist with detecting common errors.
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.
VarPix1 := false
VarPix2 := false
VarPix3 := false
ActionReadyTo := false
MyRoutine:
PixelGetColor, Pixel1, 1060, 448
PixelGetColor, Pixel2, 1076, 361
PixelGetColor, Pixel3, 1087, 454
if (Pixel1 = 0x000000) && (VarPix1 = false)
{
ActionReadyTo := true
VarPix1 := !VarPix1
}
else if (Pixel1 != 0x000000) && (VarPix1 = true)
{
ActionReadyTo := true
VarPix1 := !VarPix1
}
else if (Pixel2 = 0x000000) && (VarPix2 = false)
{
ActionReadyTo := true
VarPix2 := !VarPix2
}
else if (Pixel2 != 0x000000) && (VarPix2 = true)
{
ActionReadyTo := true
VarPix2 := !VarPix2
}
else if (Pixel3 = 0x000000) && (VarPix3 = false)
{
ActionReadyTo := true
VarPix3 := !VarPix3
}
else if (Pixel3 != 0x000000) && (VarPix3 = true)
{
ActionReadyTo := true
VarPix3 := !VarPix3
}
else if ActionReadyTo
{
send, ab
ActionReadyTo := false
}
Goto, MyRoutine
///
The problem with this script is that:
1) It looks like, from time to time, between 2 numbers, the script sends to much "ab".
So :
- either because it sends me as much as "ab" that pixels change by number.
- or if my script seems to you clean, it is that the color of the screen changes, briefly, without I realize it, between 2 numbers from time to time.
2) It is surely not optimized; there must be simpler and faster for the program to run, but with my little knowledge : that's all I can do.
Can someone tell me if my code is "ok"? And also tell me how it can be optimized ?
If you would have adopted another strategy for the basic project: which one?