Mac Mountain Lion – Audio output changer

My first tutorial for the year! Only 12 days late…

For those who do not wish to read source is located here

So I got my hands on some really nice Bowers & Wilkins MM-1 speakers for my mac they have a built in DAC (Digital Analog Convertor) which means I can connect them up through a USB port instead of the line out (Which means my optical connection to my amp can stay in). Now here comes the problem if I want to change between the Amp or the speakers I have to open ‘System Preferences’ open the audio section open the output tab and select what I want my audio to go through.

Well thats just not gonna happen.

In my bragging to my mate Sam about my sexy speakers, he said that he has to do the same thing. I said well I’m about to write a script to do it for me. He asked if I could show him how.

So I siad yeah I will write up a tutorial. A simple Applescript application that sits on my dock that when you click it, it will toggle between the two audio outputs. There is no error handling in this version so if the USB DAC is not connected it will cause problems. Error handling will come later. My end goal is to have a physical button on my desktop which when pushed will switch it for me, with a screen displaying the current audio output. I think i might even put the current itunes song information as well maybe.

1.8 TFT screen

The above image is a example of the selected output. (On a side note how good is my pixel art!)

I have just smashed some code together it works well for me, note it is not the best way of doing it or the most correct. But it works and as this project gets more complicated I will rewrite the code nicely.

I will break the above code into managable chunks and explain what is going on. If you do want to follow on copy the above code into Applescript editor and have fun.

You will also have to have Enable access for assistive devices option on in the Accessibility section of system preferences for this Script to be able to function.

Please note that the Notification section was not developed by me. You will have to download the application from

http://www.cooperative-fruitiere.com/notifications/index_en.html

And then just place the application into your Applications folder. You do not have to open or run this Applescript just talks to it when it needs to. If you do open it you may need to kill the process with activity monitor if you get error 1000 from the Applescript editor

The Applescript code that we write use that application to display the Notifications in Mountain Lion.

  •  The above opens ‘System Preferences’ and selects the sound section.

The above section is where we actually change the audio output selection over.

  • Lines 1 – 4, Select the output tab of the audio page in system preferences
  • Line 5, Checks which row is already selected if is row 2 then line 6 changes it to row 1 or if row 1 is already selected line 9 changes it to row 2.
  • Line 7 & 10, set a variable ‘deviceselected’ to the name of the audio output so we can use it later. Not these are manually set and you will need to change the name to the name of your outputs.
  • The rest is just closing of the tell’s and if statements that we opened in the first few lines.

  •  Closes ‘System Preferences’ for us.

Now the rest of the code is for the Notifications, you do not have to use this section if you don’t want. I thought it was cool when it popped up to let me now that it worked and has changed over the output. As I said before you need the Notification scripting application. (This will only work on Mountain Lion!!!)

Ok so for the notifications

  • Lines 1 – 6, tell application ‘Notifications Scripting’ the one you downloaded to display a notification
  • Line 2, is not needed its there for my testing
  • Line 3, sets a dictionary telling it that we will be handling all replies that come back and which version we would like to use.
  • Line 4, is the actual notification.

Untitled

  • Lines 8 – 16, Just handle and replies that we get back, not I am not using any of them but they are still required to be there. If you would like to handle the user clicking on the notification etc. Your code would go in there.

Once all that is in the Applescript editor the easiest way to use it is to click ‘file’ -> ‘export’ and change the file format to Application save it some where and drag a copy on to your dock.

Untitled

 

And that is it all done for the basic version. As I said before there is no error handling in this version. I plan on working on the hardware version next with a little screen and physical buttons, more than likely based around an Arduino.

 

Please subscribe and let me know how well it worked for you!

 

 

Posted in AppleScript, Audio, Mac Tagged , , ,

Nixie Clock video

I have finally started working on my Nixie clock again.

It took a while as I lost a the voltage regulator for it, then got a replacement that was the wrong package. I order the correct part by the time it came I gave up. I got around to hooking up the 1st digit (seconds digit).

It now works, I found out that I had a bridged connection on the number 5 as it was always on. Fixed that and this is what it looks like.

I hopefully get around to making the rest of it tomorrow then I will design and make up a case for it early next year!

Posted in Clock, Lights pretty lights, Nixie

New Domain

Hello all,

I have finally moved over to the new domain

http://www.hughesy.net/wp

hopefully all my traffic will slowly move over as well. Please save my new address as a bookmark and subscribe

I plan to do more tutorial in the near future I have a project I recently completed which I will upload soon. It involves xBee’s ShiftBrites my phone and python.

I thought it was pretty cool anyways.

Posted in Uncategorized

Arduino xBee on Mac with AppleScript

Hey everyone,

I said in my last post yesterday that I was going to show you something more useful than an Arduino wirelessly counting (It was AWESOME I know).

So I came up with this little beauty of a project (Ok really small and easy half a project). CoolTerm the serial communication software that we used to listen/program the xBee has AppleScripting capabilities built in. Now my experience with AppleScript or lack there of does not matter as the guys that made CoolTerm documented it well, and gave us two examples. Now the project is a very simple Temp sensor based on the LM35 from Nation Semiconductor (Datasheet). It works when I send a single character through my xBee on my computer, the Arduino replies with the current temperature through the xBee and the AppleScript says it out loud and displays a dialog with the string as well.

Pretty easy and would be a great base for a datalogging application or something of the sort, such as the AppleScript could be modified to open a file and log the latest temp every 15 minutes or so on.

So I am going to explain my process in designing this little project as I find it is the easiest way to attack a small project.

I knew that I want to use the AppleScripting abilities of CoolTerm cause it is pretty cool and opens a lot of possibilities for other projects. After reading the API documentation and going through the example code that they provided I found it was pretty easy. I wrote down in english what I needed to do.

  1. Tell – Tell AppleScript what program I am talking to
  2. Load – Load a settings file that I saved
  3. Connect – Connect using the settings in the file I loaded
  4. Write – Write a single character to make the Arduino sample the temp and send it back to me
  5. Poll  - Load the information out of the buffer in to CoolTerm ready for use
  6. ReadAll – Read all the characters out of the buffer into a string
  7. Say – Get my mac to read out the string to me
  8. Display alert – Display the string in an alert box for when I can not hear it cause my music is PUMPING (It does that from time to time)
  9. Disconnect – Close the connection to the xBee
  10. Quit – Quit CoolTerm to make the window disappear
  11. End Tell – Tell AppleScript I am done

Now I added in some more code to display alerts when I made things happen so I could see if it was working. My AppleScript ended up looking like this (Comments added for your information)

#Open CoolTerm if not open
tell application “CoolTerm”
#Load settings from where I saved then before hand see pictures below
#Was open CoolTerm set the serial port I wanted to use in the options and saved
#The file to a folder in my documents, you will need to do this and point to the
#Correct place
if LoadSetting (“Macintosh HD:Users:ashleyhughesSmilie: :Documents:CoolTermSmilie: :xBeeConnection.stc”Smilie: ;) then
#Display an alert if the settings loaded correctly
display alert “Settings loaded.”
else
#Display an alert if the settings did not load
display alert “Failed at loading settings.”
end if
#Try and connect to the xBee using the settings loaded
if Connect “xBeeConnection.stc” then
#Display an alert if the connection opened
display alert “Connected to xBee”
else
#Display an alert if the connection failed
display alert “Failed at connecting to xBee”
end if
#Write a single character to the Arduino with the temp sensor I sent a ‘U’
Write {“xBeeConnection.stc”, “U”}
#Wait 1 second to allow for the full string to be sent for the Arduino
delay 1
#Get all the character out of the buffer ready for reading
Poll (“xBeeConnection.stc”Smilie: ;)
#Read all the characters in the buffer into dataIn
set dataIn to ReadAll {“xBeeConnection.stc”}
#Say dataIn using the computers built in Text to Speach
say dataIn
#Display an alert containing what the Arduino sent to us
display alert dataIn
#Disconnect from the xBee, always disconnect or you might have problems reconnecting later!
Disconnect “xBeeConnection.stc”
#Quit CoolTerm, I left this out to begin with and it cause the CoolTerm window to stay open EWW
quit
#Finish up
end tell

AppleScript Window with all the Code

Settings to save, make sure you point to this file in the AppleScript

Now for the Arduino, all you need to do is upload this code to your Arduino. Install a LM35 with +5V and GND as per the datasheet, pins change with packages. Then connect the Vout pint to analog pin 0 on your Arduino. Install your xBee shield on the top and power it all, mine is running off a USB port charger on a wall socket.

  1. Serial begin – Start serial communications @ 9600 baud
  2. Wait – Wait for a character to be received
  3. Sample temp – Sample temp and calculate to degrees
  4. Print – Print the temp out
  5. Clear buffer – I was going to use the Serial.flush() command but it doesn’t work the way I wanted so I read instead which clears out the buffer

Then run the AppleScrip and boom (Hopefully nothing actually goes boom), your computer should tell you the temperature. Now there will be a few alert boxes, I commented out most of them except for the error ones as I don’t need to know that the settings are loaded and it connected. I had them there to start with when I was first testing out my code.

If you get an alert saying that the settings wont load, make sure that your pointing to where you saved the settings file on your computer and use ‘:’ not ‘/’ for the address. If the connection won’t open check that the xBee plugged in with the power light on? Did you set the correct port when you saved the settings?

LM35 On the LCD Shield with a little white dot

Shield stack

Power the Shield stack

Posted in AppleScript, Arduino, Mac, Project, xBee Tagged , , , , , , ,

New easier xBee for Mac Lion (OS X 10.7) with Arduino

Well, I have finally got around to posting the new tutorial for xBee’s on a ALMIGHTY mac. This was tested on my mac running OS X 10.7 (Lion), with Arduino 1.0, Processing 1.5.1, and CoolTerm 1.4.1, and an Arduino duemilanove with a ATMEGA328P

So I have found an easier way to program up your xBee’s without having to use Windows (X-CTU) to set up the settings on the xBee modules, or use screen which is a terminal program for mac that has no GUI and has weird key combinations. It is a free program called CoolTerm. Now if you want to compare the two. CoolTerm like riding a bike, where screen is like riding a bike blindfolded down an EPIC hill with no brakes.

Now into the Tutorial.

You will need the following

  1. Mac computer
  2. CoolTerm installed (Click to download CoolTerm)
  3. Arduino Software installed
  4. An Arduino any type that you can connect a xBee to
  5. A xBee Shield to go with your Arduino
  6. 2 xBee’s modules
  7. xBee explorer usb interface
xBee Explorer

xBee Explorer

xBee Shield

xBee Shield

First thing we need to do is set up both the xBee modules so that they will talk to each other.

  1. Plug in a xBee module into the xBee explorer and hook it up to your mac via USB.
  2. Open up CoolTerm then hit the ‘Options’ button on the toolbar.
  3. Select the correct port for your xBee explorer will be something like ‘usbserial-XXXXXXXX’ (mine is usbserial-A600dJm8 each is different)
  4. If your device didn’t come up disconnect it, plug it back in and click ‘Re-Scan Serial Ports’.
  5. Hit the ‘Ok’ button as the rest of the setting set at default will work just fine.
  6. Hit the ‘Connect’ button on the toolbar and BOOM you’re talking to your xBee
  7. Ok now here is where we are going to change-up the settings on the xBee, if you pause for to long the xBee will exit configuartion mode, and you will have to do the next step again, so if it’s not doing what you want to try step ’8′ again
  8. Type ‘+++’ and Don’t press enter just wait and your xBee will reply with ‘OK’ that means its ready to take AT commands
  9. Now we need to change a few things to get them talking, After each command you should recieve ‘OK’ you will not see what you type.
  10. ATID (The PAN ID) lets change this to 2171 ‘ATID2171′ ENTER
  11. ATMY (The xBees address) lets change this to 1 ‘ATMY1′ ENTER, now when we set up the other xBee module this will have to be equal to this modules ATDL
  12. ATDL (The Destination) lets change this to 0 ‘ATDL0′ ENTER, now when we set up the other xBee module this will have to be equal to this modules ATMY
  13. The above to settings will have to be swapped for the next module so 0 talks to 1 and 1 talks to 0
  14. ATBD (The Baud rate) This is set to 3 by default which is 9600 and we will leave it at that.
  15. ATWR will write all the settings we just changed to the module ‘ATWR’ ENTER, if you omit this step when you unplug power to the module it will go back to the last stored settings
  16. Now we are finished for that module hit the ‘Disconnect’ button on the toolbar.
  17. Unplug the usb and change the xBee module over, you can place the xBee module we just finished with in the xBee shield as it is ready to go.
  18. Plug in the usb
  19. Program up this one just the same as the other module, remembering to change the ATMY to 0 and the ATDL to 1
Note the above settings can be changed to values of your choosing, I just liked those values at the time

That is both modules ready to rock and roll, you can leave the second module in the xBee explorer as we will be using it as it is.

Now that the modules are set up if you where to set up an Arduino sketch that has serial output in it, the serial output will go over the air to you xBee explorer module, and into your computer. Below is a basic sketch which will send an int value over the air to your computer and increment every half second. Also if the sketch wont upload check your xBee shield there is a little switch it needs to be set to DLine to upload the code and set to UART to send the serial text to the xBee

To view this happening, using CoolTerm connect to your xBee explorer as you did is steps 3 through 6. The output in CoolTerm should be counting up. If you have any problems add a comment down the bottom and I will see if I can help. It works for me now the code is a bit flaud as if you leave it going for long enough it will do some funny stuff as the int will run out of space when it passes 32676 it will start from -32768.

CoolTerm after the Arduino Sketch was running for a while

That is all I am going to do for now, I have a project set up which will do more. It will also be more useful than counting wirelessly, but that will come tomorrow.

 

Have fun and post and questions in the comments sections and I will see if I can help you out.

 

‘HughesY’

Posted in Arduino, Mac, xBee

My xBee tutorial for Mac

I have had a few people ask me to complete my tutorial for xBee’s on mac for a while now.

I haven’t been using them that much as none of my projects need it at the moment but I said I will get around to it so I will have a new version of the tutorial finished with in a month.

Lots of pictures, maybe a video.

The plan is for me to come up with a project. Show how to set up 2 xBee’s and code up a project that will run from my computer to a Arduino in my room somewhere. Don’t know what the Arduino is going to be doing yet hopefully I will come up with something more that just a LCD.

If anyone has any ideas let me know

Posted in Arduino, Project, xBee

My new MSGEQ7

Inspired by Sparkfun’s Bar Graph Breakout Kit

I finally found a reason to play with 2 MSGEQ7′s ICs. Ever since seeing them I have been thinking what could I use them in. Being away from home for work and having large amounts of time and nothing to do. I have designed up a board that uses 8 LED’s hooked up to a 74HC595. I originally was going to have all 7 sets of 8 LED’s on a single board but went away from that idea as is to over populated and not very reusable in other projects.

The MSGEQ7 is a 7 band graphic equalizer which breaks an audio single up to 7 bands and peck detects them and outputs an analog signal of each.

The plan is to read each signal into one of my Arduino’s then output each band onto its own 74HC595 board which will be linked serially (Each board as an output connector as well) allowing them to be daisy chained together requiring less pins on the Arduino. I am hoping for an awesome looking old LED EQ.

I have only drawn up and order the 74HC595 boards at the moment I will hook up and test the MSGEQ7 when I get back home.

I ran all the routes my self not the best work I am sure but it was the first time doing it and the auto router was possessed and messed it up to many times. Learning how to run routes from top to bottom was hard until I worked out you do not have to do anything but change layers.

This is my second board that is getting fabricated properly. I am sending it off to be fabricated on the 5th of Decemeber as pcb.laen.org is closed till then, BTW He does an awesome job very cheap as well. I got a quote from a Australian company for the same board $300 as they don’t do small boards. All the boards he does are purple (AWESOME) most come gold-plated the above-board with postage will be about $25 with postage.

Posted in Arduino, Audio, Lights pretty lights

LM3915 + LM324 Audio VU Meter

So I got these LM3915′s AGES ago, pretty much when I started playing around with electronics and found out it was to hard and left them alone (In the mean time broke a pin some how???).

Bored this week with some time off work and nothing better to do, I started finishing the work on my LM317 power supply got it working then found out I have nothing to hook up to it to play with.

LM317 out of its case my 1st PCB from a fab house only forgot to run one air wire opps

Now after much chat on the forums at EEVBlog.com and http://electronics.stackexchange.com/ I had a rough diagram to go off the basic version

Basic

The above worked all right using the audio jack out of the back of my computer, but I wanted to able to adjust the input.

…WHY… oh why

Then it gets complicated. I decided to go down the op-amp path to amplify and control the input signal. I end up with my current version of the circuit (Any errors I am sorry the diagram came after the bread board design was working). Main reason for a op-amp over another design is I haven’t used op-amp’s and I wanted to play with one.

It works not adjustment YET

Now I have a working circuit I ran into 2 major problems before the above design happened, the

Problem 1: The display always had 4 bars even when there was no signal.

This took me a while to work out then I remembered from the According-to-Pete video I watched in the morning @ http://www.youtube.com/watch?v=OMJ9WGrRf6A&feature=youtube_gdata I believe my problem was because I didn’t use a dual rail power supply I connected the V- of the op-amp to 0V therefore the input was to close to the input voltage of the op-amp so I halved it using a voltage divider R5 & R6. Which fixed my problem. I don’t know if I had the correct diagnoses but it works.

Problem 2: It moves soooooo slowly.

This was quickly fixed as Kiriakos-GR @ EEVBlog forums suggested that C1 was blocking the mid to high frequencies causing it to only see low range with doesn’t change as much or something along that line, which was close it was C2 so I changed it from 100uF to 2.2uF and BOOM Baby.

 

I have a working VU Meter bouncing along to my music. I still have to add a pot to adjust the input level before R1 and then I have to hook up another one for STEREO and also set up the current through each led at the moment each only gets about 2uA on my meter. Dave @EEVBlog’s last video explains the formula for that

 

AND then move to higher powered LED’s for awesome lighting effects. Soon to be placed under the lip behind my computer spread so the higher range is on the out sides so more of my desk lights up with louder music

 

 

Sound on video doesn’t come through well on have my phone to record on. And yes it is up side down…

Posted in Audio, Lights pretty lights Tagged , , ,

LCD awesomeness Pt.1

I have done a little bit of work with AppleScript side of the project. Took awhile to get my head around coding in AppleScript and alot of googling.

I ended up with a smallish script using “SerialPort X” (Allows AppleScript to talk to serial devices).

This is the not so useful script that allowed me to test out my idea. It’s in three main parts “Run” “Idle” “Quit”. Also for this script to work properly it needs to be saved as a Application that is a Stay open type.

global portRef

on run

set portRef to serialport open “/dev/cu.usbserial-A800crBV” bps rate 9600 data bits 8 parity 0 stop bits 1 handshake 0

if portRef is equal to -1 then

display dialog ” could not open port ”

end if

end run

The above pretty much sets up a serial port object, opens it up with the correct data rates, etc. Note that you will need to change the ”/dev/cu.usbserial-A800crBV” part to the correct address of your device that happens to be the address of my FTDI board. The easiest way I find to get the correct address would be to open a Terminal window and type in ls /dev/tty.* and it will display the address. Then we check if the port was opened. If it didn’t, a dialog opens up saying so.

Now here is the main problem I ran into: Because of the way that I have set the code up it needs to run as a stay open application or the on quit section (see below) won’t run. The problem that causes is that the serial connection to your device doesn’t get closed, so in turn you can’t reconnect to it until you pull it out and put it back in. If your’e just messing around with the script, IE running it in the AppleScript editor, add serialport close portRef somewhere as that will close the connection and save you alot of swearing.

on quit

display dialog “Closing link” buttons {“Ok”} default button 1

serialport write “Closing link” to portRef

serialport close portRef

continue quit

end quit

So the above block of code is pretty easy. On the script being quit (it’s really an application not a script but it’s an AppleScript script so script/application, I will end up calling it both) it will tell you that it’s closing the link and then display it on the Arduino, close the link and finish quiting.

on idle

tell application “Mail”

tell account “MobileMe”

tell mailbox “INBOX”

set countNumber to unread count

serialport write “Unread emails ” to portRef

serialport write countNumber to portRef

end tell

end tell

end tell

return 15

end idle

Ok the main block of code, short version is it gets the unread message count off the mail application and writes it to my Arduino.

…Long version is it accesses mail then the mailbox called inbox (funnily enough thats where my mail goes) then gets the unread mail count and stores it in countNumber. I then display a nice message: “Unread emails” and the number, then I get out of the mailbox and mail and return 15 which means run this code in 15 seconds.

Now this won’t run for a while after you start the program as the first time idle get’s run is about 30 seconds and it will run every 15 seconds after that. Heaps of time for me send my self mail to watch the count go up and down.

I have an arduino connected up with an LCD and the SerialDisplay code out of the LiquidCrystal library running

Hughesy 16×2 LCD wiring

That is my test bed for the project so I can test getting information across from AppleScript

Posted in Arduino, Mac, Project Tagged , , , ,

So its been awhile My new project LCD awesomeness for MAC

So its been a while

I’m going to get straight into a new project over the next few days/weeks. It will invovle displaying information off my mac onto a 20×4 LCD screen, using an Arduino. To start with it will be done over a USB serial connection but it will be migrated to a xBee link (No wires are always better).

The basic information it will display …. for now

  • Unread email count off Mail.app for all my email accounts
  • Last unread email subject line
  • Current itunes song (Of course!)
  • Unread Adium chat count and screen names of unread chats
  • Temp of a LM35
  • RSS when i work out where to steal the info from
To start with my mac will provide all the information and my Arduino will just display it by scrolling the information across the screen. It will be sent over serial connection to Arduino
, the information will be a basic string.
To start with I think I’m going to do it in applescript, but that isn’t final yet just seams the easiest way to do it.
Hardware I’m going to use at the moment
  • Arduino (On breadboard a Mini pro with FTDI, finial will be a ATMEGA328 chip)
  • 20×4 LCD HD44780 Black on White
  • LM35 Temp sensor
  • Um wire (It’s everywhere.)
  • Few resistors multiple values
  • 10k Potentiometer
and I’m sure other bits maybe an RTC (Real Time Clock), some sensors to weather (sensor off another Arduino out side? Temp and pressure etc.).

Posted in Arduino, Mac, Project Tagged , , , ,