Interfacing with AutoHotkey (Easy GUI's on Windows & Linux)

I have started a library for interfacing AutoHotkey with your Arduino.

These examples utilize the AutoHotkey scripting language [ http://autohotkey.com ]

autohotkey.com:
AutoHotkey is a free, open-source utility for Windows. With it, you can:

  • Automate almost anything by sending keystrokes and mouse clicks. You can write a mouse or keyboard macro by hand or use the macro recorder.
  • Create hotkeys for keyboard, joystick, and mouse. Virtually any key, button, or combination can become a hotkey.
  • Expand abbreviations as you type them. For example, typing "btw" can automatically produce "by the way".
  • Create custom data-entry forms, user interfaces, and menu bars. See GUI for details.
  • Remap keys and buttons on your keyboard, joystick, and mouse.
  • Respond to signals from hand-held remote controls via the WinLIRC client script.
  • Run existing AutoIt v2 scripts and enhance them with new capabilities.
  • Convert any script into an EXE file that can be run on computers that don't have AutoHotkey installed.

Getting started might be easier than you think. Check out the quick-start tutorial.

Here is the AutoHotkey forum thread. http://www.autohotkey.com/forum/viewtopic.php?t=69598&start=0&postdays=0&postorder=asc&highlight=arduino

This is the initial release, so there will be bugs, maybe some things are done weird, etc. I just need some feedback :slight_smile:

The first example I'll give is moving a servo with a slider contol in a gui
(GUI picture below)
Moving a servo with a slider control

To use:

  • attach a servo on pin 9

1)Extract the archive
2)Upload Servo.pde sketch to arduino
3)Open Arduino-servo.ahk
4)Edit your com port on line 15
5)Save Arduino-servo.ahk
6)drag Arduino-servo.ahk onto AutoHotkey.exe
7)click and drag and move the slider to move the servo
:smiley:

I included the Autohotkey.exe for ease of use, you can get the executeable from the autohotkey.com site if you would rather. (it's in the zip download)

if you are on linux you need to do a couple things
install wine

$ sudo apt-get install wine

create a link from your linux tty device to emulate the com port
(replace ttyACM0 with the name of arduino that shows up in arduino ide)

$ sudo ln -s /dev/ttyACM0 ~/.wine/dosdevices/com2

make the AutoHotkey.exe file executeable

sudo chmod 777 AutoHotkey.exe

run the scripts like so:

wine AutoHotkey.exe CIRC10.ahk

you dont need to modify the .ahk files, they are setup to use COM2

Servo.zip (239 KB)

Servo-gui.png

Good day

Is it possible to do this for two servos, pan and tilt.
I have been strugling to control two servos with buttons (UP/DOWN/LEFT/RIGHT)in C#, C++ or VB, but with no luck.
I would rely appreciate it if you can help me with this, even if it is sliders for two servos.
Or if you know of a website with source code & scetch to control 2 servo's, please let me know.

Thank you & regards,
Riaan Deyzel

You may be interested in this example :slight_smile:

Origionally posted Windows & Linux (with wine) GUI for controlling your Arduino [with AutoHotkey] - adafruit industries

This next example has 2 buttons. they each send a byte to the arduino.

the included arduino sketch listens for serial command and responds with a Y for 1 or an N for 2

To use:
1)Extract the archive
2)Upload Command.pde sketch to arduino
3)Open Arduino-command.ahk
4)Edit your com port on line 15
5)Save Arduino-command.ahk
6)drag Arduino-command.ahk onto AutoHotkey.exe

Command.zip (238 KB)

Command-gui.png

Here is that example slightly modifed to have more buttons

;
; AutoHotkey Version: 1.x
; Language:       English
; Platform:       Windows XP +??
; Author:         ahklerner / kruzan
;
; Script Function:
;	Arduino GUI Example
;
; set some defaults
#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.
; serial settings
ARDUINO_Port     = COM2
ARDUINO_Baud     = 115200
ARDUINO_Parity   = N
ARDUINO_Data     = 8
ARDUINO_Stop     = 1
; setup arduino serial communication
arduino_setup()
; *****************GUI CODE***************
; add first button
Gui, Add, Button, gButton1_Press, Servo 1 Left
Gui, Add, Button, gButton2_Press, Servo 1 Center
Gui, Add, Button, gButton3_Press, Servo 1 Right
; add first button
Gui, Add, Button, gButton4_Press, Servo 2 Up
Gui, Add, Button, gButton5_Press, Servo 2 Center
Gui, Add, Button, gButton6_Press, Servo 2 Down

Gui, Add, Text,, Arduino Response:
Gui, Add, Text, vReply,**NO DATA RECEIVED YET**

; show the gui (it will be auto sized and positioned)
Gui, Show
; *****************************************
return
; ******button action code ******
Button1_Press:
	GuiControl,,Reply, % arduino_send("1")
return
Button2_Press:
	GuiControl,,Reply, % arduino_send("2")
return
Button3_Press:
	GuiControl,,Reply, % arduino_send("3")
return
Button4_Press:
	GuiControl,,Reply, % arduino_send("4")
return
Button5_Press:
	GuiControl,,Reply, % arduino_send("5")
return
Button6_Press:
	GuiControl,,Reply, % arduino_send("6")
return

;*************************************************************************************
; *****Do not edit below this line unless you want to change the core functionality of the script*****
;*************************************************************************************
; called when the gui is closed
;also called when program exits
GuiClose:
OnExit:
	; make sure to cleanly shut down serial port on exit
	arduino_close()
; this is important!! or else theprogram does not end when closed
ExitApp
#include %A_ScriptDir%\include\Arduino.ahk

Here is example Arduino Sketch (needs servo code)

int command = 0 ;

void setup() {
  // put your setup code here, to run once:
  delay(1000);          // wait a bit before starting
  Serial.begin(115200);   // set up Serial library at 9600 bps
}

void loop() {
  // put your main code here, to run repeatedly: 
    if (Serial.available() > 0) {
    command = Serial.read();
    if (command == "1"){
	//pan left
    }if (command == "2"){
	//pan center
    }if (command == "3"){
	//pan right
    }if (command == "4"){
	//tilt up
    }if (command == "5"){
	//tilt center
    }if (command == "6"){
	//tilt down
    }
    command = 0;  
}
}

This example is getting sensor data.

I decided to base it off of the oomlut CIRC10 example. [ http://www.oomlout.com/a/products/ardx/circ-10 ]

no modifications to the arduino sketch above are needed.

see image below what the example gui looks like on windows xp with zune theme

The necessary files are attached.

once you have completed the circuit as directed, and uploaded the arduino sketch from the example

To use:
1)Extract the archive
2)Open CIRC10.ahk
3)Edit your com port on line 15
4)Save CIRC10.ahk
5)drag CIRC10.ahk onto AutoHotkey.exe

:slight_smile:

CIRC10-gui.png

CIRC10.zip (238 KB)

Good day

I would like to know of the following scetch will work for the "Here is that example slightly modifed to have more buttons" to control two servos on D9 & D10?, as the code supplied does not have servo code in it.

#include <Servo.h> 
 
Servo myservo1;  
Servo myservo2; 
int command = 0 ;

void setup() {
// put your setup code here, to run once:
delay(1000);          // wait a bit before starting
Serial.begin(9600);   // set up Serial library at 9600 bps
myservo1.attach(9);
myservo2.attach(10);
}

void loop() {
  // put your main code here, to run repeatedly: 
    if (Serial.available() > 0) {
    command = Serial.read();
    myservo1.write(command);                  // sets the servo position according to the scaled value 
    Serial.println(command);
    delay(15); 
    myservo2.write(command);                  // sets the servo position according to the scaled value 
    Serial.println(command);
    delay(15);
    
    if (command == '1')
	//pan left
    if (command == '2')
	//pan center
    if (command == '3')
	//pan right
    if (command == '4')
	//tilt up
    if (command == '5')
	//tilt center
    if (command == '6')
	//tilt down
    
    command = 0;  
}
}

sorry, i thought if you were far enough along to try to make a gui to move your servos, you would already know what values to use to point the servo to a position

maybe this helps?

#include <Servo.h> 
 
Servo myservo1;  
Servo myservo2; 
int command = 0 ;

void setup() {
// put your setup code here, to run once:
delay(1000);          // wait a bit before starting
Serial.begin(9600);   // set up Serial library at 9600 bps
myservo1.attach(9);
myservo2.attach(10);
}

void loop() {
  // put your main code here, to run repeatedly: 
    if (Serial.available() > 0) {
    command = Serial.read();
    if (command == '1'){
	//pan left
	myservo1.write(0);                 
	Serial.println(command);
	delay(15); 
    }else if (command == '2'){
	//pan center
	myservo1.write(90);                 
	Serial.println(command);
	delay(15); 
    }else if (command == '3'){
	//pan right
	myservo1.write(180);               
	Serial.println(command);
	delay(15); 
    }else if (command == '4'){
	//tilt up
	myservo2.write(0);                  
	Serial.println(command);
	delay(15);
    }else if (command == '5'){
	//tilt center
	myservo2.write(90);                
	Serial.println(command);
	delay(15);
    }else if (command == '6'){
	//tilt down
	myservo2.write(180);              
	Serial.println(command);
	delay(15);
    }
    command = 0;  
}
}

Thank you very much kruzan.

I will give it a go this weekend, and see how it works.

Regards,
Riaan Deyzel

Hi,

I have tested this code, and it did not work, I will look further for the same app to control 2 servo's, but will look for c# code, as I do not know AutoHotKey at all.

Regards,
Riaan Deyzel

In the arduino code command is defined as int. But void loop says (command == "1"). This is not okay. Changing it to if (command == '1') is better. But is seems akh is not sending a '1'. If I change to if (command == 1) than arduino recognises the command form akh. This way sending commands to Arduino works wel. However the akh GUI is still showing chineese caracters in response. So help is needed.

My arduino code with lcd shield:

#include <LCD4Bit_mod.h> // initialise LCD library
LCD4Bit_mod lcd = LCD4Bit_mod(2);

int command = 0 ;
char buf[12];

void setup() {
delay(1000); // wait a bit before starting
Serial.begin(115200); // set up Serial library at 9600 bps
lcd.init();
lcd.clear(); // clear screen of old display
delay(1000); // time to see the cleared screen
lcd.printIn("Start"); // visible start after reset
delay(1000); // time to see the "Start" displayed
lcd.clear(); // clear screen before display of first value
delay(1000);
}

void loop() {
if (Serial.available() > 0) {
lcd.clear();
command = Serial.read();
if (command == 1){show();}
if (command == '2'){show();} // faulty
if (command == '3'){show();} // fauly
if (command == 4){show();}
if (command == 5){show();}
if (command == 6){show();}
command = 0;
}
}

void show(){
Serial.println(command);
lcd.printIn(itoa(command, buf, 10));
}

G'day,
I'm hoping you might have some ideas how I could expand upon this...

Specifically I wish to have several sliders in AHK controlling several servos (and perhaps some examples of controlling RGB LEDs)
I can't figure out quite how to do this... Send a signal from AHK to prepare the Arduino code for incoming servo data? I've tried a bunch of things and nothing is working.

Kruzan, you seem to be some kind of genius on this topic... is there any chance you could help us out with some pointers on getting multiple sliders sending data to multiple servos via AHK?

Cheers

Ehi, where is the Library you were talking about at the beginning of this post?

Did you manage to complete it?

I want to make an autohotkey script to control my arduino and make it a stand alone (with a touch screen and everything). Your library sounds very interesting!

I'm trying to get the arduino-command.ahk
To run and I keep getting the error:

Error in #include file "include\Arduino.ahk":
Call to nonexistent function.

Specifically: isFunc(f:=" OnSerialData")

What is wrong?

The "Arduino.ahk" is to run on your PC not on your arduino.
".ahk" is the extension for AutoHotkey scripts. To run it on your (windows) pc, drag it onto AutoHotkey.exe. It does make no sense to include it in the arduino sketch.

Hi Kruzan,
Went through your servo motor controlling via AutoHotKey.Thanks for sharing your knowledge with us.

I am trying to receive the data sent by Arduino (lets say some text) at serial port. And then once I receive it, I want AutoHotKey code to speak it for me. I mean convert that text into speech.

I found below line fo code to convert text to speech which works fine as well.

"ComObjCreate("SAPI.SpVoice").Speak("Peter Piper picked a peck of pickled peppers")"

Need you help in receiving the data from Arduino and pass it to above Speak function. Could not find the AutoHotKey code to receive data from serialPort.

Is there any chance you can help me with this ? Hoping for positive response.

Thanks in Advance.
Gunjan