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;
#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