As many of you know, I am a nerd. My most recent nerdy acquisition was an Arduino. For those that have no idea what an Arduino is or does, let me give you a quick explanation- straight from the Arduino website:
Arduino is a tool for making computers that can sense and control more of the physical world than your desktop computer. It’s an open-source physical computing platform based on a simple microcontroller board, and a development environment for writing software for the board.
Arduino can be used to develop interactive objects, taking inputs from a variety of switches or sensors, and controlling a variety of lights, motors, and other physical outputs. Arduino projects can be stand-alone, or they can be communicate with software running on your computer (e.g. Flash, Processing, MaxMSP [or in our case, AutoIt].) The boards can be assembled by hand or purchased preassembled; the open-source IDE can be downloaded for free.
The Arduino programming language is an implementation of Wiring, a similar physical computing platform, which is based on the Processing multimedia programming environment.
http://www.arduino.cc/en/Guide/Introduction
When I first discovered Arduino I was delighted as it had some great features – features that I was looking for. First things first, it was cheap. Very cheap. For a starter kit (Includes the Duemilanove version of the Arduino, USB cable, breadboard, jumpers, assortment of components from resistors to LEDs to switches) cost me AU $59.95. For the basic Arduino alone, you can expect to pay around AU $40. For the features and power this board has, it is a great bargain. Along with my starter kit I bought a 100 Common Anode RGB LEDs, a larger breadboard and various other components to get enough gear to have a proper play.
After getting over the first hurdle of getting the basic Arudino tutorials up and running and having a general play, I wanted to move onto my next challenge- making it work with AutoIt. If you didn’t know AutoIt is my current language of choice; it was my first language and to be honest, I’m too lazy to learn anything “better”, keeping in mind that for an interpreted scripting language it is very powerful indeed- more than enough functionality for what I use it for. And for my basic projects it was perfect.
The first thing to do was to work out how to communicate with the Arduino and my PC. Behold the first step, the serial communication. The best example I have found about serial communication is below:
Serial may sound like a tasty breakfast food, but its actually quite different. The word serial means “one after the other.” For example, a serial killer doesn’t stop with one murder, but stabs many people one after the other. Serial data transfer is when we transfer data one bit at a time, one right after the other.
Information is passed back & forth between the computer and Arduino by, essentially, setting a pin high or low. One side sets the pin and the other reads it. It’s a little like Morse code, where you can use dits and dahs to send messages by telegram. In this case, instead of a long cable, its only a few feet.
http://www.ladyada.net/learn/arduino/lesson4.html
Or a picture for those visually inclined…
Serial communication in the Arduino is done using the Serial Library (who would have guessed it!)

The library is easy and simple to use, and it includes a set of functions to communicate between your computer and the Arduino:
Serial.Begin()
Serial.end()
Serial.available()
Serial.read()
Serial.flush()
Serial.print()
Serial.println()
Serial.write()
The main functions we will use is Serial.Begin(), as it is required to start the serial communication; Serial.Available(), so we know when data is being sent to the Arduino; Serial.Read() and Serial.Write() for directly reading data. So how can we say, control a LED with Autoit.
To start lets define what we want to achieve. At the end of this project want to be able to turn on 2 LEDs, a red and a yellow one. First lets hook our LEDs to our breadboard and Arduino. In this case I will use pins 12 and 13, placing an appropriate resistor on pin 12 as pin 13 already has one inbuilt. Here is some pictures of the final product:

How I wired the LEDs.

How I wired the Arduino
And just incase you have no idea what I have done here is a diagram created using Frizting. Frizting makes it very easy to create diagrams so it is worth trying it out if you haven’t used it already.

This is how I wired the Arduino to the breadboard.
Now let us make sure that we’ve connected everything up correctly. To do that I devised a simple test based on the blinking example; flash one LED then the other LED… Simple!
/* Sketch Name..: Serial LED Test
* Author.......: Brett Francis
* Website......: http://www.signa5.com
* Description..: Turn a LED on a pin either on or off. Used as a test
* for connections so we can use the serial comunication
* sketch.
* Date Created.: 11/06/2010
* Modifed......: -
*/
//LED Pins
int yelLED = 13; //Inbuild resistor
int redLED = 12;
void setup ()
{
//Set the LED pins to be an OUTPUT
pinMode (yelLED, OUTPUT);
pinMode (redLED, OUTPUT);
}
// Loop
void loop ()
{
//Turn yellow on, red off
digitalWrite(yelLED, HIGH);
digitalWrite(redLED, LOW);
//wait 1 second
delay(1000);
//Turn yellow off, red on
digitalWrite(yelLED, LOW);
digitalWrite(redLED, HIGH);
//wait 1 second
delay(1000);
} |
So now we have that working, lets work on using serial communication. I decided that I would create a simple protocol to turn a specific LED on and then off.
LSCR
L is an integer translating to which LED to control. In this case, 1 is for the red LED and 2 is for the yellow.
S is another integer which dictates the state- 1 sets the state of the LED to on, or 0 to turn it off. The CR is a carriage return, so the Arduino knows when we have stopped sending data.
Lets start creating the sketch that will take commands from our AutoIt program. To manipulate the received data, I will be using the string library, formally known as TextString.
/* Sketch Name..: LED Control using Serial Communication
* Author.......: Brett Francis
* Website......: http://www.signa5.com
* Description..: Turn a LED pin either on or off using an AutoIt program
* comunicating using a serial connection.
* Date Created.: 11/06/2010
* Modifed......: -
*/
// Include Function Library
#include
//LED Pins
int yelLED = 13; //Inbuild resistor
int redLED = 12;
//Internal
int curpin;
int curstate;
//Lets the function know if we have a string to display
int stringsent = false;
//EOFChar
char myEOF = 13;
// Create the input string
String inString = String(4);
void setup ()
{
//Set the LED pins to be an OUTPUT
pinMode (yelLED, OUTPUT);
pinMode (redLED, OUTPUT);
//Start Serial Communication
Serial.begin(9600);
//Print a message to the user
Serial.println("Serial Communication Ready");
}
// Loop Through
void loop ()
{
if(Serial.available() > 0)
{
getIncomingChars();
}
if (stringsent == true)
{
switchLED();
inString = "";
}
}
//Function to turn LED on or off
void switchLED()
{
//We know we have a string now
stringsent = false;
//PROTOCOL
// LS = LED (1 CHAR) + STATE (1 CHAR)
//Trick to convert a string directly to a number
char str1 = inString.charAt(0);
curpin = str1 - '0';
char str2 = inString.charAt(1);
curstate = str2 - '0';
//Convert the state 0, 1 to something more usable.
if (curstate == 1)
{
curstate = HIGH;
}
else
{
curstate = LOW;
}
// Turn the relevant LED on or OFF
if (curpin == 1)
{
digitalWrite(redLED, curstate);
}
else if (curpin == 2)
{
digitalWrite(yelLED, curstate);
}
}
void getIncomingChars() {
// read the incoming data as a char:
char inChar = Serial.read();
// if you're not at the end of the string, append
// the incoming character:
if (inChar == myEOF)
{
stringsent = true;
}
else
{
inString.append(inChar);
}
} |
Hopefully the above code should be able to able to explain what is happening but here is the basics:
The setup() function initiates serial communication, sets the LED pins to be outputs (opposed to inputs). The next bit (the loop) will loop through, see if we are getting data and if so, return data. The function that gets the data is getIncomingChars(). It basically loops through the input buffer until it hits the CR, where it will return that value and set a variable to true. It is by setting the variable to true that we know we have to turn a light on or off which leads to the last function, switchLED(). It takes the input string and uses the string functions to extract the first (LED number) and second (state) from the string. Then it turns the state into a value the digital functions can understand, and turns the relevant LED on or off. Pretty simple eh?
Now to the AutoIt code. It is obvious from how we are communicating with the Arduino that we need some sort of serial UDF. Thankfully martin has come to the rescue with his UDF to communicate with the serial port (and in our case a USB serial port). If you play with the example you can send 11, 10 to turn the red LED on or off. So we know that part works lets create our own code. What we will create first is something similar to our test, and then I’ll add some additional scripts. Really what you can do is only limited by your imagination.
With all of the code examples, the Arduino was connected to COM Port 6. If you can’t figure out what it should be, open the plug your Arduino in and open the Arduino developer program. From there take a look at the ports listed in the Tools > Serial Port menu. If you find more than one, note the ports, disconnect your Arduino and look again. The one that is missing should be your Arduino.
;Include the Serial UDF
#include 'CommMG.au3'
;Internal for the Serial UDF
Global $sportSetError = ''
;COM Vars
Global $CMPort = 6 ; Port
Global $CmBoBaud = 9600 ; Baud
Global $CmboDataBits = 8 ; Data Bits
Global $CmBoParity = "none" ; Parity
Global $CmBoStop = 1 ; Stop
Global $setflow = 2 ; Flow
;Start up communication with the Arduino
_CommSetPort($CMPort, $sportSetError, $CmBoBaud, $CmboDataBits, $CmBoParity, $CmBoStop, $setflow)
;Start a loop
While 1
;Turn yellow on and red off
_CommSendstring("21" & @CR)
_CommSendstring("10" & @CR)
;Wait a second
Sleep (1000)
;Turn yellow off and red on
_CommSendstring("20" & @CR)
_CommSendstring("11" & @CR)
Sleep (1000)
WEnd |
Here are a few different additional examples as promised.
Police Style Strobe
A strobe similar to those you see on emergency service vehicles and the like.
;Include the Serial UDF
#include 'CommMG.au3'
; What LED are we using?
Global $light = 1
;Internal for the Serial UDF
Global $sportSetError = ''
;COM Vars
Global $CMPort = 6 ; Port
Global $CmBoBaud = 9600 ; Baud
Global $CmboDataBits = 8 ; Data Bits
Global $CmBoParity = "none" ; Parity
Global $CmBoStop = 1 ; Stop
Global $setflow = 2 ; Flow
;Start up communication with the Arduino
_CommSetPort($CMPort, $sportSetError, $CmBoBaud, $CmboDataBits, $CmBoParity, $CmBoStop, $setflow)
;Start a timer
$timer = TimerInit()
;Start a loop
While 1
;Check if we should change the LED
If TimerDiff($timer) >= 750 Then
;Turn the current one off so it doesn't stay light
_CommSendstring($light & "0" & @CR)
;Change the light
If $light = 1 Then
$light = 2
Else
$light = 1
EndIf
;Reset the timer
$timer = TimerInit()
EndIf
;Turn it off
_CommSendstring($light & "0" & @CR)
;Wait 50ms
Sleep(50)
;Turn it on
_CommSendstring($light & "1" & @CR)
WEnd |
Countdown Clock
What I like to call “Countdown Clock” – the strobe gets faster as the seconds progress while the other LED flashes every second. A little bit of fun really… It is also my favourite examples…
;Include the Serial UDF
#include 'CommMG.au3'
;Value for the red to stay off for (ms)
$waitoff = 900
;Internal Timing
$waiton = 1000 - $waitoff
$wait = $waitoff
$oldsec = @SEC
$freq = _GetFreq()
;LED State
$redon = False
$yelon = False
;Internal for the Serial UDF
Global $sportSetError = ''
;COM Vars
Global $CMPort = 6 ; Port
Global $CmBoBaud = 9600 ; Baud
Global $CmboDataBits = 8 ; Data Bits
Global $CmBoParity = "none" ; Parity
Global $CmBoStop = 1 ; Stop
Global $setflow = 2 ; Flow
;Start up communication with the Arduino
_CommSetPort($CMPort, $sportSetError, $CmBoBaud, $CmboDataBits, $CmBoParity, $CmBoStop, $setflow)
; Start a timer
$timer = TimerInit()
$timer2 = TimerInit()
;Start a loop
While 1
;So we don't get the frequency every 2ms...
If $oldsec <> @SEC Then $freq = _GetFreq()
;Make the red LED flash once a second.
;It is using an alternative timing for on and off states
If TimerDiff($timer) >= $wait Then
If $redon = True Then ;LED IS ON- TURN IT OFF
$redon = False
$wait = $waitoff
_CommSendstring("10" & @CR)
Else
$redon = True
$wait = $waiton
_CommSendstring("11" & @CR)
EndIf
$timer = TimerInit()
EndIf
;Flash the Yellow LED at the correct frequency.
;Frequency is caclulated with _GetFreq every second.
;The higher the seconds, the higher the freqency.
If TimerDiff($timer2) >= $freq Then
If $yelon = True Then
$yelon = False
_CommSendstring("20" & @CR)
Else
$yelon = True
_CommSendstring("21" & @CR)
EndIf
$timer2 = TimerInit()
EndIf
WEnd
Func _GetFreq()
;This function takes the current seconds count
;and converts it to a frequency.
$oldsec = @SEC
Select
Case $oldsec > 55
Return 20
Case $oldsec > 50
Return 50
Case $oldsec > 40
Return 100
Case $oldsec > 30
Return 200
Case $oldsec > 20
Return 300
Case $oldsec > 10
Return 400
Case Else
Return 500
EndSelect
EndFunc ;==>_GetFreq |
So for now thats it. This was a huge post, but there was a lot to cover… Later I will explore playing with more LEDs and maybe even PWM and RGB LEDs! Oh this is so much fun
All code files are available for download:
Code Files