Sign in to follow this  
Followers 0
JuggaloZeke

Having a problem ending a While loop

5 posts in this topic

#1 ·  Posted (edited)

I have written a script that will continually connect to the internet via my air card, visit a webpage (http://autoit.myminicity.com/) which acts like a counter, and then disconnects from the internet and repeats the process to get a new IP.

The problem I'm having is with my While loops. Either they are eternally stuck in the loop even though the expression is false or they work right through the loop while the expression is true. I may be calling them incorrectly, I am not sure which is why I'm here.

The way the script SHOULD work: The script determines where the icon to my VZAccess Manager program is and records its position. It will use this position to poll its color to determine when to move on with the script. It will send an {ENTER} key to the program to begin connecting. At this point it should enter the first loop. While the icon is either orange(disconnected) or yellow(connecting) it should continue to loop. When it changes to another color (green[connected] or red[disconnecting]) is when it should exit the loop and continue on by sending a URL to browser, waiting for the page to load, and then going back into a loop to disconnect (just waiting for greed and red to pass this time) and then starting over.

And I will be replacing the send lines with ControlSend after playing around with that a little bit. Pretty cool feature.

#cs ----------------------------------------------------------------------------

 AutoIt Version: 3.2.10.0
 Author:         Brandon Kling

 Script Function:
    Connect, load specified MyMiniCity pages, disconnect, repeat.

#ce ----------------------------------------------------------------------------

; Script Start

AutoItSetOption("WinTitleMatchMode", 2)

Global $Paused
HotKeySet("{PAUSE}", "TogglePause")
HotKeySet("{ESC}", "Terminate")

Dim $coord = PixelSearch(625, 760, 950, 760, 0xDE5B04, 0, 16) ;Finds VZAccess Manager icon in taskbar.

;MsgBox ( 0 , "Location Selector" , "Place mouse in lower left corner of VZAccess Manager icon and press Enter." )
;Dim $location = MouseGetPos()
;MsgBox ( 0 , "DEBUG" , "Mouse x,y: " & $location [ 0 ] & ", " & $location [ 1 ] )
;MsgBox ( 0 , "DEBUG" , "Mouse x,y: " & $coord [ 0 ] & ", " & $coord [ 1 ] )
;Dim $deccolor = PixelGetColor ( $location [ 0 ] , $location [ 1 ] )
;MsgBox ( 0 , "DEBUG" , "Decimal color: " & $deccolor )
;Dim $color = Hex ( $deccolor , 6 )

Dim $deccolor = PixelGetColor($coord[0], $coord[1]) ;Gets color of VZAccess Manager icon (which should be DE5B04)

;MsgBox ( 0 , "DEBUG" , "Decimal color: " & $deccolor )

Dim $color = Hex($deccolor, 6) ;Converts captured color to hex

Dim Const $yellow = "71620E" ;Constant for the yellow color

Dim Const $red = "A60000" ;Constant for the red color

Dim Const $green = "00830C" ;Constant for the green color

Dim Const $orange = "DE5B04" ;Constant for the orange color

;MsgBox ( 0 , "DEBUG" , "Hex color: " & $color & ", variable type: " & VarGetType ( $color ) & ", color types: " & VarGetType ( $red ) )
;MsgBox ( 0 , "Ready to Start" , "The program is now ready to run." )

While 1
    
    WinActivate("VZAccess Manager")
    
    ToolTip("Activated VZAccess Manager.", 0, 0)
    
    Sleep(1000)
    
    Send("{ENTER}")
    
    ToolTip("Sent ENTER key to connect.", 0, 0)
    
    While $color = $orange Or $yellow
        
        ToolTip("Starting WHILE loop", 0, 0)
    
        Sleep(1000)
        
;       $deccolor = PixelGetColor ( $location [ 0 ] , $location [ 1 ] )

        $deccolor = PixelGetColor($coord[0], $coord[1])
        
        ToolTip("Updating COLOR variable.", 0, 0)
    
        Sleep(1000)
        
    WEnd
    
    ToolTip("Successfully worked through first WHILE loop.", 0, 0)
    
    Sleep(1000)
    
    WinActivate("Windows Internet Explorer")
    
    Send("{F6}")
    
    Sleep(1000)
    
    Send("http://autoit.myminicity.com/{ENTER}")
    
    WinActivate("VZAccess Manager")
    
    While $color = $green Or $red
    
;       $deccolor = PixelGetColor ( $location [ 0 ] , $location [ 1 ] )

        $deccolor = PixelGetColor($coord[0], $coord[1])
    
    WEnd

    Send("{ENTER}")
    
WEnd

Func TogglePause()
    $Paused = NOT $Paused
    While $Paused
        Sleep(100)
        ToolTip('Script is "Paused"', 0, 0)
    WEnd
    ToolTip("")
EndFunc

Func Terminate()
    Exit 0
EndFunc
Edited by JuggaloZeke

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

I have written a script that will continually connect to the internet via my air card, visit a webpage (http://autoit.myminicity.com/) which acts like a counter, and then disconnects from the internet and repeats the process to get a new IP.

The problem I'm having is with my While loops. Either they are eternally stuck in the loop even though the expression is false or they work right through the loop while the expression is true. I may be calling them incorrectly, I am not sure which is why I'm here.

The way the script SHOULD work: The script determines where the icon to my VZAccess Manager program is and records its position. It will use this position to poll its color to determine when to move on with the script. It will send an {ENTER} key to the program to begin connecting. At this point it should enter the first loop. While the icon is either orange(disconnected) or yellow(connecting) it should continue to loop. When it changes to another color (green[connected] or red[disconnecting]) is when it should exit the loop and continue on by sending a URL to browser, waiting for the page to load, and then going back into a loop to disconnect (just waiting for greed and red to pass this time) and then starting over.

And I will be replacing the send lines with ControlSend after playing around with that a little bit. Pretty cool feature.

#cs ----------------------------------------------------------------------------

 AutoIt Version: 3.2.10.0
 Author:         Brandon Kling

 Script Function:
    Connect, load specified MyMiniCity pages, disconnect, repeat.

#ce ----------------------------------------------------------------------------

; Script Start

AutoItSetOption("WinTitleMatchMode", 2)

Global $Paused
HotKeySet("{PAUSE}", "TogglePause")
HotKeySet("{ESC}", "Terminate")

Dim $coord = PixelSearch(625, 760, 950, 760, 0xDE5B04, 0, 16) ;Finds VZAccess Manager icon in taskbar.

;MsgBox ( 0 , "Location Selector" , "Place mouse in lower left corner of VZAccess Manager icon and press Enter." )
;Dim $location = MouseGetPos()
;MsgBox ( 0 , "DEBUG" , "Mouse x,y: " & $location [ 0 ] & ", " & $location [ 1 ] )
;MsgBox ( 0 , "DEBUG" , "Mouse x,y: " & $coord [ 0 ] & ", " & $coord [ 1 ] )
;Dim $deccolor = PixelGetColor ( $location [ 0 ] , $location [ 1 ] )
;MsgBox ( 0 , "DEBUG" , "Decimal color: " & $deccolor )
;Dim $color = Hex ( $deccolor , 6 )

Dim $deccolor = PixelGetColor($coord[0], $coord[1]) ;Gets color of VZAccess Manager icon (which should be DE5B04)

;MsgBox ( 0 , "DEBUG" , "Decimal color: " & $deccolor )

Dim $color = Hex($deccolor, 6) ;Converts captured color to hex

Dim Const $yellow = "71620E" ;Constant for the yellow color

Dim Const $red = "A60000" ;Constant for the red color

Dim Const $green = "00830C" ;Constant for the green color

Dim Const $orange = "DE5B04" ;Constant for the orange color

;MsgBox ( 0 , "DEBUG" , "Hex color: " & $color & ", variable type: " & VarGetType ( $color ) & ", color types: " & VarGetType ( $red ) )
;MsgBox ( 0 , "Ready to Start" , "The program is now ready to run." )

While 1
    
    WinActivate("VZAccess Manager")
    
    ToolTip("Activated VZAccess Manager.", 0, 0)
    
    Sleep(1000)
    
    Send("{ENTER}")
    
    ToolTip("Sent ENTER key to connect.", 0, 0)
    
    While $color = $orange Or $yellow
        
        ToolTip("Starting WHILE loop", 0, 0)
    
        Sleep(1000)
        
;       $deccolor = PixelGetColor ( $location [ 0 ] , $location [ 1 ] )

        $deccolor = PixelGetColor($coord[0], $coord[1])
        
        ToolTip("Updating COLOR variable.", 0, 0)
    
        Sleep(1000)
        
    WEnd
    
    ToolTip("Successfully worked through first WHILE loop.", 0, 0)
    
    Sleep(1000)
    
    WinActivate("Windows Internet Explorer")
    
    Send("{F6}")
    
    Sleep(1000)
    
    Send("http://autoit.myminicity.com/{ENTER}")
    
    WinActivate("VZAccess Manager")
    
    While $color = $green Or $red
    
;       $deccolor = PixelGetColor ( $location [ 0 ] , $location [ 1 ] )

        $deccolor = PixelGetColor($coord[0], $coord[1])
    
    WEnd

    Send("{ENTER}")
    
WEnd

Func TogglePause()
    $Paused = NOT $Paused
    While $Paused
        Sleep(100)
        ToolTip('Script is "Paused"', 0, 0)
    WEnd
    ToolTip("")
EndFunc

Func Terminate()
    Exit 0
EndFunc
You need to make a few changes. The ones I notices are

A. Pixelgetcolor returns a decimal number so keep with numbers. Hex returns a string so you won't make much progress trying to compare a number to a string. Use numbers for your colours, eg

$color = 0xff0033

or

$color = 0

B. Your condition logic is incorrect. You've written it the way we say things but that's not how AutoIt understands it sometimes.

Instead of

While $color = $orange Or $yellow

write

While $color = $orange Or $color = $yellow
Edited by martin

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
Share on other sites

You need to make a few changes. The ones I notices are

A. Pixelgetcolor returns a decimal number so keep with numbers. Hex returns a string so you won't make much progress trying to compare a number to a string. Use numbers for your colours, eg

$color = 0xff0033

or

$color = 0

I don't get what you mean? I am aware PixelGetColor returns a decimal number, and that when converted to Hex it creates a string. Did I not make the constants strings as well? This is one part that has had be confused and I've tried it several different ways, even listing my colors in the 0xFF0033 format. Or are you just talking about the $color variable? That is the part I've been struggling with.

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

I don't get what you mean? I am aware PixelGetColor returns a decimal number, and that when converted to Hex it creates a string. Did I not make the constants strings as well? This is one part that has had be confused and I've tried it several different ways, even listing my colors in the 0xFF0033 format. Or are you just talking about the $color variable? That is the part I've been struggling with.

You made the constants strings like this

Dim Const $yellow = "71620E"

So $yellow is a string

In your While loop, (Which I misread first time by the way) you have

While $color = $orange Or $yellow;Nothing is happening to change $color so you'll be stuck in the loop
       
        ToolTip("Starting WHILE loop", 0, 0)
   
        Sleep(1000)
       
;     $deccolor = PixelGetColor ( $location [ 0 ] , $location [ 1 ] )

        $deccolor = PixelGetColor($coord[0], $coord[1]);I thought this was $color = 
;$deccolor is a number not a string
       
        ToolTip("Updating COLOR variable.", 0, 0)
   
        Sleep(1000)
       
    WEnd

I think you need this, or at least what I mean is, I've changed the bits I was talking about to what I think you want.

#cs ----------------------------------------------------------------------------
    
    AutoIt Version: 3.2.10.0
    Author:      Brandon Kling
    
    Script Function:
    Connect, load specified MyMiniCity pages, disconnect, repeat.
    
#ce ----------------------------------------------------------------------------

; Script Start

AutoItSetOption("WinTitleMatchMode", 2)

Global $Paused
HotKeySet("{PAUSE}", "TogglePause")
HotKeySet("{ESC}", "Terminate")

Dim $coord = PixelSearch(625, 760, 950, 760, 0xDE5B04, 0, 16);Finds VZAccess Manager icon in taskbar.

;MsgBox ( 0 , "Location Selector" , "Place mouse in lower left corner of VZAccess Manager icon and press Enter." )
;Dim $location = MouseGetPos()
;MsgBox ( 0 , "DEBUG" , "Mouse x,y: " & $location [ 0 ] & ", " & $location [ 1 ] )
;MsgBox ( 0 , "DEBUG" , "Mouse x,y: " & $coord [ 0 ] & ", " & $coord [ 1 ] )
;Dim $deccolor = PixelGetColor ( $location [ 0 ] , $location [ 1 ] )
;MsgBox ( 0 , "DEBUG" , "Decimal color: " & $deccolor )
;Dim $color = Hex ( $deccolor , 6 )

Dim $deccolor = PixelGetColor($coord[0], $coord[1]);Gets color of VZAccess Manager icon (which should be DE5B04)

;MsgBox ( 0 , "DEBUG" , "Decimal color: " & $deccolor )

Const $color = $deccolor

Const $yellow = 0x71620E;Constant for the yellow color

Const $red = 0xA60000;Constant for the red color

Const $green = 0x00830C;Constant for the green color

Const $orange = 0xDE5B04;Constant for the orange color

;MsgBox ( 0 , "DEBUG" , "Hex color: " & $color & ", variable type: " & VarGetType ( $color ) & ", color types: " & VarGetType ( $red ) )
;MsgBox ( 0 , "Ready to Start" , "The program is now ready to run." )

While 1

    WinActivate("VZAccess Manager")

    ToolTip("Activated VZAccess Manager.", 0, 0)

    Sleep(1000)

    Send("{ENTER}")

    ToolTip("Sent ENTER key to connect.", 0, 0)

    While $color = $orange Or $color = $yellow

        ToolTip("Starting WHILE loop", 0, 0)

        Sleep(1000)

;     $deccolor = PixelGetColor ( $location [ 0 ] , $location [ 1 ] )

        $color = PixelGetColor($coord[0], $coord[1])

        ToolTip("Updating COLOR variable.", 0, 0)

        Sleep(1000)

    WEnd

    ToolTip("Successfully worked through first WHILE loop.", 0, 0)

    Sleep(1000)

    WinActivate("Windows Internet Explorer")

    Send("{F6}")

    Sleep(1000)

    Send("http://autoit.myminicity.com/{ENTER}")

    WinActivate("VZAccess Manager")

    While $color = $green Or$color = $red

;     $deccolor = PixelGetColor ( $location [ 0 ] , $location [ 1 ] )

        $color = PixelGetColor($coord[0], $coord[1])

    WEnd

    Send("{ENTER}")

WEnd

Func TogglePause()
    $Paused = Not $Paused
    While $Paused
        Sleep(100)
        ToolTip('Script is "Paused"', 0, 0)
    WEnd
    ToolTip("")
EndFunc;==>TogglePause

Func Terminate()
    Exit 0
EndFunc;==>Terminate

But it won't work still because as soon as you reach the first while loop $color is not equal to either $orange or $yellow so the while loop will be skipped. I don't know what you're trying to do but maybe you should set $color = $orange first rather than to $deccolor.

Edited by martin

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
Share on other sites

Actually, that worked flawlessly. :D

When the script first searches for the icon in the task bar notice is is searching for the Orange value. By default, the icon is orange (while it is disconnected) so that is why I chose that as a starting point. Obviously, if the card is in any other state it won't work but I can easily make sure it is where it needs to be to start.

Thanks for the help!

Share this post


Link to post
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
Sign in to follow this  
Followers 0