Jump to content

Recommended Posts

Hello,

i have been making a countdown timer and its all working just how i like it, its just skipping the 0's en going from 1 - 59 ( it does wait the time needed for the counter to go from 1 - 0 - 59) It just goes from 1 to 59, showing the 1 for the time needed to go from 1 to 0 and then to 59.

 

Here's my code:

For $i = 60 To 1 Step -1
        Next

        If $i = 0 Then
            $Sc = $Sc - 1
        EndIf

        If $Sc = 0 Then
            $Sc = 0  ;~   <--- Shouldnt this work? Setting it to 0 then wait 1 sec to set it to 59?
            Sleep(1000)
            $Mn = $Mn - 1
            $Sc = 59
        EndIf


        If $Mn = 0 And $Sc = 0 Then
            $CDHrs = 0
            Sleep(1000)
            $Mn = 59
        EndIf

        GUICtrlSetData($Countdown, 'Hrs:'  & $CDHrs & ' ' & 'Min:' & $Mn & ' ' & 'Sec:' & $Sc)
        Sleep(1000)

I dont need the solution in code, just point me into the general direction where i am going wrong, i'd like to learn from my mistake. Thanks in advance.

 

~ Maffiagang

Edited by Maffiagang
Link to post
Share on other sites

Here, it would take too long to tell you what it is wrong.  But remember one thing, it is a bad idea to put Sleep inside the GUI loop.  Other than that, study the script, if you have more question, just let me know :

#include <GUIConstants.au3>

GUICreate ("Test")
Local $Countdown = GUICtrlCreateLabel ("", 50, 50, 300, 30)
GUISetState()

Local $CDHrs = 1, $Mn = 10, $Sc = 20
GUICtrlSetData($Countdown, 'Hrs:'  & $CDHrs & ' ' & 'Min:' & $Mn & ' ' & 'Sec:' & $Sc)
Local $hTimer = TimerInit()

While True
  Switch GUIGetMsg()
    Case $GUI_EVENT_CLOSE
      ExitLoop
  EndSwitch
  If TimerDiff($hTimer) < 1000 Then ContinueLoop
  $hTimer = TimerInit()
  $Sc -= 1
  If $Sc < 0 Then
    $Mn -= 1
    If $Mn < 0 Then
      $CDHrs -= 1
      If $CDHrs < 0 Then ExitLoop
      $Mn = 59
    EndIf
    $Sc = 59
  EndIf
  GUICtrlSetData($Countdown, 'Hrs:'  & $CDHrs & ' ' & 'Min:' & $Mn & ' ' & 'Sec:' & $Sc)
WEnd

 

Edited by Nine
corrected a small bug
Link to post
Share on other sites

A different approach (banged my head more than once on various hard places while trying to change Excel formula to AutoIT :idiot: )

Someone has to verify but seems more accurate than Nine's code. Avg. CPU loads looks similar for both (2.5-4%).

Console Output:

120011.8351 - 120000

#include <GUIConstants.au3>

GUICreate ("Test")
Local $Countdown = GUICtrlCreateLabel ("", 50, 50, 300, 30)
GUISetState()

Local $CDHrs = 0, $Mn = 2, $Sc = 0
GUICtrlSetData($Countdown, 'Hrs:'  & $CDHrs & ' ' & 'Min:' & $Mn & ' ' & 'Sec:' & $Sc)

Local $iCountDownTime = ($CDHrs*60*60+$Mn*60+$Sc)*1000
Local $hTimer = TimerInit()
Local $bTimerRunning = True
Local $iCounter = 0

While True
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
        ExitLoop
    EndSwitch

    If $bTimerRunning Then

        Local $iSc = floor(mod(60+$Sc-mod(int(TimerDiff($hTimer)/1000),60),60))
        Local $iMn = Int(mod((60+$Mn-Floor(TimerDiff($hTimer)/1000-$Sc)/60),60))
        Local $iCDHrs = Int(mod(($CDHrs-Floor(TimerDiff($hTimer)/1000-$Sc-60*$Mn)/(60*60)),60))

        if $iCounter <> Int(TimerDiff($hTimer)/1000) Then
            GUICtrlSetData($Countdown, 'Hrs:'  & $iCDHrs & ' ' & 'Min:' & $iMn & ' ' & 'Sec:' & $iSc)
            $iCounter = Int(TimerDiff($hTimer)/1000)
        EndIf

        If $iSc=0 and $iMn=0 and $iCDHrs= 0 Then
            ConsoleWrite(TimerDiff($hTimer) & " - " & $iCountDownTime & @CRLF)
            $bTimerRunning=False
        EndIf

    EndIf
    
WEnd

 

Modified Nine's Code to include a second timer (I hope it is accurate?).

Console Output:

122316.3811

#include <GUIConstants.au3>

GUICreate ("Test")
Local $Countdown = GUICtrlCreateLabel ("", 50, 50, 300, 30)
GUISetState()

Local $CDHrs = 0, $Mn = 2, $Sc = 0
GUICtrlSetData($Countdown, 'Hrs:'  & $CDHrs & ' ' & 'Min:' & $Mn & ' ' & 'Sec:' & $Sc)
Local $hTimer = TimerInit()
Local $hTimer2 = TimerInit()

While True
  Switch GUIGetMsg()
    Case $GUI_EVENT_CLOSE
      ExitLoop
  EndSwitch
  If TimerDiff($hTimer) < 1000 Then ContinueLoop
  $hTimer = TimerInit()
  $Sc -= 1
  If $Sc < 0 Then
    $Mn -= 1
    If $Mn < 0 Then
      $CDHrs -= 1
      If $CDHrs < 0 Then
          ConsoleWrite(TimerDiff($hTimer2) & @CRLF)
          ExitLoop
      EndIf
      $Mn = 59
    EndIf
    $Sc = 59
  EndIf
  GUICtrlSetData($Countdown, 'Hrs:'  & $CDHrs & ' ' & 'Min:' & $Mn & ' ' & 'Sec:' & $Sc)
WEnd

 

Link to post
Share on other sites
On 12/22/2020 at 8:25 PM, Nine said:

Here, it would take too long to tell you what it is wrong.  But remember one thing, it is a bad idea to put Sleep inside the GUI loop.  Other than that, study the script, if you have more question, just let me know :

#include <GUIConstants.au3>

GUICreate ("Test")
Local $Countdown = GUICtrlCreateLabel ("", 50, 50, 300, 30)
GUISetState()

Local $CDHrs = 1, $Mn = 10, $Sc = 20
GUICtrlSetData($Countdown, 'Hrs:'  & $CDHrs & ' ' & 'Min:' & $Mn & ' ' & 'Sec:' & $Sc)
Local $hTimer = TimerInit()

While True
  Switch GUIGetMsg()
    Case $GUI_EVENT_CLOSE
      ExitLoop
  EndSwitch
  If TimerDiff($hTimer) < 1000 Then ContinueLoop
  $hTimer = TimerInit()
  $Sc -= 1
  If $Sc < 0 Then
    $Mn -= 1
    If $Mn < 0 Then
      $CDHrs -= 1
      If $CDHrs < 0 Then ExitLoop
      $Mn = 59
    EndIf
    $Sc = 59
  EndIf
  GUICtrlSetData($Countdown, 'Hrs:'  & $CDHrs & ' ' & 'Min:' & $Mn & ' ' & 'Sec:' & $Sc)
WEnd

 

I knew it should be possible with only ifs instead of using for, thank you for this, ive added it to my code, Thanks!

Btw, i'm using OnEventMode and that piece of code was not in my main while but in a different func. My main while only consists of a waiting loop for the start button to be pressed. :)

It was my mistake for not posting the full piece of my code.

I was going this way before, but i was using my own timer and another timer which substracted from themselves, by which i was getting a time. 

(Somehow with round and substracting the hours / mins / secs from that value, i could only get the last 30 sec of each minute to display (derp) 

Probably had to do something with me having to add a - 0.5 to each equation to get it to work entirely..._ i couldnt get it to work by getting the full 1 (or 2) digit without any decimal (it would always round up or down, which it shouldnt in my case)

Your way is SO much simpler than what i was trying before i tried using for. Thanks again. :) 

Edited by Maffiagang
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
  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By Dequality
      error code >Running:(3.3.14.2):C:\Program Files (x86)\AutoIt3\autoit3.exe "D:\Scripts\Tests\Test.au3" --> Press Ctrl+Alt+Break to Restart or Ctrl+Break to Stop +>16:23:40 AutoIt3.exe ended.rc:0 +>16:23:40 AutoIt3Wrapper Finished. >Exit code: 0 Time: 0.6734 HotKeySet("{F4}", _myExit()) Global $aCoordinates1[2] = [499, 582] Global $aCoordinates2[2] = [672, 614] While(1)     $instrument = PixelSearch(602, 439, 601, 438, 0x434343, 1)     if not(@error) Then         $musicscore = PixelSearch(1185, 529, 1184, 528, 0xDDA03B, 1)         If not(@error) Then             MouseMove($musicscore[0], $musicscore[1], 3)             Sleep(500)             MouseClick("RIGHT", $musicscore[0], $musicscore[1], 1, 1)         EndIf     EndIf     MouseClick("LEFT", random($aCoordinates1[0], $aCoordinates1[1]), Random($aCoordinates2[0], $aCoordinates2[1]))    Sleep(Random(304000, 309043)) WEnd Func _myExit()     Exit EndFunc Any help are highly appreciatet... :-) 
       
      Dequality.
    • By Tats
      Hi,
      Good Day!
      I have a problem and having difficulty implementing it. I want to copy the files in a certain folder, rename it, and when it exist in the destination folder it will add an index counter.
       
      Example source folder having files like:
      AA_123.TXT
      BB_123.TXT
      CC_123.TXT
      DD_123.TXT
       
      After file copy the target folder would be like this:
      123.TXT
      123_1.TXT
      123_2.TXT
      123_3.TXT
       
      How to achieve this?
      Thanks in advance
       
       
    • By Dequality
      Can anyone tell me if its possible to make autoit search for text? (im trying to search for a button a pop up(the color changes every time so i cant use img search) so i thought why not try using text search if possible) , the text is following: 'OK', 'Okay', 'Tak', 'Fedt'.
      I've searched around most possibilities i found was img search which doesnt work cuz of the color change.. ^^
      Any help are HIGHLY appreciatet.
      -Dequality.
    • By triodz
      I want the number in a file to be overwritten with the next number up each time a button is pressed. 
      So it is pressed once, the number '1' is written to the file. It is pressed again and the number '2' overwrites the first number.
      At the moment I get the number 1 written, but can't get it to overwrite. What I have so far:
      #include <ButtonConstants.au3> #include <GUIConstantsEx.au3> #include <StaticConstants.au3> #include <WindowsConstants.au3> #include <FileConstants.au3> #include <MsgBoxConstants.au3> #include <WinAPIFiles.au3> #Region ### START Koda GUI section ### Form=C:\Users\soulf\Desktop\CalcGui\RLCalc.kxf $Form1 = GUICreate("Form1", 615, 437, 426, 141) $One = GUICtrlCreateButton("1", 32, 72, 25, 25) GUICtrlSetBkColor(-1, 0xFF0000) GUISetState(@SW_SHOW) #EndRegion ### END Koda GUI section ### Func First ()     $fPath = "C:\temp\First.txt"     $fOpen = FileOpen ($fPath, 2)     $Counter = FileReadLine ($fpath)     $counter = $counter + 1     $fWrite = filewriteline ($fPath, $counter)     FileClose ($fpath) EndFunc While 1     $nMsg = GUIGetMsg()     Switch $nMsg         Case $GUI_EVENT_CLOSE             Exit         Case $One             Call ("First")     EndSwitch WEnd I tried a Do Until statement, but that just puts the numbers 1 to 5 in the file.
      Any help would be greatly appreciated! 
    • By DiveHigh
      Hello There!  
      I've encountered a small issue i just cant seem to program my way out of...

      What im doing is:        ;or rather, what im trying to do is
      Pixelsearch for a color, within a part of my screen,
      If color is present, i will rightclick it, for a menu to appear,
      in which i want to Leftclick an option. Simplicity itself.   

      The issue is that because this menu appears in different parts of my screen, its complicated to select the option i want to click.
      There is no special color to it, and i cannot define the area in which to click, because the menu appears different places.
      The only thing that is in common for the menu is that the Leftclick needs to be (lets say) 30pixels below the rightclick
      .

      What can be done,  but seemingly not by me, is to get the coordinates of the rightclick, and use them as the "starting point" from where the coordinates for the leftclick will be.
      (Not sure if that makes sence, but maybe the code below will help you understand)

      You guys like example codes, so therefore i will give it my best shot, and hope it proves usefull

      $pos = Pixelsearch(200, 400, 200, 400, 0xFFFFFF)        ;This color is what i need to rightclick to bring up the menu
      If not @Error then                                                               ;If the color is present
      (Mouseclick("Right", $pos[0], $pos[1], 1, 1)                   ;Rightclick it, to bring up menu
      ;Here comes the part i cannot get to work
      $x = MousegetPos(X)                                                          ;To get the Coordinate "X", so we can calculated with it for the position of the leftclick
      $y = MousegetPos(Y)                                                           ; To get the Coordinate "Y", so that can be calculated with for the position of the leftclick

      MouseClick("Left", $x[0], $y-30[1], 1, 1)                         ;This was supposed to give me a leftclick that is 30pixels below the rightclick.
      (The MouseGetPos(X), may seem as a waste of space in this example, but in my script i will need both. 
              Im only using one in the example, mainly so that i wont confuse myself, but also to make it easier to digest for you guys)


      I have not been able to find any topic that covers this, but my apologies if such a topic already exist in the forums. 

      Maybe while im at it, can i ask,   what good does the [0] and [1] do, when using a "$variable" as coordinate?   


      Thanks in advance
      - DiveHigh
×
×
  • Create New...