Drivers

Artoo has a extensible system for connecting to hardware devices.

Analog Sensor


Makes it possible to interact with analog inputs on your device and obtain values that represent the analog input send by the sensors. The value received from an analog input read from the sensor goes from 0 to 1027.

How To Connect

For Arduino:

require 'artoo'
connection :arduino, :adaptor => :firmata, :port => '127.0.0.1:8023'
device :sensor, driver: :analog_sensor, pin: 0, interval: 0.25, upper: 900, lower: 200

For Digispark:

require 'artoo'
connection :digispark, :adaptor => :littlewire, :vendor => 0x1781, :product => 0x0c9f
device :sensor, driver: :analog_sensor, pin: 0, interval: 0.25, upper: 900, lower: 200

How To Use

Example using a photoresistor sensor to turn a LED on and off.

require 'artoo'

connection :arduino, :adaptor => :firmata, :port => '127.0.0.1:8023'

# Analog inputs are based from 0 to 5 on the Arduino UNO, may vary on other arduino boards
device :sensor, driver: :analog_sensor, pin: 0, interval: 0.25, upper: 900, lower: 200
device :led, :driver => :led, :pin => 8

work do
  puts
  puts "Reading sensor in analog pin #{ sensor.pin }"
  puts "Reading intervals every => #{ sensor.interval }"
  puts "Initial sensor value => #{ sensor.analog_read(0) }"
  puts "Sensor upper trigger set at value => #{ sensor.upper }"
  puts "Sensor lower trigger set at value => #{ sensor.lower }"

  on sensor, :upper => proc {
    puts "UPPER LIMIT REACHED!"
    led.off
  }

  on sensor, :lower => proc {
    puts "LOWER SENSOR LIMIT REACHED!"
    led.on
  }
end

How to use events and call them on your drivers

Pretty simple, when the connection and the driver have been setup (as explained above) you can call events on them as follows:

on sensor, :upper => proc { puts "UPPER LIMIT REACHED!" }
on sensor, :lower => proc { puts "LOWER LIMIT REACHED!" }

Commands

pin

Returns the pin number set up when creating the sensor.

Params

no params

Returns

integer

upper

Returns the upper limit set up when creating the sensor.

Params

no params

Returns

integer

lower

Returns the lower limit set up when creating the sensor.

Params

no params

Returns

integer

analog_read(pin_number)

Returns an integer value that represents the analog read from the sensor, the value goes from 0 to 1027.

Params
  • pin_number - params, the pin value
Returns

integer (from 0 to 1027)

Events

The limits that trigger the events are setup when creating the sensor (see above in the section titled "How to stablish a connection and driver" ), there are :upper and :lower limits, as well as an :interval param that defines how often the sensor should be read; when the specified amount of time passes the sensor is read, if one of the limits stablished is reached the corresponging event will be triggered.

start_driver

Starts the driver.

:upper

Gets triggered if the value read by the sensor is greater or equal to :upper (value passed when setting up the sensor).

:lower

Gets triggered if the value read by the sensor is less or equal to :lower (value passed when setting up the sensor).

Circuit

Compatibility