Created
November 12, 2008 16:59
-
-
Save atduskgreg/24207 to your computer and use it in GitHub Desktop.
RAD [Ruby Arduino] servo thermometer demo from RubyConf 2008
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ServoDemo < ArduinoSketch | |
external_vars :sensor_position => "int, 0", :servo_amount => "int, 0" | |
output_pin 15, :as => :myTemp, :device => :onewire | |
output_pin 14, :as => :my_servo, :device => :servo | |
serial_begin | |
@hi_byte = "0, int" | |
@lo_byte = "0, int" | |
@device_crc = int | |
@t_reading = int | |
@sign_bit = int | |
@tc_100 = int | |
def loop | |
servo_refresh | |
start_temp_conversion | |
toggle 13 | |
delay 750 | |
toggle 13 | |
get_temp_data | |
print_temperature | |
adjust_dial | |
end | |
def get_temp_data | |
until myTemp.reset do # reset bus, verify its clear and high | |
delay 500 | |
end | |
myTemp.skip # listen up! | |
myTemp.write 0xBE, 1 # send me your data conversions | |
@lo_byte = myTemp.read # get irst byte | |
@hi_byte = myTemp.read # get second byte | |
7.times { @device_crc = myTemp.read } # get next 6 bytes, drop them on floor | |
# next byte the ninth byte is the CRC | |
# DS18B20 brings data temperature back as 12 bits | |
# in degrees centigrade with 4 bits fractional, that is | |
# each bit s 1/16 of a degreeC | |
@t_reading = build_int @hi_byte, @lo_byte | |
@sign_bit = bit_and @t_reading, 0x8000 | |
@t_reading = twos_comp @t_reading if @sign_bit # negative | |
@tc_100 = (6 * @t_reading) + (@t_reading / 4) #multiply by (100 * 0.0625) or 6.25 | |
end | |
def start_temp_conversion | |
until myTemp.reset do # reset bus, verify its clear and high | |
delay 500 | |
end | |
myTemp.skip # "listen up - everybody!" | |
myTemp.write 0x44, 1 # temperature sensors, strta conversion | |
end | |
def adjust_dial | |
temp_dec = ((@tc_100 / 100) * 10 ) + ((@tc_100 % 100)/10) | |
temp_position = (temp_dec - 200) * (255.0 / 300) | |
serial_print "servo: " | |
serial_println temp_position | |
my_servo.position temp_position | |
end | |
def print_temperature | |
serial_print (@tc_100 / 100) # separate off the whole | |
serial_print "." | |
serial_println ((@tc_100 % 100)/10) # and fractional portions | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment