controlling an arduino with cocoa

13
http://www.instructables.com/id/Controlling-an-Arduino-with-Cocoa/ Home Sign Up! Explore Community Submit All Art Craft Food Games Green Home Kids Life Music Offbeat Outdoors Pets Photo Ride Science Tech Controlling an Arduino with Cocoa (Mac OS X) or C# (Windows) by computergeek on October 12, 2009 Table of Contents License: Attribution Non-commercial Share Alike (by-nc-sa) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 Intro: Controlling an Arduino with Cocoa (Mac OS X) or C# (Windows) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 File Downloads . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 step 1: Stuff you need: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 step 2: Coding the app . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 step 3: Designing the GUI (Graphic User Interface) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 step 4: Programming the Arduino . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 step 5: Using, Notes, & More Ideas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 step 6: How this works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 step 7: Coding in C# (Windows) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 Related Instructables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 Advertisements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12

Upload: mc-rene-solis-r

Post on 18-Nov-2014

123 views

Category:

Documents


1 download

DESCRIPTION

Good Tutorial for Cocoa & C# using the arduino

TRANSCRIPT

Page 1: Controlling an Arduino With Cocoa

http://www.instructables.com/id/Controlling-an-Arduino-with-Cocoa/

Home   Sign Up!   Explore   Community   Submit

   All     Art     Craft     Food     Games     Green     Home     Kids     Life     Music     Offbeat     Outdoors     Pets     Photo     Ride     Science     Tech

Controlling an Arduino with Cocoa (Mac OS X) or C# (Windows)by computergeek on October 12, 2009

Table of Contents

License:   Attribution Non-commercial Share Alike (by-nc-sa) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2

Intro:   Controlling an Arduino with Cocoa (Mac OS X) or C# (Windows) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2

File Downloads . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2

step 1:   Stuff you need: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2

step 2:   Coding the app . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3

step 3:   Designing the GUI (Graphic User Interface) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7

step 4:   Programming the Arduino . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9

step 5:   Using, Notes, & More Ideas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10

step 6:   How this works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10

step 7:   Coding in C# (Windows) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10

Related Instructables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12

Advertisements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12

Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12

Page 2: Controlling an Arduino With Cocoa

http://www.instructables.com/id/Controlling-an-Arduino-with-Cocoa/

License:   Attribution Non-commercial Share Alike (by-nc-sa)

Intro:  Controlling an Arduino with Cocoa (Mac OS X) or C# (Windows) No I'm not talking about controlling an Arduino with a chocolate bar or cocoa powder. I'm talking about the programming language, Objective-C. So what is cocoa?Cocoa is a set of frameworks written in the programming language of Objective-C for Mac OS X. No there isn't a test at the steps so you don't have to worry aboutremembering all of this. Just know that when I talk about cocoa I'm not trying to make you hungry.

 Now I understand some people are probably wondering why you would want to use Cocoa instead of Processing, the reason I have is: if you use Cocoa over Processingyou can have a full Computer Application(e.g. Safari, Mail, iCal) that can have a physical output. (e.g. Lights, motors, servos)

So Have Fun, Work hard, & Play nice! Please ask any questions you have!

Notes:- This will only work for Mac OS X & Windows- The source code is attached below.- If you don't want to code the app, or you don't have xcode, you can find the app for OS X in the source below under: 'Build -> Debug'- The instructions for Windows are in step 7

File Downloads

Arduino Controller.zip (2 MB)[NOTE: When saving, if you see .tmp as the file ext, rename it to 'Arduino Controller.zip']

step 1: Stuff you need:Hardware:- Arduino- A-B USB Cable- Computer with Mac OS X

Software:- Xcode- Arduino IDE

Page 3: Controlling an Arduino With Cocoa

http://www.instructables.com/id/Controlling-an-Arduino-with-Cocoa/

step 2: Coding the appHopefully this makes sense. If you need help please ask any questions you have.Note:  If you don't want to code the app, or you don't have xcode, you can find the app in the source code download under: 'Build -> Debug'

Create the project: 1) Open Xcode 2) Press 'Command + shift + n' This will open the new project dialog 3) Select 'Cocoa Application' under "Mac OS X -> Applications" Press Next 4) Call the app what you want. I'm calling mine "Arduino Controller"

Add your class: 5) Once the project is created, Press 'Command + n' 6) Select the Objective-C Class option from the Classes option under the Mac OS X section 7) Call it MainController. Then press finish

Add the code: 8) Open MainController.h 9) After the '}' & before the '@end' add the code:

-(IBAction)ledOn:(id)sender;

-(IBAction)ledOff:(id)sender; 

 10) Open MainController.m 11) After the code ''@implementation MainController" & before the '@end' add the code: 

-(IBAction)ledOn:(id)sender{

popen("echo i > /dev/tty.usbserial-A6006hmi", "r");

}

 

-(IBAction)ledOff:(id)sender{

popen("echo o > /dev/tty.usbserial-A6006hmi", "r");

}

Finishing The Coding:

12) Press 'Command + b' & press save all13) Double click on MainWindow.xib. It is located under Resources

14) Interface Builder will open

Page 4: Controlling an Arduino With Cocoa

http://www.instructables.com/id/Controlling-an-Arduino-with-Cocoa/

Image Notes1. Open Xcode

Image Notes1. New Project Window2. Select "Cocoa Application"

Image Notes1. You should see something like this

Image Notes1. Press Command+n to get the new file dialog2. Select Objective-C Class3. Under Cocoa

Image Notes1. Call it MainController.m

Image Notes1. Files added. Don't worry if it's not in the classes folder2. In the file MainController.h add the code listed above after the '}' & before theline '@end'

Page 5: Controlling an Arduino With Cocoa

http://www.instructables.com/id/Controlling-an-Arduino-with-Cocoa/

Image Notes1. Copy the code above to the section between '@implementationMainController' & '@end'

Image Notes1. Add the code above between the brackets for both methods.

Image Notes1. Press 'Command+b' to get something like this

Image Notes1. Interface Builder (IB)

Image Notes1. Drag 2 buttons from the library pane to the window2. Library Pane

Image Notes1. Double click on the buttons to rename them.

Page 6: Controlling an Arduino With Cocoa

http://www.instructables.com/id/Controlling-an-Arduino-with-Cocoa/

Image Notes1. Resize the window

Image Notes1. Drag to here2. Find the thing that looks like this. (NSObject)

Image Notes1. Go to the Identity inspector for the object you just created. (Command+6)

Image Notes1. Change this to MainController

Image Notes1. Go to the Connections inspector for the object you just created.(Command+5)

Image Notes1. Click on the little round circle and drag to the correct button.

Page 7: Controlling an Arduino With Cocoa

http://www.instructables.com/id/Controlling-an-Arduino-with-Cocoa/

Image Notes1. Press build and go

step 3: Designing the GUI (Graphic User Interface)If you have questions please ask them!

Creating the interface:1) With interface builder open, find the buttons. Pick a style you like & drag 2 of them over to the window2) Double click on them to rename one "On" & the other "Off"3) Resize the window if you want4) In the library find the thing that looks like a blue cube (NSObject) & drag it to the window with the list of files5) Click on the object you just added to the window6) Press 'Command + 6' to open the Identity Inspector7) Under the section 'Class' type MainController8) Press 'Command + 5' to open the Connections Inspector9) Click & drag the little circle to each of the respective buttons. (e.g. ledOn to the On button)10) Save & close Interface builder11) Press "Build & Go" In Xcode to run the cocoa app

Image Notes1. Interface Builder (IB)

Image Notes1. Drag 2 buttons from the library pane to the window2. Library Pane

Page 8: Controlling an Arduino With Cocoa

http://www.instructables.com/id/Controlling-an-Arduino-with-Cocoa/

Image Notes1. Double click on the buttons to rename them.

Image Notes1. Go to the Connections inspector for the object you just created. (Command+5)

Image Notes1. Resize the window

Image Notes1. Drag to here2. Find the thing that looks like this. (NSObject)

Image Notes1. Go to the Identity inspector for the object you just created. (Command+6)

Image Notes1. Change this to MainController

Page 9: Controlling an Arduino With Cocoa

http://www.instructables.com/id/Controlling-an-Arduino-with-Cocoa/

Image Notes1. Click on the little round circle and drag to the correct button.

Image Notes1. Press build and go

step 4: Programming the ArduinoUploading the sketch to your arduino:1) Open the .pde file, in the source download, in the Arduino IDE2) Connect your arduino & press Upload to I/O Board3) Once the file is uploaded open the serial monitor4) Go to the Arduino Controller program. If it's not running open it.5) You can now use the On & Off buttons to control the led built in on pin 13 the arduino

Image Notes1. Upload to I/O Board button

Image Notes1. Serial Monitor

Page 10: Controlling an Arduino With Cocoa

http://www.instructables.com/id/Controlling-an-Arduino-with-Cocoa/

step 5: Using, Notes, & More IdeasTo use the controller, make sure the serial monitor in the Arduino IDE is running. This is important!  To use the app you can either press build & go in xcode or find the app in the xcode project under:'Build -> Debug'

 Notes:- This will only work on Mac OS X- The Source code is attached on the first step.- If you don't want to code the app, or you don't have xcode, you can find the app in the source code under: 'Build -> Debug'

Ideas:- Make it so there are two buttons that control a servo's rotation- Make a small piano- Make a physical output for your mac- Make a pan & tilt webcam

step 6: How this worksIn the cocoa application there are 2 methods, ledOn & ledOff, that are activated by the pushing of the buttons. Within each of those methods there is a line of code thatlooks like this:popen("echo i > /dev/tty.usbserial-A6006hmi", "r");This code sends the letter i to the Serial port: /dev/tty.usbserial-A6006hmi which is received by the arduino

With in the arduino program there is the code:if (Serial.available() > 0) {    // read the incoming byte:    incomingByte = Serial.read();    if(incomingByte == 105){      digitalWrite(ledPin, HIGH);    }    else if(incomingByte == 111){      digitalWrite(ledPin, LOW);    }This takes the decimal value from the sent character on the serial port connection. If it is a i then it turns the led on if it is a o it turns it off. 

For the Arduino to receive the signal from the cocoa app the serial monitor in the Arduino IDE needs to be open.

If this doesn't make sense it's fine. This is kind of a reference if you want to expand the functionality of the program.

step 7: Coding in C# (Windows) Well I finally got the windows part done!! It turns out you don't have to leave the Adruino IDE open when you use C# for programming. You need either Visual Studio or Visual C# express (Free) . If you don't want to write the app the setup is included in the source code file at the intro. I would NOT recommend trying to write this programif you don't have experience writing code in C#.

Here are the steps:

Picture 1) Create a new projectPicture 2) Choose "Windows Form Application" & Name it what you wantPicture 3) This is what you should havePicture 4) Find the button in the ToolboxPicture 5)  Get 2 buttons & drag them to the windowPicture 6) Left click on the first one. Under properties, go to title & rename it "On"Picture 7) Rename the second button & resize the windowPicture 8) Select both of the buttons & double click them to create methods for themPicture 9) Paste in the code attached below

Page 11: Controlling an Arduino With Cocoa

http://www.instructables.com/id/Controlling-an-Arduino-with-Cocoa/

Page 12: Controlling an Arduino With Cocoa

http://www.instructables.com/id/Controlling-an-Arduino-with-Cocoa/

Related Instructables

MacOS Foot Switchfrom a GuitarAmp Pedal. byaymans

Watts-your-consumption? -Wireless PowerMeter bymcloke74

Create anApplescript/ArduinoAlert flag. byaarone

Arduino LedDice byDavuzz11

AdvancedTruffle-Making:SlabbedGanache andMulti-LayerTruffles byMongpoovian

How to makeWindows VistaOr XP Look LikeMac Os XWithout PuttingYour ComputerAt Risk bylaxdude31

DIY AppleRemote Shieldfor the Arduinoby computergeek

Arduino XMAShitcounter byalex_weber

Advertisements

Comments14 comments Add Comment

 adamohern says:  Feb 8, 2010. 11:11 AM  REPLYThanks for the great info! Is it just as easy to get info back from the Arduino to display on the screen? I want to make a bicycle computer that displays all ofits info in Mac OS X... does that seem like something that would be relatively easy using this method?

 beak90 says:  Nov 9, 2009. 4:22 PM  REPLYI wrote up a wiki page about how to do this a while ago located here: http://www.arduino.cc/playground/Interfacing/CocoaIt looks like its been changed a little since I wrote it, but I wrote up the part on using Popen().

Also, you have to change the part in the code after the tty.usbserial to whatever port your Arduino is on. So you have to look in the Arduino program underTools/Serial Port then replace the "/dev/tty.usbserial-A6006hmi" thats in this code to the selected serial port in the Arduino program. Read my post for moreinfo about this.

 andrew101 says:  Nov 5, 2009. 6:44 PM  REPLYcool code, only thing is, when i use the serial monitor to send an i or o to the arduino it works, but the program wont...

i needed to change the com port to com4 because thats what my arduino uses, and when i press on or off the RX light goes on for a short time but it doesnttrigger...

any ideas?

 andrew101 says:  Nov 5, 2009. 6:46 PM  REPLYoh, nevermind, after a quick arduino reset it recognizes it. nice :)

 Radiosity says:  Oct 19, 2009. 6:42 PM  REPLY  Very nice! I'm going to try this next chance I get. Thanks!

Page 13: Controlling an Arduino With Cocoa

http://www.instructables.com/id/Controlling-an-Arduino-with-Cocoa/

 computergeek says:  Oct 19, 2009. 7:22 PM  REPLY Thanks. I'm glad you like it!

 Radiosity says:  Oct 21, 2009. 9:37 PM  REPLYI just tried this and it worked great, but I just have one suggestion:

In your code you used the serial port "/dev/tty.usbserialA6006hmi", but this is dependent on which computer you are using. In my case I had toreplace both instances of that with "/dev/tty.usbserialA4001KMJ" (which was the port I had selected in the Arduino IDE) in order for my application toproperly send data to my arduino. I suggest to add a step in which you find out which serial port you are using and add it into the code. (Just toprevent any confusion for somebody else reading this instructable)

Other than that great instructable and you have my vote!

 guschabal says:  Oct 19, 2009. 8:18 PM  REPLYThanks men! I write a similar program using WPF Application and your code for C#(Windows)!

Thanks a lot!!

 computergeek says:  Oct 19, 2009. 8:23 PM  REPLY Your welcome!

 jeff-o says:  Oct 15, 2009. 10:37 AM  REPLYVery neat.  How would you go about building a standaloneapplication that interfaces to the Arduino, but doesn't require theArduino IDE to be running?

Or is that the subject for a whole Instructable in itself?

 computergeek says:  Oct 15, 2009. 5:11 PM  REPLY I'm glad you like it! Are you using Mac OS X or Windows? If youhave Windows you don't need it open. But for some reason the Arduino IDEneeds to beopen in Mac OS X. It might be possible, but I have no clue.

 jeff-o says:  Oct 16, 2009. 5:23 AM  REPLYOS X.  Not that I'm much of a programmer - I just thought it would be nice to build matching hardware and software.  Could it be a driver issue?  Asin, a driver that is only loaded when the IDE is running?

 computergeek says:  Oct 16, 2009. 11:24 AM  REPLY It isn't a driver problem, but how the code works. If you run:"echo i > /dev/tty.usbserial-A6006hmi", "r"in terminal with the the arduino serial monitor open it will also work. All popen() does it run code in terminal. Here's an article on this topic . I doquite a bit of programming in cocoa, but not much with serial connections. So I'm fairly new to this.

 jeff-o says:  Oct 19, 2009. 5:51 AM  REPLYAh, well, maybe that will be fixed someday if enough people complain.  ;)