Create an Applescript/Arduino Alert Flag.

13,769

38

44

Posted

Introduction: Create an Applescript/Arduino Alert Flag.

Have you ever felt like the mail sound on your Mac just wasn't enough? Simple sounds and alerts just don't cut it for you? You want something more apparent and rewarding? If so, this is the Instructable you've been looking for.

In this Instructable, I'll show you how to hook up your Arduino to your Mac and throw up an actual flag when the alert of your choice occurs. In our example, we'll be doing an email alert, but you can use AppleScript to call this when almost anything happens.

I've created a little Instructables flag to go off when I receive comments on my instructables.

Let's get started!

Step 1: What You Need

Hardware:

An Arduino: I know at least that the Duemilanove and the Diecimila will work. I don't know if older board versions will work.

A Flag: I made an instructables flag to alert me when I get instructables comment.

A 47 Ohm resistor: This is to prevent the Arduino from resetting at the close of a serial connection

A Servo

Wires to connect the Arduino to the Servo. My wires were a bit longer than needful.

Software:

Tod Kurt's Arduino Serial C code. Tod put this code up which makes it tremendously easy to communicate with the Arduino. Files included here. For more info look here:
http://todbot.com/blog/2006/12/06/arduino-serial-c-code-to-talk-to-arduino/

- Arduino Code

- Run Flag Applescript

Step 2: Setup the Arduino

We need to get the physical side of this equation set up first.

Out of the Servo, There are 3 wires: Voltage, Ground, and Control. In this setup, we're plugging those into 5v, Ground and Pin 9.

Also, we're going to put a 47ohm resistor into the 3v3 pin and the reset pin. This will prevent the arduino from resetting every time there's a serial connection closed. This was a real pain for me to deal with early on, I'd written the software to handle the reset with a delay, but it wasn't ever working the way I wanted it to. Resistor solves that problem easily.

Note: According to the source I got this from, the Resistor causes an excessive 20mA current in the Arduino when the reset button is pressed. The person who tested it said it's outside of specifications, but still worked. Avoid resetting the Arduino when you have the Resistor is connected.

Software
Load the included software onto the Arduino. Make changes as you see fit. Essentially, you're sending a 0 or a 1 to the arduino, and it's going to turn the flag up or down based on that.

Step 3: Setup the Servo & Flag

Tape Flag to pole.
Tape pole to the servo


...Pretty simple.

Step 4: Setup Software Communication

First, you'll need to compile Tod Kurt's Arduino serial communication script. This is super easy.
Open up the terminal and navigate your way to where you have the arduino-serial.c file
Type in the following:

gcc -o arduino-serial arduino-serial.c

Once it's done compiling, type:
./arduino-serial
and it should print to the screen usage information.

Let's test out our Flag. In the terminal, navigate to where your newly made arduino-serial script and type in the following:

./arduino-serial -b 9600 -p /dev/tty.usbserial-A4001lGx -s 1

You'll need to substitute your Arduino's serial port. If you're not sure what this is, go into the Arduino software, and look under Tools->Serial Port. The order is important here. The -b is baud, and must be set before the port (-p) is set. -s is the message to be sent.

This should raise the flag, You can lower it by changing the "1" at the end of that line to a "0"(zero)

Once that works, the next thing is to setup the associated AppleScript. With this Applescript, you're going to have to tell it where everything is. For ease, I stuck everything in the same spot, namely: ~/Library/Scripts/ , of course you can use whatever you'd like.
Once you've got your Applescript setup, give it a go by pressing that big green "run" button at the top.

One more thing and we're set. Now we need to setup Mail.app to run the Applescript.
Open up Mail and go into the preferences.
Select Rules
Add a Rule
In the rule, set the conditions as you like. For me, I've setup the rule to have the "from" address contain "instructables.com", so it will fire when I receive anything from Instructables.

You're finished! If you have a method of testing it out, give it a shot.

Step 5: Final Thoughts

You don't have to use Mail.app to take advantage of this, obviously. You can fire the script from any application that will access Applescript, including the Finder.

Windows users: I'm not familiar enough with Outlook to know if you have ways to triggering script execution, but I imagine someone here would have no problem figuring it out.

Because of the way Applescript is executed, the delay in the script will stop everything from happening while it waits. I haven't explored fixing this yet, but I imagine it would be done on the side of the Arduino, rather than the Applescript. For instance, send two parameters to the Arduino: The up/down bit, and a duration...



I hope you've enjoyed the Instructable. I put it together to see if I could figure out a little bit more about interacting with the real world from the computer world.

Share

    Recommendations

    • Science of Cooking

      Science of Cooking
    • Microcontroller Contest

      Microcontroller Contest
    • Spotless Contest

      Spotless Contest
    user

    We have a be nice policy.
    Please be positive and constructive.

    Tips

    Questions

    44 Comments

    My servo doesn't move when I enter the command in the terminal. Can someone help me?

    when I download runflag it downloads and saves as an unknown file type so what do I need to select to open it?
    I tried adobe application installer but it didn't work...so please help!

    runflag is an applescript file. If you're using it on Windows, it won't work.

    The applescript simply calls runs the C script which puts the flag up. Depending on what email client you use, you may be able to write a script for that which will run the same C shell script in windows.

    ok...so is there any way we can do thisbut on windows XP?

    No.
    You would be able to use to c script, but not the Applescript. You. Would have to find a way to call the c script from within your email client.

    Is it possible to use this method but for windows XP...hope there is...I would really love any help on this!
    Thanks

    Excellent post. This will be my first servo project!

    great write up, thanks a ton. I am very new to this. I am using arduino to control a dc motor, and i wanted to make a voice command script recognized by speech recognition + applescript to run an appropriate forward or backward movement. i had the arduino portion set up and working by entering either an f (for forward) or b (for backward) in the serial monitor of arduino, but after i compiled Tom Kurt's code following your instructions the motor no longer turns. I can send the character f or b through terminal and it successfully makes it through to arduino (proven through a serial.println to the serial monitor), but no turn on the motor.. do I need to setup another serial port?

    here is my test terminal command:
    ./arduino-serial -b9600 -p /dev/tty.usbserial-A700dYg4 -s f

    results in: "Running forward" in the serial monitor

    and here's my arduino code:

    #include

    AF_DCMotor motor(2, MOTOR12_1KHZ);

    void setup() {
    Serial.begin(9600);
    motor.setSpeed(255);
    }

    void loop() {
    if (Serial.available() > 0) {
    int inByte=Serial.read();
    switch (inByte) {
    case 'f':
    Serial.println("Running forward");
    motor.run(FORWARD);
    delay(500);
    motor.run(RELEASE);
    break;
    case 'b':
    Serial.println("Running backward");
    motor.run(BACKWARD);
    delay(500);
    motor.run(RELEASE);
    break;
    default:
    Serial.println("default value reached");
    }
    }
    }

    thanks for your time/advice!