I've been working on my first Mac app for a few weeks and I'm launching it today. It's super-niche and solves a really specific problem: Drawing pictures to an 8x8 LED matrix breakout board from an Arduino. Let's say you wanted to draw a smiley face. In code, that looks like this:
Doing this by hand is obviously a pain. With Matrix Pen you just click to draw, like this:
Matrix Pen is $5. You can read more about it here and buy a copy here.
This isn't as complicated as some tutorials make it seem. You don't need a resistor and each push button just needs one wire to an input pin and one wire to ground. Wire up your button to your Uno like this:
In the setup() function of your code set the input pin to INPUT_PULLUP:
voidsetup(){pinMode(8,INPUT_PULLUP);}
To determine if your button is being pushed you do a standard digitalRead on the pin. The only tricky part with this is that you aren't looking for a HIGH signal like you might assume – your button will give off LOW when it's pressed. You'll also want to add a delay statement to limit the amount of reads per second you perform on the button. This keeps your code from executing over and over when your button is held down. Here's the code:
voidloop(){if(digitalRead(8)==LOW){//The button is pressed!
}delay(100);}
Multiple Buttons
It's easy to expand out to more buttons – just make sure they share a common ground. Here's an example with two buttons:
voidsetup(){pinMode(12,INPUT_PULLUP);pinMode(11,INPUT_PULLUP);}voidloop(){if(digitalRead(12)==LOW){//Button one is pressed!
}if(digitalRead(11)==LOW){//Button two is pressed!
}delay(100);}
Bonus Pins
If you're out of digital pins on your Uno, the analog input pins can be used as bonus digital pins – great if you've already used all your digital pins up through something with a lot of wiring like a seven-segment display.
This blog post from Crisp is the only example of a JSON Pretty Print BBEdit text filter I could find and unfortunately I was only able to get it to work about 25% of the time – the other 75% I would get this error message from BBEdit:
Invalid parameters were detected for an operation (MacOS Error code: -50)
There's not a lot going on in the script so I banged my head against it until I figured out how to fix it: Aggressively insisting on UTF-8 string encoding. Here's the modified script:
Call me old-fashioned if you want, but when I write code I want to do it in a Cocoa window, like a dignified human being. So I was pretty disappointed to find out that the primary IDE for the Spark Core is a text box on a web site:
Obviously that's not gonna do. I dug a little deeper and it turns out that there is a Spark desktop app called Spark Dev – but it's a fork of Atom, Github's web-based text editor. No thanks.
There's a light at the end of this JavaScript tunnel: Spark has an excellent command line app – Spark CLI. It can handle everything the web IDE does including compiling code and flashing your Spark over wifi. With that component in place, it's just a matter of writing some Applescript to get BBEdit pounding out Spark code. Here's my (mostly ripped off from the web) Compile for Spark script:
settheFiletomissingvaluetellapplication"BBEdit"setactiveWindowtofrontwindowif(classofactiveWindowisprojectwindow)thensetprojectDocumenttoprojectdocumentofactiveWindowif((countofitemsofprojectDocument)>0)thensetfirstFileItemtoitem1ofprojectDocumentasaliaselsesetfirstFileItemtofileofdocumentofactiveWindowasaliasendifif(ondiskofprojectDocument)thensettheProjectFiletofileofprojectDocumentasaliastellapplication"Finder"settheProjectDirtocontaineroftheProjectFilesetfirstFileDirtocontaineroffirstFileItemendtellif(firstFileDiris equaltotheProjectDir)then-- Use project filesettheFiletotheProjectDirasaliaselse-- External project file -> use first item to set contextsettheFiletofirstFileItemendifelse-- BBEdit doesn't provide direct access to the Instaproject root-- Use the first node from the project listsettheFiletofirstFileItemendifendifendtelltellapplication"Terminal"if(thefirstwindowwhosebusyisfalse)existsthensetwintothefirstwindowwhosebusyisfalsesetfrontmostofwintotruedoscript"spark compile "&thequoted formofthePOSIXpathoftheFileelsedoscript"spark compile "&thequoted formofthePOSIXpathoftheFileendifactivateendtell
And here's my Flash to Spark script:
settheFiletomissingvaluetellapplication"BBEdit"setactiveWindowtofrontwindowif(classofactiveWindowisprojectwindow)thensetprojectDocumenttoprojectdocumentofactiveWindowif((countofitemsofprojectDocument)>0)thensetfirstFileItemtoitem1ofprojectDocumentasaliaselsesetfirstFileItemtofileofdocumentofactiveWindowasaliasendifif(ondiskofprojectDocument)thensettheProjectFiletofileofprojectDocumentasaliastellapplication"Finder"settheProjectDirtocontaineroftheProjectFilesetfirstFileDirtocontaineroffirstFileItemendtellif(firstFileDiris equaltotheProjectDir)then-- Use project filesettheFiletotheProjectDirasaliaselse-- External project file -> use first item to set contextsettheFiletofirstFileItemendifelse-- BBEdit doesn't provide direct access to the Instaproject root-- Use the first node from the project listsettheFiletofirstFileItemendifendifendtelltellapplication"Terminal"if(thefirstwindowwhosebusyisfalse)existsthensetwintothefirstwindowwhosebusyisfalsesetfrontmostofwintotruedoscript"spark flash THE_NAME_OF_YOUR_CORE "&thequoted formofthePOSIXpathoftheFileelsedoscript"spark flash THE_NAME_OF_YOUR_CORE "&thequoted formofthePOSIXpathoftheFileendifactivateendtell