JBAU Posted May 14, 2013 Share Posted May 14, 2013 I have this on my UNO R3; #define led1 13 // Variable that will contain the character received via the serial port int Received = ' '; void setup() { // Open the serial port at 9600, Define the pin led1 as OUTPUT Serial.begin(9600); pinMode(led1, OUTPUT); } void loop() { if (Serial.available() > 0) { // If the serial buffer contains data Received = Serial.read(); // Read the received data switch (Received){ // Interpret the data received and act accordingly case '0': digitalWrite(led1,LOW); // If I get 0 turn off led1 Serial.print("1OFF"); break; case '1': digitalWrite(led1,HIGH); // If I get 1 turn on led1 Serial.print("1ON"); break; }}} And Using; expandcollapse popup#include <ButtonConstants.au3> #include <GUIConstantsEx.au3> #include <WindowsConstants.au3> #include 'CommMG.au3' ;~ Declare variable $Red & $Green for easier explanation Global $Red = 0xFF0000 Global $Green = 0x00FF00 #Region ### START Koda GUI section ### Form= ; ~ Creation of the main Gui Position 402px_Right , 120px_Down , 389px_Wide & 341px_High $Form1 = GUICreate ( "Arduino GUI" , 402 , 120 , 389 , 341 ) ;~ $Led1 30px in size & Assigned $Red $Led1 = GUICtrlCreateGraphic ( 10 , 10 , 30 , 30 ) GUICtrlSetGraphic ( - 1 , $GUI_GR_COLOR , $Red , $Red ) GUICtrlSetGraphic ( - 1 , $GUI_GR_ELLIPSE , 0 , 0 , 30 , 30 ) ;~ Creation of ~ 3 buttons $Button1 = GUICtrlCreateButton ( "Pin 13" , 50 , 10 , 55 , 30 ) GUISetState (@SW_SHOW) #EndRegion ### END Koda GUI section ### ;~ Com Serial Settings Global $Com_Port = 3 Global $BitPerSecond = 9600 Global $BitDati = 8 Global $Parity = 0 Global $BitStop = 1 Global $FlowControl = 2 Global $sErr _CommSetPort ( $Com_Port , $sErr , $BitPerSecond , $BitDati , $Parity , $BitStop , $FlowControl ) Global $Button1_press = True ;~ $BS_PUSHBUTTON variable declaration , management of the buttons Global $BS_PUSHBUTTON While 1 $ReceivedFromCom = _CommGetString ( ) Sleep ( 20 ) ;~ ConsoleWrite ("Received =" & @ CRLF & $ ReceivedFromCom) Switch $ReceivedFromCom Case "1ON" GUICtrlSetGraphic ( $Led1 , $GUI_GR_COLOR , $Green , $Green ) GUICtrlSetGraphic ( $Led1 , $GUI_GR_ELLIPSE , 0 , 0 , 30 , 30 ) GUICtrlSetGraphic ( $Led1 , $GUI_GR_REFRESH ) Case "1OFF" GUICtrlSetGraphic ( $Led1 , $GUI_GR_COLOR , $Red , $Red ) GUICtrlSetGraphic ( $Led1 , $GUI_GR_ELLIPSE , 0 , 0 , 30 , 30 ) GUICtrlSetGraphic ( $Led1 , $GUI_GR_REFRESH ) EndSwitch $nMsg = GUIGetMsg ( ) Switch $nMsg Case $GUI_EVENT_CLOSE Exit Case $Button1 If ( $Button1_press = False ) Then _CommSendString ( "0" ) ;Turn OFF led1 $Button1_press = True Else _CommSendString ( "1" ) ;Turn ON led1 $Button1_press = False EndIf EndSwitch WEnd So i would like to draw the $led1 state using _CommGetString to set the LED green if Pin13 is active. So when the gui is launched, i know exactly what state Pin13 is at and nor just rewriting the current state. Salute to Martin Link to comment Share on other sites More sharing options...
FireFox Posted May 14, 2013 Share Posted May 14, 2013 Hi,Welcome to the autoit forum Try the Serial.println function instead of the Serial.print one.Also, add a ConsoleWrite debug of the $ReceivedFromCom variable to check where the problem comes from Br, FireFox. Link to comment Share on other sites More sharing options...
JBAU Posted May 14, 2013 Author Share Posted May 14, 2013 ConsoleWrite debug Just started, more reading  Got a semi - show me , example ? Link to comment Share on other sites More sharing options...
FireFox Posted May 14, 2013 Share Posted May 14, 2013 ;... While 1 $ReceivedFromCom = _CommGetString ( ) ConsoleWrite($ReceivedFromCom & @CrLf) ;... Link to comment Share on other sites More sharing options...
JBAU Posted May 14, 2013 Author Share Posted May 14, 2013 Nice, thanks Link to comment Share on other sites More sharing options...
JBAU Posted May 14, 2013 Author Share Posted May 14, 2013 Stumped. 1 sent = 1ON 0 sent = 1OFF No changes to led Link to comment Share on other sites More sharing options...
FireFox Posted May 14, 2013 Share Posted May 14, 2013 Well, I don't know the problem here. Surely the string sent is not clearly received.I will test it tomorrow afternoon if no one gives the answer.Br, FireFox. Link to comment Share on other sites More sharing options...
FireFox Posted May 15, 2013 Share Posted May 15, 2013 (edited) Your code works fine, check that your arduino is not on the COM Port 1. However, I rewrote the scripts in a cleaner way : #define led1 13 // Variable that will contain the character received via the serial port char Received = '0'; void setup() { // Open the serial port at 9600, Define the pin led1 as OUTPUT Serial.begin(9600); pinMode(led1, OUTPUT); } void loop() { if (Serial.available() > 0) { // If the serial buffer contains data Received = Serial.read(); // Read the received data switch (Received){ // Interpret the data received and act accordingly case '0': digitalWrite(led1, LOW); // If I get 0 turn off led1 Serial.print("1OFF"); break; case '1': digitalWrite(led1, HIGH); // If I get 1 turn on led1 Serial.print("1ON"); break; } } } expandcollapse popup#include <GUIConstantsEx.au3> #include <WindowsConstants.au3> #include <Constants.au3> #include "CommMG.au3" ;~ Com Serial Settings Global $Com_Port = 3, $BitPerSecond = 9600, $BitDati = 8, $Parity = 0, $BitStop = 1, $FlowControl = 2, $sErr = "" _CommSetPort($Com_Port, $sErr, $BitPerSecond, $BitDati, $Parity, $BitStop, $FlowControl) Global $blLEDON = True #region GUI Global $hForm = 0, $iGrLed1 = 0, $iButton1 = 0 $hForm = GUICreate("Arduino GUI") $iGrLed1 = GUICtrlCreateGraphic(10, 10, 30, 30) GUICtrlSetGraphic($iGrLed1, $GUI_GR_COLOR, $COLOR_RED, $COLOR_RED) GUICtrlSetGraphic($iGrLed1, $GUI_GR_ELLIPSE, 0, 0, 30, 30) $iButton1 = GUICtrlCreateButton("Pin 13", 50, 10, 55, 30) GUISetState(@SW_SHOW, $hForm) #endregion GUI Local $iMsg = 0, $sReceivedFromCom = "" While 1 Sleep(20) $iMsg = GUIGetMsg() Switch $iMsg Case $GUI_EVENT_CLOSE Exit Case $iButton1 If Not $blLEDON Then _CommSendString("0") ;Turn OFF led1 Else _CommSendString("1") ;Turn ON led1 EndIf $blLEDON = Not $blLEDON EndSwitch $sReceivedFromCom = _CommGetString() If $sReceivedFromCom = "" Then ContinueLoop ConsoleWrite("Received: " & $sReceivedFromCom & @CRLF) Switch $sReceivedFromCom Case "1ON" GUICtrlSetGraphic($iGrLed1, $GUI_GR_COLOR, $COLOR_GREEN, $COLOR_GREEN) GUICtrlSetGraphic($iGrLed1, $GUI_GR_ELLIPSE, 0, 0, 30, 30) GUICtrlSetGraphic($iGrLed1, $GUI_GR_REFRESH) Case "1OFF" GUICtrlSetGraphic($iGrLed1, $GUI_GR_COLOR, $COLOR_RED, $COLOR_RED) GUICtrlSetGraphic($iGrLed1, $GUI_GR_ELLIPSE, 0, 0, 30, 30) GUICtrlSetGraphic($iGrLed1, $GUI_GR_REFRESH) EndSwitch WEnd Br, FireFox. Edited May 15, 2013 by FireFox Link to comment Share on other sites More sharing options...
JBAU Posted May 16, 2013 Author Share Posted May 16, 2013 (edited) Looks Prettier, Led Graphic now stays red , nice try though Realistically, probably just need a way to redraw/refresh the gui dependent on whats in the buffer. Edited May 16, 2013 by JBAU Link to comment Share on other sites More sharing options...
FireFox Posted May 16, 2013 Share Posted May 16, 2013 (edited) hm? Does it work or not? Or should I say: Have you managed to get it working?Br, FireFox. Edited May 16, 2013 by FireFox Link to comment Share on other sites More sharing options...
JBAU Posted May 16, 2013 Author Share Posted May 16, 2013 Your version, led in gui stays red always, changes on uno. Mine same as before, works gui and board. What i wanted was to be able to close the script while the uno led was active, & re-run the script, with the gui graphic of the led be green, not red. I don't think is possible without actually polling the pin & not the buffer, i could be wrong. I am thankful as i can see a neater or more compounded way of writing things , thanks! Link to comment Share on other sites More sharing options...
FireFox Posted May 16, 2013 Share Posted May 16, 2013 Your version, led in gui stays red always, changes on uno.Then something's wrong with what you have done.What i wanted was to be able to close the script while the uno led was active, & re-run the script, with the gui graphic of the led be green, not red.Yes it's possible, send another command to ask for the led status, then set it on the GUI.Br, FireFox. Link to comment Share on other sites More sharing options...
JBAU Posted May 16, 2013 Author Share Posted May 16, 2013 Your version, led in gui stays red always, changes on uno. Then something's wrong with what you have done. Uploaded with your edits of the sketch and script. Link to comment Share on other sites More sharing options...
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