danish_draj Posted March 20, 2019 Posted March 20, 2019 Hello everybody, I had made a program and GUI where if I press the Read button, It will display the value obtained by a sensor in the GUI i had created. Can i know how to make the Read button to be automated to get the sensor value and how to disable the program for 3 mins and 5mins? expandcollapse popup#include "Support\CommMg.au3" #include <MsgBoxConstants.au3> #include <Sound.au3> #include <ButtonConstants.au3> #include <EditConstants.au3> #include <GUIConstantsEx.au3> #include <StaticConstants.au3> #include <WindowsConstants.au3> #Region ### START Koda GUI section ### Form= $Form1 = GUICreate("Form1", 391, 177, 192, 124) $Label1 = GUICtrlCreateLabel("PRESSURE MONITORING SYSTEM", 8, 8, 373, 28, $SS_CENTER, $WS_EX_CLIENTEDGE) GUICtrlSetFont(-1, 14, 800, 0, "MS Sans Serif") $Label2 = GUICtrlCreateLabel("READING PSI", 16, 60, 161, 17, $SS_CENTER, $WS_EX_CLIENTEDGE) $Input1 = GUICtrlCreateInput("", 16, 90, 161, 32, BitOR($GUI_SS_DEFAULT_INPUT,$ES_CENTER,$ES_READONLY)) $Input2 = GUICtrlCreateInput("", 16, 122, 161, 32, BitOR($GUI_SS_DEFAULT_INPUT,$ES_CENTER,$ES_READONLY)) GUICtrlSetFont(-1, 14, 800, 0, "MS Sans Serif") $READ = GUICtrlCreateButton("READ", 216, 64, 147, 65) GUISetState(@SW_SHOW) #EndRegion ### END Koda GUI section ### Global $sportSetError = 0 Local $port = 25 _CommSetPort($port, $sportSetError, 9600, 8, "none", 1, 2) If $sportSetError <> "" Then MsgBox(262160,"Communication Error","Com "&$port&" Error"&@CRLF&"Comport List"&@CRLF&_CommListPorts(),0) Exit EndIf While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit Case $READ READ_VALUE() EndSwitch WEnd ;MsgBox($MB_SYSTEMMODAL, "Title", _CommListPorts()) Func READ_VALUE() _Commsendstring("R") ;get value from Arduino Sleep(500) $datar = _Commgetstring() ;$datar = bits of arduino(total bits in Arduino = 10240)(this bits using got 8) ;MsgBox(0, "A", $data) $data = $datar*0.0049 GUICtrlSetData($Input1, $data) Local $min = IniRead("CDA range.ini", "MIN", "M_MIN", "") Local $max = IniRead("CDA range.ini", "MAX", "M_MAX", "") $path = @ScriptDir & "\alarm.wav" If Number($data) < Number($min) Then GUICtrlSetBkColor($Input2, 0xFF0000) GUICtrlSetData($Input2, "Pressure Low") _SoundPlay($path, 0) sleep(3000) _SoundStop($path) ;MsgBox(0, 0, "PRESSURE LOW") ;EndIf ElseIf Number($data) > Number($max) Then ;MsgBox(0, "", $max) ;GUICtrlSetBkColor($Input1, 0xFF0000) GUICtrlSetBkColor($Input2, 0xFF0000) GUICtrlSetData($Input2, "Pressure High") _SoundPlay($path, 0) sleep(3000) _SoundStop($path) ;MsgBox(0, 0, "PRESSURE HIGH") Else GUICtrlSetBkColor($Input2, 0x00FF00) GUICtrlSetData($Input2, "Pressure Good") EndIf EndFunc the coding The GUI i had created
FrancescoDiMuro Posted March 20, 2019 Posted March 20, 2019 6 hours ago, danish_draj said: Can i know how to make the Read button to be automated to get the sensor value You could use a timer or AdLibRegister/Unregister function, to get the value every N milliseconds 6 hours ago, danish_draj said: and how to disable the program for 3 mins and 5mins? Why would you do that? Click here to see my signature: Spoiler ALWAYS GOOD TO READ: Forum Rules Forum Etiquette
danish_draj Posted March 21, 2019 Author Posted March 21, 2019 @FrancescoDiMuro hi there, Actually im doing that because im using pressure sensor to detect pressure drop in my workplace. In the code above, i did a alarm where if pressure drop certain range, an alarm/sound would go off. I dont want it to go off whenever i hook it up to an empty chamber because this could indicate " False Alarm" for my program to alert that the pressure drop
iAmNewbe Posted March 21, 2019 Posted March 21, 2019 When playing sound you have to close the handle after it is done playing or the sound is stopped. If you do not do this, I have ran into an issue especially if you run multiple copies of the application at once where all sound on the computer stops playing. I use SoundPlay instead of _SoundPlay because you can close the handle and stop the sound with one line SoundPlay("") Something to take into consideration with playing sound that unless you close the handle to currently playing sound, even if you stop it, you could run into no sound at all and you have to reboot to get it back.
iAmNewbe Posted March 21, 2019 Posted March 21, 2019 (edited) You want to add a Sleep statement at the end of your While loop to reduce the CPU usage your program is currently using. While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit Case $READ READ_VALUE() EndSwitch Sleep(10) ; Added to eliminate unnecessary CPU usage WEnd The quickest solution for your program delay is to just change the Sleep statement at the beginning of the Read_Value() function to 5 minutes. Sleep uses Milliseconds so 1000 MS is equal to 1 second. 60000 MS is equivalent to 1 minute. Edited March 21, 2019 by iAmNewbe
iAmNewbe Posted March 21, 2019 Posted March 21, 2019 Another option with the Program delay is to create a Pause feature where you can manually pause the loop and then restart it when you are ready. OR.. just close the program then restart it when you are ready. An option for the Pause feature is to use a variable switch and set it to True or False based on where you clicked to Pause or Start the program. I use a Tray Menu Item and set the label to change for Pause or Start. Then in the While Loop you already have you can either enclose the Switch in an If statement or set a new Case in the Switch and set an outside the Loop variable and check it. If $toggle = "Start" or something like that. Then set $toggle or whatever variable name to Pause to bypass the Switch. There are different ways to do it but that is one option.
danish_draj Posted March 21, 2019 Author Posted March 21, 2019 Hi @iAmNewbe Could you show an example for the Pause feature coding? BTW i had already done the Auto Read Now i cant seem to find a way on pausing the program for 3mins to 5min
FrancescoDiMuro Posted March 21, 2019 Posted March 21, 2019 (edited) @danish_draj Instead of pausing the script, why don't you just disable the alarm? You can set a boolean flag which you check before checking that the value of the pressure is in the non-alarm range. In this way, you avoid to pause your script, but you don't receive any false positive alarm either Edited March 21, 2019 by FrancescoDiMuro Click here to see my signature: Spoiler ALWAYS GOOD TO READ: Forum Rules Forum Etiquette
iAmNewbe Posted March 21, 2019 Posted March 21, 2019 (edited) Here is an example of A way to pause or prevent code to run within a loop. I am using a Tray Menu and Tray Menu Item to manually initiate the "Pause" expandcollapse popup#include <AutoItConstants.au3> #include <TrayConstants.au3> #Region ; Tray Opt("TrayMenuMode", 3) ; Default tray menu items will not be shown. Opt("TrayOnEventMode", 1) TraySetClick(16) ; Release Right Mouse Button Show Tray Menu Global $pauseMenuItem = TrayCreateItem("Pause") TrayItemSetOnEvent(-1, "startStop") TrayCreateItem("") TrayCreateItem("Exit") TrayItemSetOnEvent(-1, "Terminate") #EndRegion Global $pause = False While 1 If $pause = False Then ; Some code that runs here EndIf Sleep(10) WEnd Func startStop() If $pause = True Then TrayItemSetText($pauseMenuItem, "Pause") $pause = False Else TrayItemSetText($pauseMenuItem, "Start") $pause = True EndIf EndFunc Func Terminate() Exit EndFunc Edited March 21, 2019 by iAmNewbe
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now