Class ArduinoSketch
In: lib/rad/arduino_sketch.rb
Parent: Object

ArduinoSketch is the main access point for working with RAD. Sub-classes of ArduinoSketch have access to a wide array of convenient class methods (documented below) for doing the most common kinds of setup needed when programming the Arduino such as configuring input and output pins and establishing serial connections. Here is the canonical ‘hello world’ example of blinking a single LED in RAD:

  class HelloWorld < ArduinoSketch
    output_pin 13, :as => led

    def loop
      blink 13, 500
    end
  end

As you can see from this example, your ArduinoSketch sub-class can be dividied into two-parts: class methods for doing configuration and a loop method which will be run repeatedly at the Arduino‘s clock rate. Documentation for the various available class methods is below. The ArduinoSketch base class is designed to work with a series of rake tasks to automatically translate your loop method into C++ for compilation by the Arduino toolchain (see files/lib/rad/tasks/build_and_make_rake.html for details). See rad.rubyforge.org/examples for lots more examples of usage.

Arduino built-in methods

Thanks to this translation process you can take advantage of the complete Arduino software API (full docs here: www.arduino.cc/en/Reference/HomePage). What follows is the core of a RAD-Arduino dictionary for translating between RAD methods and the Arduino functionality they invoke, N.B. many Arduino methods have been left out (including the libraries for Time, Math, and Random Numbers, as the translation between them and their RAD counterparts should be relatively straightforward after perusing the examples here). For further details on each method, visit their Arduino documenation.

Digital I/O

digital_write(pin, value)

  Arduino method: digitalWrite(pin, value)

  Description: "Ouputs either HIGH or LOW at a specified pin."

  Documentation: http://www.arduino.cc/en/Reference/DigitalWrite

digital_read(pin)

  Arduino method: digitalRead(pin)

  Description: "Reads the value from a specified pin, it will be either HIGH or LOW."

  Documentation: http://www.arduino.cc/en/Reference/DigitalRead

Analog I/O

analog_read(pin)

  Arduino method: analogRead(pin)

  Description: "Reads the value from the specified analog pin. The Arduino board contains a 6 channel
    (8 channels on the Mini), 10-bit analog to digital converter. This means that it will map input
    voltages between 0 and 5 volts into integer values between 0 and 1023. This yields a resolution
    between readings of: 5 volts / 1024 units or, .0049 volts (4.9 mV) per unit. It takes about 100
    us (0.0001 s) to read an analog input, so the maximum reading rate is about 10,000 times a second."

  Documentation: http://www.arduino.cc/en/Reference/AnalogRead

analog_write(pin, value)

  Arduino method: analogWrite(pin, value)

  Description: "Writes an analog value (PWM wave) to a pin. On newer Arduino boards (including the Mini
    and BT) with the ATmega168 chip, this function works on pins 3, 5, 6, 9, 10, and 11. Older USB and
    serial Arduino boards with an ATmega8 only support analogWrite() on pins 9, 10, and 11. Can be used
    to light a LED at varying brightnesses or drive a motor at various speeds. After a call to analogWrite,
    the pin will generate a steady wave until the next call to analogWrite (or a call to digitalRead or
    digitalWrite on the same pin)."

  Documentation: http://www.arduino.cc/en/Reference/AnalogWrite

Serial Communication

serial_available()

  Arduino method: Serial.available()

  Description: "Get the number of bytes (characters) available for reading over the serial port.
    Returns the number of bytes available to read in the serial buffer, or 0 if none are
    available. If any data has come in, Serial.available() will be greater than 0. The serial buffer
    can hold up to 128 bytes."

  Documentation: http://www.arduino.cc/en/Serial/Available

serial_read()

  Arduino method: Serial.read()

  Description: "Reads incoming serial data and returns the first byte of incoming serial data
    available (or -1 if no data is available)"

  Documentation: http://www.arduino.cc/en/Serial/Read

serial_print(data)

  Arduino method: Serial.print(data)

  Description: "Prints data to the serial port."

  Documentation: http://www.arduino.cc/en/Serial/Print

serial_println(data)

  Arduino method: Serial.println(data)

  Description: "Prints a data to the serial port, followed by a carriage return character
    (ASCII 13, or '\r') and a newline character (ASCII 10, or '\n'). This command takes the
    same forms as Serial.print():"

  Documentation: http://www.arduino.cc/en/Serial/Println

serial_flush()

  Arduino method: Serial.flush()

  Description: "Flushes the buffer of incoming serial data. That is, any call to Serial.read()
    or Serial.available() will return only data received after the most recent call
    to Serial.flush()."

  Documentation: http://www.arduino.cc/en/Serial/Flush

Methods

Public Instance methods

Write inline assembler code. ‘Name’ is a symbol representing the name of the function to be defined in the assembly code; ‘signature’ is the function signature for the function being defined; and ‘code’ is the assembly code itself (both of these last two arguments are strings). See an example here: rad.rubyforge.org/examples/assembler_test.html

Confiugre a single pin for input and setup a method to refer to that pin, i.e.:

  input_pin 3, :as => :button

would configure pin 3 as an input and let you refer to it from the then on by calling the `button` method in your loop like so:

  def loop
    digital_write led if digital_read button
  end

Like ArduinoSketch#input_pin but configure more than one input pin simultaneously. Takes an array of pin numbers.

Confiugre a single pin for output and setup a method to refer to that pin, i.e.:

  output_pin 7, :as => :led

would configure pin 7 as an output and let you refer to it from the then on by calling the `led` method in your loop like so:

  def loop
    digital_write led, ON
  end

Like ArduinoSketch#output_pin but configure more than one output pin simultaneously. Takes an array of pin numbers.

Configure Arduino for serial communication. Optionally, set the baud rate:

  serial_begin :rate => 2400

default is 9600. See www.arduino.cc/en/Serial/Begin for more details.

Treat a pair of digital I/O pins as a serial line. See: www.arduino.cc/en/Tutorial/SoftwareSerial

Setup variables outside of the loop. Does some magic based on type of arguments. Subject to renaming. Use with caution.

[Validate]