Summarized using AI

Arduino the Ruby Way

Austin Vance • November 01, 2012 • Denver, Colorado • Talk

In his talk "Arduino the Ruby Way" at RubyConf 2012, Austin Vance explored the realm of physical computing using Arduino and Ruby. He presented Arduino as a user-friendly tool for software engineers looking to create physical interactions without delving deep into C/C++ programming. Vance shared several key points regarding the capabilities and integration of Ruby with Arduino, making physical computing more accessible:

  • Introduction to Arduino:

    • Describes Arduino as a single processor microcontroller that simplifies interactions with the physical world.
    • Various Arduino models were introduced, showcasing their versatility from tiny form factors to wearable tech.
  • Open Source Nature:

    • Emphasized that Arduino hardware and software are open-source, allowing for community collaboration and innovation.
  • Physical Computing Examples:

    • Discussed projects such as automated home systems and custom devices, including a scoreboard for foosball, showcasing the exciting applications of Arduino in real life.
    • He demonstrated how programming Arduino enables hands-on interaction with physical devices, thus bridging the gap between software and hardware.
  • Using Ruby with Arduino:

    • Discussed the challenges faced when programming in C/C++ for Arduino and the benefits of using Ruby through a gem called ‘rad’, which allows for a more straightforward programming experience.
    • Addressed limitations of rad and proposed a new approach where Ruby code communicates with Arduino as a service rather than compiling directly for the microcontroller, thus enhancing usability.
  • Interaction Between Programming Languages:

    • Introduced Node.js libraries for Arduino, stating they function effectively due to their asynchronous nature compared to Ruby.
    • Vance shared how he created a Ruby gem combining functionalities he appreciated in existing libraries to allow seamless communication with Arduino.
  • Challenges and Solutions in Developing the Ruby Gem:

    • Vance discussed technical hurdles, such as creating an event-driven architecture for the Ruby library while maintaining efficiency and avoiding dependencies.
    • He highlighted the design of key components within the library, detailing how users can easily interact with their Arduino devices through a simplified interface.

In conclusion, Vance's presentation demonstrates the evolution of programming physical devices using high-level languages like Ruby, encouraging developers to explore the creative potentials of Arduino. He also invites contributions to his Ruby gem, fostering an open-source community spirit to further enhance collaboration in the field of physical computing.

Arduino the Ruby Way
Austin Vance • Denver, Colorado • Talk

Date: November 01, 2012
Published: March 19, 2013
Announced: unknown

What's the deal
Writing software is great, websites, applications, and scripts are things we interact with every day. What about writing software that we can interact with in the physical world? From automated sprinkler systems that turn off when you pull into your drive way, to scoreboards at the company foosball table physical computing opens a door for the innovator inside every software engineer.

It's too hard
Not true, there are amazing libraries and compilers that let us write software for Arduinos in the best language, Ruby. Think about it no C, no headers, and no static types, just plain old ruby. That is a good deal.

In this talk I will go over a few of the most popular Ruby libraries for interacting with an Arduino, talk about some basics of each library and then spend time on how these libraries and Arduino can interact to create applications that respond in the physical world. There will be t-shirts shot from a robotic cannon.

RubyConf 2012

00:00:15.679 I'm Austin and uh I really like working with my hands so I've been playing with
00:00:21.480 the Arduino over the past couple months and have really really enjoyed it and I finally got a talk together and uh this
00:00:29.160 is it I've never given a conference talk before I've actually never been to a conference so I I hope you
00:00:35.960 guys um I hope you guys like it I I hope the
00:00:41.320 conference or I hope the talk is a big bang and uh um works out well I work for
00:00:48.600 pivotal uh I like thanking them they they're paying for me to be here and got
00:00:53.800 me a hotel and everything it's really nice and if you ever want to find me I'm on GitHub and Twitter
00:01:00.039 at Austin BV so first what is an Arduino so I assume a lot of people have heard of
00:01:06.280 them but they don't really know what they are they've ever actually touched one or seen one or held one so I have
00:01:11.479 one up here um screwed into the cannon so you can come look at it after the talk um but what they are is they're a
00:01:18.439 single process microcontroller meaning they're a small computer but they only do one thing and
00:01:24.680 they do that one thing over and over and over again and they take the shape and form or they take a lot of shape and
00:01:29.759 form form so that that first one is the mini it's about the size of a thumb then we have the lily pad which is an Arduino
00:01:35.479 that's designed to be sewn into clothing it's waterproof um can be washed can go and scuba diving do whatever you want
00:01:42.439 you have the Digis spark which is a Kickstarter which is about the size of a quarter and could plug right into uh
00:01:48.240 plug right into USB Arduino is all open source the community is is really really
00:01:54.079 an open source community and the the hardware on the the Arduino is open source the firmware is open source the
00:01:59.520 soft that you use to program with your Arduino is open source and they allow for a lot of different variations but
00:02:05.000 the main Arduino you'll play with and the one that's on this Canon is the Uno the Uno is probably the biggest but it's
00:02:11.160 also the best for prototyping so the Uno comes with everything you need to get started with an Arduino first you have
00:02:17.000 your USB you can plug right into a plug right into a computer and put your put your software right onto the Arduino
00:02:23.280 without thinking about how to connect them you have power so your Arduino can stand alone you have the microcontroller
00:02:29.000 which is the processor the computer that that makes the decisions and runs your program you have analog pins and digital
00:02:35.160 pins the analog and digital pins are your way of interfacing with the world so what can you do with an Arduino
00:02:41.680 you really can do just about everything with an Arduino people have done done tons of things I built a cannon
00:02:48.680 um why not people build Rovers skateboards scary
00:02:55.879 toilets displays and Halloween costumes all control by a single a single
00:03:02.080 microcontroller they let you start working with your hands seeing physical seeing physic physical things changed
00:03:08.319 from the software you write which is really cool because I've written a lot of software I do that every day but it I'm I'm Tethered to a screen and Arduino
00:03:15.400 is like taken away my screen and now I can just interact with whatever I want um so I started playing with them
00:03:20.840 actually to to do some home automation stuff and um and just kind of like Get get like
00:03:27.799 temperature controls or thermometers to work turn on and off lights other other people in my office have done done
00:03:33.040 similar things like opening and closing windows or turning on and off fans so you can really start like writing code that that like makes your life better
00:03:39.959 not just your workday better um how do you do this well you use the Arduino IDE which is also open source the Ard
00:03:46.480 Arduino IDE comes with two main features it's really a basic IDE but mainly it has a a verify and compile Arduino comes
00:03:54.599 with a lot of header files that let it give you a almost a DSL for C++ to write Ard
00:04:00.480 um to write code for your Arduino so it has a verify and compile um button and
00:04:06.400 that will include all those headers and test your code for you or make sure it can compile and it has a console so you
00:04:12.360 can make sure everything on your Arduino is working the way you expect it to be working what this does then is after you
00:04:18.400 compile your U after you compile your code and you burn it onto your uino it's permanently there the second you start
00:04:23.520 providing power to this little chip I'm not going to point the cannon down because it's loaded but the second uh
00:04:29.039 the second you provide Power to your Arduino the the program on it starts running and it's permanently there you
00:04:34.240 can keep replacing that program but it's permanently there until you replace it with something new so you have this one-way communication your Arduino ends
00:04:40.520 up being Standalone it's isolated and it's small it's really cool because you can start putting them anywhere you want you can bury them in your walls put them
00:04:46.759 in your yard to control sprinklers do whatever you want with them but you have to write C++ I don't
00:04:53.000 like writing C+ plus I hate thinking about memory allocation I love slow boot times like all those things are really
00:05:06.320 writing Ruby code for your Arduino and it provides you almost the exact same functionality that you would have with the Arduino IDE but in Ruby so what does
00:05:14.080 a rad script look like looks like this looks really really simple so this is Ruby you say you set a pin 13 to Output
00:05:21.039 you name it as an LED and then rad gives you a little blink function so what happens then is rad takes this sketch
00:05:27.479 which is what you call something you put onto your Arduino and compiles it into C and it ends up looking like this um it's
00:05:34.919 not pretty there's statically typed variables and semicolons and and you know something you don't really want you
00:05:40.560 don't want to play with I can't dream and see I can like start thinking in Ruby and that's like one of the things I
00:05:45.680 like about that software or think like about Ruby but rad has a couple problems one it hasn't been pushed to in about
00:05:51.919 two years which means no support for one um Ruby 1 n no support for two you have
00:05:57.120 to downgrade a lot of the gems that have been updated because um that have been updated since then because they're not
00:06:02.199 compatible anymore and then debugging was difficult for me um I don't have a lot of experience with C besides
00:06:08.120 college and so I had to think about how my Ruby code was becoming C and failing
00:06:13.400 comp compilation in C and then come back to Ruby and figure out how to change that or change my Ruby code and so there
00:06:18.840 was this like extra uh barrier impeding me and so it was really tough so why not
00:06:24.199 think about it in a different way why not think about our Arduino as a service so we'll just talk to the
00:06:30.120 Arduino we'll put all our high higher level logic everything that we want our program to do on a computer and we'll
00:06:35.680 let our Arduino do what it's good at we'll let it start interfacing with Hardware interfacing with the physical
00:06:40.880 world reading sensors and that that's actually was like a pretty cool idea so I started looking at how to do this now
00:06:46.880 there's some problems you have those three benefits of how you burn to an Arduino before what happens when you
00:06:52.199 burn Cod on to an Arduino you're Standalone you're isolated and you're small well now all of a sudden we're Tethered to something so we're no longer
00:06:58.080 Standalone and we're no long isolated and if I'm Tu if I'm plugged into my laptop I'm not really small anymore
00:07:04.840 which now now I just may as well have a computer but the Arduino does give you one benefit over a computer you can plug
00:07:10.440 sensors into it it actually has ways to plug wires into it and start talking to proximity sensors or buttons or anything
00:07:16.599 like that that you wouldn't do with your laptop so let's try and get back to Standalone isolated and small well first
00:07:22.560 if we want to use a computer there's the Raspberry Pi the Raspberry Pi is a small computer that can run Linux Debian um or
00:07:29.319 actually actually I think people have gotten a lot more running on it but plug an Arduino into a Raspberry Pi and all of a sudden you're Standalone again this
00:07:35.800 thing is isolated the power comes from the the Raspberry Pi and the Raspberry Pi is small enough to tuck into your
00:07:41.479 wall or bury in your yard so you're now small and Standalone again which is pretty pretty neat in the case of
00:07:47.159 isolation I don't really care like being plugged into a computer provides so much benefit for me I don't need to worry
00:07:53.560 about being isolated from that and if I can get them both small and to and self-contained then that's that's good
00:07:59.560 good enough um everything seems to be tethered in this world already so why not why does my Arduino have to be
00:08:04.840 completely disconnected so I started playing with this and I found that nodejs has really
00:08:10.199 figured it out there's a lot of node.js libraries out there to start interfacing with your
00:08:15.599 Arduino um why did node.js figure this out while Ruby is seems way behind well
00:08:21.759 I think because mainly Arduino is asynchronous and no JS is asynchronous so it's really easy to move between the
00:08:27.599 two and start working with one with working for one with the other so that provides a lot of benefit Ruby is not
00:08:34.159 asynchronous so there are a couple node.js libraries or npm packages if you want to start playing with node and your
00:08:40.000 Arduino that are um pretty good Johnny 5 is really good it provides a lot of utility but uh it's mainly Geared for
00:08:46.920 robotics so unless you're doing robotics it there's a lot of extra stuff that you have to worry about that's probably not
00:08:52.240 as important then there's duino duino is the one I I really enjoyed and it actually helped me learn a lot about my
00:08:58.800 Arduino and get get going with it and they say exactly what I was thinking like why not just let the Arduino do the
00:09:04.320 lowl stuff while the higher level stuff can be done in JavaScript that seems pretty good so now
00:09:09.519 what what Dino did is took all the comple pro complex problems moved them up to a computer and just put a small
00:09:16.720 C++ library on the Arduino that you never have to read like as a programmer I don't want to read it I don't want to
00:09:22.120 think about it it's a service it's like talking to Twitter it should just do things for me I don't want to think about the Arduino so they really did do
00:09:28.399 that they turned the this node library has turned the has turned the Arduino
00:09:34.399 into a service so now we have message passaging between the two and it works really
00:09:40.480 really well it you you can start prototyping things almost instantly in a language that you love but there's
00:09:45.800 nothing in Ruby so what are we going to do without without our favorite language the
00:09:51.760 language that we're all here to AC at a conference for the the language that we we work in every day
00:09:58.079 now I wrot a gem so I implemented what I
00:10:03.360 thought dinino would do in Ruby and I had a couple goals for the gem the first
00:10:10.120 goal was I wanted to do it the pivotal way which is to fully test um fully test drive and make sure that everything
00:10:16.240 everything is um consumed by automated tests or or uh covered by automated
00:10:22.360 tests that was really really difficult for me because I don't understand a lot of the things that are happening between
00:10:27.760 between my computer and the uino and the cord and the the low-level stuff so it actually was pretty interesting to get
00:10:33.200 into and I will say the testing is is lacking still um there's not a lot of integration tests integration level
00:10:38.920 tests the Arduino can test itself but there's not a lot of that going on so it would be really nice if this gem could
00:10:45.920 move there the other goals is I wanted this library to be really really easy to extend I wanted people there's so many
00:10:52.079 there's so many pieces of Hardware out there there's stepper motors from tons of different companies sensors from tons
00:10:57.880 of different companies all of them have a slightly different way of reading hard or reading the physical world I wanted
00:11:03.279 you to be able to write your own library or write your own classes that build upon like this this base structure that
00:11:09.959 lets you interface with your Arduino I wanted it to be documented um there's a lot of Hardware stuff out
00:11:16.600 there and it's just at like the electrical engineering level of I can't
00:11:21.680 understand it's just it's too difficult for me and I just like I blank out and I get frustrated and then I normally just go outside so
00:11:29.519 um I wanted people to to look at look at this gem look at the documentation and
00:11:34.760 be able to plug their Arduino into a computer and have a light blinking in five minutes um and and what really really is
00:11:42.000 good about that I think is you you don't hit this brick wall and all of a sudden you just don't want to do this anymore
00:11:47.160 because you you're constantly getting you're constantly getting a feeling of moving forwards so you never you're never just stuck and the documentation
00:11:54.440 really helped the zero startup time was also a big part of this I didn't want you to have to like grap your grap your
00:12:00.959 um user Dev to find your Arduino and like plug in the file and deal with IO
00:12:06.440 objects like that's all stuff no one really wants to deal with no one wants to think about how that stuff works how streaming Works how all that stuff works
00:12:13.279 I wanted you to plug in Arduino and hit go and that should that should be it and
00:12:18.360 lastly it better be fun that's like the whole point of this right like we we program because we enjoy it and so I
00:12:23.959 want it to be fun to to play with I want you to really enjoy building cannons and shooting potatoes at garages
00:12:31.680 um so I began I began development by myself um and I decided I didn't want to
00:12:38.720 write anyc uh so I stole from I stole from that Ecto doino the
00:12:45.399 Ecto Ecto Library doino the C that he wrote and I just put it into my library
00:12:51.279 so I want to thank him for writing a great a great base for us um for you know this ruby
00:12:57.040 gem and uh because of that I was able to get get bootstrapped in Ruby really quickly but I had a couple of problems
00:13:03.519 that I faced first I'm synchronous Ruby is stuck synchronous the Arduino is
00:13:09.279 asynchronous and what I mean by the Arduino being asynchronous is you don't ask an Arduino what state a button is in
00:13:16.279 or what temperature it is outside the Arduino is constantly telling you that the button was just pressed and so you
00:13:22.839 have to be listening for the Arduino to tell you something and in Ruby the easiest way to do that is to just block
00:13:28.600 so you just end up saying like oh I'll just wait until a button is pressed down and then I'll do something but if you want to do this in a website or a build
00:13:34.839 script or something like that all of a sudden you're blocking you know incoming requests and that's that's no fun so the
00:13:41.920 first thing I thought was well why not just copy like line for line the JavaScript um library that I like into
00:13:48.040 the event machine so that worked pretty well it started to like work and I was getting
00:13:53.720 lights blinking and but buttons buttons pressing and and everything was pretty neat but it didn't really looked
00:14:00.199 good it looked like JavaScript written in Ruby um which was a big bummer right
00:14:06.959 like we want we want our Ruby to be readable we don't want it to be nested we want it to feel like feel like Ruby
00:14:12.040 look at this we have um neing up a board then when the board is as soon as the board is ready because when the board
00:14:17.279 news itself up which is the Arduino um when the board news itself up it has to like do all this communication and
00:14:22.759 handshaking and then when that board's finally ready we can send it a message and we
00:14:32.000 yeah we'll leave that off for now um so we we can send it a message and now we
00:14:37.560 need to wait for that message to like say that it was okay and ouro to say like okay we received the message and
00:14:43.040 finally we can start a timer and that timer will run in a link a loop and we'll blink a light so that's a lot of work just to Blink a light like compared
00:14:49.519 to what rad had going on like rad was pretty pretty simple for blinking light compared to this so I felt like it was kind of a
00:14:56.120 loss so I moved to celluloid um there's been a couple talks about Celluloid I
00:15:01.199 think there's another one coming up so I won't like give you the the real scoop on it but it's it was pretty fun working
00:15:06.480 with uh really it just provided like an actor pattern and a way to like push these things off to the side and not
00:15:11.560 think about them and it gave me a lot of utility to to like pull up dying threads or if
00:15:17.680 an exception occurs that was good but there was also a lot of overhead and as much as I like people doing work for me
00:15:24.519 like Celluloid uh I didn't need all the extra overhead so finally I decided to
00:15:29.680 roll my own event Loop so this actually gave me a a huge win which was that my G my gem has zero
00:15:36.800 dependencies so when you gem install this you get nothing else which I thought was awesome um you don't have to
00:15:42.560 worry about making sure you're updated with Celluloid or the DSL doesn't match and the other thing that's really good
00:15:47.639 is is it provides a lot of control for exception handling um the way Celluloid handles exceptions is not always ideal
00:15:53.920 for physical like physical Computing or Hardware hacking maybe if my Arduino accepts if I
00:15:59.920 can't receive a message I want my website to die like I want something real to happen I don't want that to just like keep trying to spin it back up and
00:16:06.120 it's harder to do that stuff it's harder to do that stuff with the uh with Celluloid and it's rolled
00:16:13.759 specifically for the gem so there's a really there's a really nice DSL around the event Loop and tacking things into it but the same rules apply as with
00:16:20.639 Celluloid or with a vent machine you never can block it so um it was nice to
00:16:26.839 kind of get get that wi out get that WI went out the door and like feel like I I actually had control over my own Library
00:16:32.240 I didn't have to read other people's documentation so then how did it end up how did it end up um I think it ended up
00:16:39.240 really well I was really surprised uh I've never written a real gem that I
00:16:44.880 wanted to open source before from from scratch and I didn't know how this would turn out I mean there's a lot of really
00:16:51.079 good choices out there but it turned out really really well so first I'll just kind of talk
00:16:56.360 about what it does and how you guys can get started with it because hopefully like after this talk you'll call up spark fun which is a great place to buy
00:17:02.680 all this stuff call up spark fun or adaa fruit and you're like hey man send me an Arduino right away I want it like
00:17:08.199 tomorrow and then you just start working with this stuff because it's really fun and uh actually spark fun is right in
00:17:14.319 Boulder so it's like a 45 minute drive if you want to get up there tomorrow you probably could make it
00:17:20.280 um so the board the CL the board class is the phys or the programmatic
00:17:26.240 representation of the Arduino so what does that mean it is the Arduino
00:17:33.679 it has pins it has IO it knows how to talk to things it knows how to read
00:17:39.360 things it knows how to send it knows how to to look at messages that are coming into it it is your Arduino just in code
00:17:46.679 and so what you can actually read through it and it like the the class structure ended up looking like an Arduino almost like you have like things
00:17:53.320 that you can set pins up on and you have your microcontroller and your your heartbeats and your loops and all that
00:17:58.640 stuff stuff that that make like what your Arduino is like it it's physical piece and so that's really cool so it
00:18:05.240 really is like the heart of the gem it provides all of the utility it is
00:18:11.679 the core but there's one thing that it doesn't do it doesn't communicate and
00:18:17.039 that was a really conscious decision um instead we moved communication into its own class which is the txrx txrx is a
00:18:24.440 domain term if you've never done Hardware hacking you probably have never heard it or if you've never done
00:18:29.799 um but what it means is essentially like sending and receiving of messages or um
00:18:35.440 input and output so txrx if the Arduino if the board class is the heart then txrx is
00:18:43.480 probably the mouth it it's how you talk to the computer or it's how the computer
00:18:49.640 talks to the Arduino and so what that did separating that out gave us some
00:18:55.280 really really cool um some really really cool features we didn't see coming but the first thing that txrx does that's
00:19:01.960 really important is it finds your Arduino for you so it goes out groping your system and looking at all your USB
00:19:07.440 ports and finds your Arduino and sets all that stuff up and tells the board that when it's finally ready the other
00:19:13.960 thing that we do with it is we inject it so I've become a big fan of dependency injection and we can talk about that
00:19:19.960 whenever you want but not not while I'm on stage but I uh I really like it and
00:19:26.200 by injecting this I'm in no way coupled to my my communication method so if someone wanted to they could write a
00:19:32.520 txrx class for Wi-Fi so you don't actually have to be plugged in so right now if the Canon is is plugged into a
00:19:39.520 USB port on my computer you could rewrite this txrx and as long as you adhere to the same public interface then
00:19:46.080 you can use Wi-Fi or Bluetooth or infrared lights blinking at each other like whatever you want it would be really cool to do something like just
00:19:53.000 crazy like that and we wanted those possibilities to be open so then eventually you're not Tethered to your computer anymore and Standalone again um
00:20:00.840 which would be really really neat so why not I mean I'll I'll accept poll
00:20:06.080 requests by the way this is an open source gem I want people to contribute um just like the the Arduino Community
00:20:12.360 is extremely open source friendly um if you if you submit polar requests I'll
00:20:18.159 I'll take them and then I'll just give you push access to the gym um so as long as you're a contribut as long as you want to contribute you can always
00:20:25.320 contribute so now all this stuff those two classes that I talked about are really low level and you might never even have to open them up they deal with
00:20:31.880 IO objects and and non-blocking and have event Loops buried into the depths of them and they're they're like really
00:20:38.080 like the guts of what makes this happen but if you don't want to think about that then don't panic because there's a
00:20:45.080 bunch of other stuff that are part there's a bunch of other things that are part of this gem that really like will
00:20:50.240 help you um start working with your Arduino and not have to think about the core or
00:20:55.760 the base or the lowlevel stuff and that's all in modu components so module components is where we got that where we
00:21:01.720 got that benefit of easy to expend easy to extend so everything in the module components extends base component base
00:21:08.280 component does a whole bunch of little bits or does a little bit of work for you setting up pins making sure the board is there um knowing if it's an
00:21:15.200 input or an output device or whatever you really need so if you write your own just
00:21:21.919 inherit from that and you'll get a lot of free stuff done for you so let's talk about a couple of the classes that that
00:21:27.600 you can use tonight or tomorrow when you go get your Arduino from spark fun um
00:21:33.320 you can use you can start working with an LED the LED is your hello world for
00:21:39.480 physical Computing like turn blinking an LED is it like that now you you've finally gotten like something happening
00:21:45.520 and that's the easiest thing to do so that was the first class we wrote so what does an LED class look like in the
00:21:52.400 gem like if you were to open it up and read the source code that's it um because everything is hidden so
00:22:00.120 far into the board and all that lowle stuff is pulled away from you this is all there is to it so you set your PIN
00:22:07.559 you inherit from so of course this is an inheriting from base component so we call Supra on our
00:22:13.400 initializer and then we just make sure we're talking about an output device and then we turn the LED off so that's
00:22:19.120 setting the PIN to low then we just have two methods on and off and we can either turn the pin on or
00:22:26.200 turn the PIN to a high or a low and in in in like the physical world if you were to plug a multimeter into these
00:22:31.279 pins what that means is you're actually letting electricity flow through um a
00:22:36.520 wire or not so it's actually like that it goes down to like that far or low and so that that's been really cool for me
00:22:43.520 learning like how computers really communicate is and and building up on top of that so this is what the class
00:22:50.080 looks like if you were to write another LED class you would get something similar I think but what's it look like
00:22:55.400 when you use it looks like this so it's smaller than the rad smaller
00:23:01.960 than rad and all you really do is you new up your board and your LED so you you KN up
00:23:07.200 your board and you inject your communication class into it in my case it's the txrx or the the serial port or
00:23:13.000 a USB cable and then I knew up an LED and I tell the LED what pin I'm on and I
00:23:19.400 give it the board the reason we're injecting the board and we've talked about this a lot but the reason we're injecting the board is Aaron Patterson
00:23:25.559 just gave a talk about making salami and he was using a not Arduino um
00:23:32.000 chipset um and you could actually rewrite the board to communicate with something that's not in Arduino and
00:23:37.760 continue to use the components and so that was one of our goals too is so everything is not dependent upon what
00:23:43.440 what stands above it um so as you get more and more High Lev the lowl things
00:23:48.679 can be can be interchanged as long as you adhere to a public interface and then all you do is you
00:23:54.200 turn the LED on and off and it reads really well it all Lays against the left line of of your of your text editor um
00:24:01.720 it's not nested it doesn't look like JavaScript with DOs and ends I think this ended up really really cool I was like stoked when I saw when I saw this
00:24:08.480 finally working um and and it looked so clean um but it didn't solve or it
00:24:14.840 solves one problem and that's talking to the board so talking to the board is is
00:24:19.960 really easy because that's not blocking IO sending messages is really easy because you don't care if those messages are ever received what you really care
00:24:26.799 about is when the board tells you something when a button is pressed when a laser line is broken um when something
00:24:32.240 bends you Flex sensor bends or something like that what you do is you want you want to be told that that just happened
00:24:37.880 and that's where it gets difficult in Ruby and is really really easy in JavaScript so the first place we tackled
00:24:43.440 this was with a a digital input device a button so if you look at class
00:24:50.960 button what does it look like button class again inherits from
00:24:57.399 the component base but instead of setting itself up on a pin it actually adds itself as Hardware to the board so
00:25:04.559 it tells the board hey I'm here as a piece of Hardware you should be looking at me and the other thing that it does
00:25:12.240 is it tells the board what pin it lives on and then it tells the board to start reading on that pin so the C++ the C++
00:25:18.440 library on the board can dynamically figure out what pins it needs to be reading on based on what Hardware is
00:25:24.880 added to the board so that was something really cool is that you're not actually set setting up your pins ever if you look at the C++ you're not actually
00:25:30.760 setting up your pins in like a setup loop on the Arduino if you if you guys have ever used one they're dynamically
00:25:36.919 changing um as classes die or are born and the other thing you do so then then
00:25:42.640 you have this update function and the board calls update on whatever Hardware it's looking at whenever a state
00:25:50.799 changes um so in the case of an analog or digital Hardware the the state change
00:25:55.880 is really the only important part you know you're not constantly reading um information you're just you only care
00:26:01.360 when the button goes down you don't care what you know what what's happening in between down and up so it calls that with a little bit of
00:26:08.960 information the data in this case is a one or a zero um it could be changed to something more meaningful like a symbol
00:26:15.640 and probably should be and then we call the button up and button down which are callbacks so those
00:26:22.640 callbacks end up finally getting passed in to your class and you'll see those in a minute but first want to talk about
00:26:29.200 documentation so this is where things kind of get weird and you have a little bit more than an LED going on where LEDs
00:26:34.960 you kind of just drop in and they work or even on in the case of the Uno there's an LED built into it so you can
00:26:40.720 play with like it blinking lights without ever buying an LED or plugging wires into anything um so we wanted to
00:26:48.240 make it easy for not only you to get started with this gem but get started with your Arduino so we created fritzing
00:26:55.200 documents for all of the utility classes so in the case of a button it's really simple in the case of a stepper motor
00:27:00.799 it's not um but they're they're all there and the goal is that you know when you want to start working with your
00:27:07.120 Arduino you don't even you don't have to go Google how to plug all this stuff in and figure out what resistors you're supposed to be using or transistors or
00:27:14.000 capacitors like all of that stuff just exists in a in image file and a how-to with an example and a running script
00:27:20.960 that will make like in in the case of a button when you press the button will console log something for you or in the
00:27:26.760 case of a stpp motor will spin a steper in either direction which you'll see in a minute so the goal is that all of this
00:27:32.200 stuff is really well documented there's images to help help people get started if you've never done this before and and
00:27:38.840 example classes so if you your codee's not working but the example class is or the example file is working then you
00:27:44.919 probably are doing it you need to figure out a better way or need to yeah
00:27:50.120 practice I don't I don't know so what does a button look like when you actually use it you pass procs
00:27:57.600 into buttons lambdas um functions into into a button as callbacks now you're starting to feel
00:28:04.120 a little bit more like like uh an asynchronous language now because because now we have functions that we've
00:28:10.120 passed in and we call later they're not called right away and they're not they're P we're passing functions around
00:28:15.320 rather than just calling functions on things but I couldn't think of a better way to do this and if someone thinks of
00:28:20.440 one please feel free um but what you do is you pass callbacks into button down
00:28:25.559 and up and you can pass as as many as you want in or remove them later if you want or whatever you want to do and when the
00:28:31.840 button goes down it Loops through all those call backs and calls them asynchronously and then in the same same
00:28:37.200 in the same case for up so we make sure we we keep all those callbacks in an asynchronous world so you're never
00:28:42.240 blocking your your main thread your Loop or your website or whatever is happening that that's really that's really like
00:28:48.279 the core of your application um and it ends up being pretty small like to get a button
00:28:54.440 working that's I was pretty happy like that's four lines of code and it all you're already blinking in LED so we
00:29:00.600 have the LED on and off and passing in and stuff so that's that's digital
00:29:06.000 input what about analog input now a lot of decisions can't be made for you like State change so that's actually a little
00:29:12.320 bit more difficult so we have a sensor class the sensor class is another one of those core classes it's really uh um I
00:29:19.039 mean it it works you could use it for any sort of analog um input device it works really well but it it doesn't have
00:29:24.840 like the nice like onof that an LED has or the button up and down like those methods that mean something to someone
00:29:30.559 instead we have to deal in in abstracts because we don't know what sensor you're actually working with so what I hope
00:29:36.279 people do is they take this sensor class and in the case of um my Halloween party
00:29:41.600 I had little sensors around my yard and as people walked by they would whisper to like Arduino would whisper like
00:29:48.399 creepy things at you um and uh I extended I extended sensor
00:29:56.159 for the proximity sensor so a proximity sensor all I cared about was if someone was within a certain range not the range
00:30:02.559 changed so then I could say like within and give it a distance and the the sensor class knows now it has now
00:30:08.480 there's meaning there's a DSL to working with a sensor on my Arduino and I thought that was really cool so that's
00:30:13.720 that's kind of how you would do it so what does the sensor look like inside the sensor inside looks pretty simple it
00:30:20.640 has the same update function as a button but you're adding analog Hardware which tells the board that it needs to know
00:30:25.840 about everything um that's that's one thing that that's the one thing that different that's different you need to know about
00:30:31.720 everything not just State changes and we start read so it's again very very simple these classes are really small I
00:30:37.519 don't think we have a class over like 25 lines um in the end like that's been a really big win besides the board which
00:30:44.080 is fairly large um but that has all the event Loop buried inside of it as
00:30:49.960 well so when you when you use a sensor what's it look like you just pass in when data received
00:30:58.080 you pass a proc into that or Lambda or function of any kind and it'll call those asynchronously when data is
00:31:03.320 received and it passes data into the function that you're passing into it so as you see the proc takes one parameter
00:31:08.600 data and it will call it will call that proc with the data that the sensor is reading so in the case of a proximity
00:31:14.919 the proximity sensors that I was using it gives you numbers between like one and a thousand and those numbers between
00:31:21.000 one and a thous a thousand have like a logarithmic curve that mean distance so then I had to have some math about how
00:31:26.360 to get the distance to mean like feet to me but um it was always called with the
00:31:32.600 data that was getting receiv or with the data that the sensor is is seeing so that raw data and then I have to translate it in my class to something
00:31:39.480 meaningful um finally uh another pivot wrote the stepper um class which I use
00:31:47.120 on the Canon I I'm really bummed actually it's right here is a little stepper motor um I had this this thing
00:31:53.480 like full pitch control from a website so you could aim it at yourself um um and then uh in an attempt when it got
00:32:01.919 off balance this the motor was was wasn't powerful enough to pull the cannon back up
00:32:07.880 so um the way stepper Motors work is it's for electromagnets and it pulses them to get a wheel spinning and so
00:32:13.799 actually you can provide more power to a stapper motor and it gets more powerful um unlike a DC motor which like that
00:32:19.919 can't happen but I provided way too much and I fried everything actually created a little fire it was neat in my
00:32:26.600 hotel so um that's also a fun part about physical
00:32:32.200 Hardware is you end up with like cuts and bruises and burns um which is fun
00:32:37.279 for me because yeah it's interesting you actually can hold something um it gets hot and not not just like my laptop but
00:32:45.720 but be careful please um so what does the stepper motor look like it's two methods so step
00:32:53.440 clockwise and step step counterclockwise
00:32:58.559 so all it does is it pulses my pins the pulses the pins on the on the motor just
00:33:03.960 like you should to get the wheel spinning but it's two methods that's what uh six eight 10 lines of code to
00:33:11.840 get a stepper motor working so that was that was pretty cool and if you go find a a different stepper motor it's going
00:33:17.559 to be about the same amount of work for you to get it working so that's what I I think like was really NE is as as you go
00:33:23.240 play with different types of Hardware it should be easy for you to implement them in dyo and then start working with them
00:33:29.159 so then to to spin a a step promotor we'll just spin it a full Revolution clockwise and counterclockwise so that's
00:33:35.080 all we that's all this this script does is it just like turns it around and then the other action so that's that's some of the
00:33:42.159 stuff that we wrote It's a a quick overview there's a ton more that we're writing have written I say we a bunch of
00:33:47.799 my friends have been helping um make sure this is well tested and working and you know I've just had so many people
00:33:53.440 that have have enjoyed it I think everyone in my office has has at least given some sort of input which has been
00:33:58.840 really really cool but we have a lot more that you can play with too as you start playing with your your arduinos
00:34:04.840 you can start working with infrared um I have uh been working on a
00:34:10.520 foosball table app so a website that take keeps score for our foosball table that goes like with leaderboards so you
00:34:16.359 can register who you are and then we know who the best foosball player in the office is um and I use infrared infrared
00:34:23.359 light in the goal and the ball breaks the beam and that's how I know that a goal was scored so we have an infrared red sensor um that's a stepper motor um
00:34:31.800 we have RGB LEDs so you can change the color you have a a multicolor LED and we
00:34:37.320 have servos so servos are like are a motor that is just not close to as powerful as a stepper um but a lot more
00:34:44.560 precise as well um and there's there's even more than that it's like hard to go
00:34:50.679 into how much like has been in the pipeline and not pushed or been been worked on and and now has made it up and
00:34:58.200 everything seems to be very well tested and really well supported so I hope that like it's really easy for you guys to
00:35:03.400 get going so what does this code really look like when you use it because I've been using it right so this is this this
00:35:09.880 is the build script for my for the for the gem Dyno so it runs the build script
00:35:16.640 if the build is green I turn on a green LED otherwise I turn on a red LED so I actually like have a little light on my
00:35:22.720 desk that turns red or green based on whether or not um I broke the build um
00:35:27.760 for the gem which I think is pretty cool um super nerdy too
00:35:34.960 but what about in rails so rails is rails is weird or I guess just the web
00:35:40.800 is weird right because we have these longlived classes we have buttons and LEDs and boards and and uh communicators
00:35:47.119 txrx that live a lot longer than most of your models ever dream of living right if they're living as long as the board
00:35:53.040 is living then there's probably a problem with your web server um because they should be born when the request comes in and die as the request goes out
00:35:59.680 that's not the case of a board the board you want to be the board you want to be able to monitor the world around it even
00:36:04.920 when requests aren't coming in and out because you might maybe you want to to to use Aaron Patterson's live streaming
00:36:10.359 or Pusher or whatever to send information to people on your website about the current temperature around you
00:36:15.520 or he could use it for his his salami stuff so I ended up I ended up putting
00:36:21.400 everything in an in in an initializer in rails anything that was long lived lives in an initializer maybe there's a better
00:36:27.319 place I don't know um if there is or you have opinions on it I'm always welcome to
00:36:32.400 them um just find me but I've been a fan of Sinatra so this is the code for the
00:36:38.640 Canon that's it um so to run the Canon all I do is I
00:36:45.440 have one endpoint that fires the Canon and the Canon is digital is digital um
00:36:50.760 output so it looks like or it feels like an LED it may as well be an LED actually when I was building the the stuff for it
00:36:56.760 I was using an LED instead of a cannon because it's way more dangerous to have a cannon firing on accident
00:37:04.119 um but this is it so um why not look at it and and first of all thank you very
00:37:12.520 much so
00:37:20.359 let's oh it's over need a browser
00:37:27.640 so this is the website for the Canon
00:37:34.720 um yeah there's a t-shirt in it um disclaimer I'm
00:37:42.079 sorry um the compressor could be turned on for
00:37:48.280 number
00:38:02.960 on yeah safety there's there's a emergency shut off valve
00:38:44.800 all the way
00:38:53.760 in so anybody who wants can go to uh curry. looc c r i
00:39:02.040 E.L on your computer and uh you can actually click fire just don't do it
00:39:07.200 while he's in front of the Canon
00:39:14.040 um anybody get if you can get there I don't know if the conference Wi-Fi is actually
00:39:22.960 working Curry c r i E.L and then it's Port 4567
00:39:55.720 out whoa whoa all right you're safe I turned the valve
00:40:03.359 off the safety's on who's trying to kill my
00:40:16.400 friend what no I don't I hope not at least I
00:40:22.359 made the website during the last talk so so now someone yeah you
00:40:34.640 free last
00:40:42.359 one yeah oranges sandwiches potatoes whatever will
00:40:49.280 fit beers breakfast uh go to the
00:41:12.040 right so so thank you the gem is at Austin
Explore all talks recorded at RubyConf 2012
+46