This document discusses using Arduino with Ruby. It provides an overview of Arduino hardware and software, introduces RAD (Ruby Arduino Development) for writing Ruby code to control Arduino boards, and presents several example projects that combine Arduino and Ruby including Ruby on Bells, Barduino, and a Flying Robot. Code examples are provided for blinking an LED, fading an LED, reading an analog sensor, and using servos from Ruby. Additional sensors and shields discussed include Sharp IR sensors, ultrasonic rangefinders, and WiFi shields.
22. Blink LED
// Blinking LED
#define LED 13 // LED connected to
// Digital pin 13
void setup ()
{
pinMode(LED, OUTPUT); // sets the digital
// pin as output
}
void loop()
{
digitalWrite(LED, HIGH); // turns the LED on
delay(1000); // waits for a second
digitalWrite(LED, LOW); // turns the LED off
delay(1000); // waits for a second
}
23. Blink LED
class BlinkLed < ArduinoSketch
output_pin 13, :as => :led
def loop
blink led, 1000
end
end
24. Fade LED in and out
int ledPin = 9; // LED connected to digital pin 9
void setup() {
// nothing happens in setup
}
void loop() {
// fade in from min to max in increments of 5 points:
for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=5) {
// sets the value (range from 0 to 255):
analogWrite(ledPin, fadeValue);
// wait for 30 milliseconds to see the dimming effect
delay(30);
}
// fade out from max to min in increments of 5 points:
for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=5) {
// sets the value (range from 0 to 255):
analogWrite(ledPin, fadeValue);
// wait for 30 milliseconds to see the dimming effect
delay(30);
}
}
25. Fade LED in and out
class Analogled < ArduinoSketch
output_pin 9, :as => :led
@i = 1
def loop
while @i < 255 do
analogWrite led, @i
@i +=5
delay 30
end
while @i > 0 do
analogWrite led, @i
@i -=5
delay 30
end
end
end
26. Blink LED analogue input
int sensorPin = 0; // select the input pin for the sensor
int ledPin = 13; // select the pin for the LED
int sensorValue = 0; // variable to store the value coming from
// the sensor
void setup() {
// declare the ledPin as an OUTPUT:
pinMode(ledPin, OUTPUT);
}
void loop() {
// read the value from the sensor:
sensorValue = analogRead(sensorPin);
// turn the ledPin on
digitalWrite(ledPin, HIGH);
// stop the program for <sensorValue> milliseconds:
delay(sensorValue);
// turn the ledPin off:
digitalWrite(ledPin, LOW);
// stop the program for for <sensorValue> milliseconds:
delay(sensorValue);
}
27. Blink LED analogue input
class Analoginput < ArduinoSketch
output_pin 13, :as => :led
@val = 0
def loop
@val = analogRead(0)
digitalWrite (led, 1)
delay @val
digitalWrite (led, 0)
delay @val
end
end
30. Serial Read
class Serialcom1 < ArduinoSketch
input_pin 0, :as => :sensor
serial_begin :rate => 19200
def loop
serial_println analogRead sensor
delay 1000
end
end
31. Plugins and Libraries
• C Methods, directives,
external variables and
assignments and calls
that maybe added to
main setup method
• Arduino libraries
32. Servo code
class Servo < ArduinoSketch
output_pin 11, :as => :servo, :device => :servo
def loop
servo_refresh
servo.position 90
end
end
33. class Servo < ArduinoSketch
output_pin 11, :as => :my_servo, :device => :servo
def loop
servo_refresh
my_servo.position 180
servo_delay 1000
my_servo.position 0
servo_delay 1000
end
def servo_delay(t)
t.times do
delay 1
servo_refresh
end
end
end
55. class Srf05 < ArduinoPlugin
# Triggers a pulse and returns distance in cm.
long pingsrf05(int ultraSoundpin) {
unsigned long ultrasoundDuration;
// switch pin to output
pinMode(ultraSoundpin, OUTPUT);
// send a low, wait 2 microseconds, send a high then wait 10us
digitalWrite(ultraSoundpin, LOW);
delayMicroseconds(2);
digitalWrite(ultraSoundpin, HIGH);
delayMicroseconds(10);
digitalWrite(ultraSoundpin, LOW);
// switch pin to input
pinMode(ultraSoundpin, INPUT);
// wait for a pulse to come in as high
ultrasoundDuration = pulseIn(ultraSoundpin, HIGH);
return(ultrasoundDuration/58);
}
end
Most Common and easiest to use 32 kb flash memory
7-12v LED 13
Many clones offer same or extra functionality
Sold 10,000
Previous version of the current USB Arduino board
16kb flash memory
The big one
shield compatible 128kb flash memory 4 hardware uarts
Different versionsLilypad 8Mhzsewn onto clothes stylish puple
Lilypad example
EthernetStackable
Motor
I/O BoardEasy to add servos and sensorsAdd Bluetooth as Serial port
Processing also need to install usb driver suitable for your platform
Main Gem outdatedMadrona fork GithubRuby 1.91 readyBrian Lyles Fork can use 18
Uses Ruby2c
Basic Installaton on Mac OS X Instructions of Github for other flavours RAD creates directory stucture
lots of examples in the examples directory
Hardware setup
Software set up
Write coderake make:upload
Co-founder of Arduino
Excellent BookEasy start
Well explained Examples
Processing code
Blink LED at rate dependent on sensor value
Blinks LED at rate set by sensor
Plugins preferred methodLibraries give access to quite a few devices
servo refresh called roughly every 50ms, won&#x2019;t call less than 20 to keep position
library
device:servo tells us to work with servo library written by Brian Riley
moving between 2 points, note servo_delay
flying robot then adapted to work with blimpduino
Ruby conf 2008
Problems shoesSnow Leopard Actively being worked on this was where I was going to show my robot linked to shoes however we can briefly look at flying robot code
XBees but could use any serial interface
Start up in shoes
lib serial code
background
compass
3 look left right and centre
analogue non linear, algorithm Tom Igoe&#x2019;s book Making things talk
Two modes control of ping 4 wires or 3
4th wire tied to ground
plugin, digital pin
plugin pass pin in and returns distance
Wishield Wifi
wishield only arrived from United States the other day so still need to write plugin. Arduino working for a few hours sending light values and temperature
ModularStackable shieldsPing noise run in circlelooks for clear path
No affiliation, working on adding speech and getting bluetooth working with shoes