Created
August 23, 2013 20:20
-
-
Save christiangenco/6323580 to your computer and use it in GitHub Desktop.
Arduino and ruby code for The Vibrating Belt. More information can be found at: http://christian.gen.co/2013/04/08/the-vibrating-belt.html
This file contains 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
#simplest ruby program to read from arduino serial, | |
#using the SerialPort gem | |
#(http://rubygems.org/gems/serialport) | |
# from http://playground.arduino.cc/interfacing/ruby | |
require 'serialport' | |
require 'pry' | |
# https://github.com/igrigorik/em-websocket | |
# require 'em-websocket' | |
#params for serial port | |
port_str = "/dev/tty.usbmodem1411" | |
baud_rate = 9600 | |
data_bits = 8 | |
stop_bits = 1 | |
parity = SerialPort::NONE | |
@@sp = SerialPort.new(port_str, baud_rate, data_bits, stop_bits, parity) | |
def map(value, min, max, mappedMin, mappedMax) | |
min, max = [min, max].sort | |
mappedMin, mappedMax = [mappedMin, mappedMax].sort | |
# make sure the value is within the bounds | |
value = [value, min, max].sort[1] | |
value += (mappedMin - min) | |
value *= (mappedMax / (max * 1.0)) | |
value.round(2) | |
end | |
# @frequency = 3 | |
def angle(i) | |
@@sp.print i | |
# @@sp.print "\000" | |
end | |
def vibe(angle, power) | |
codes = [] | |
codes << "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F" | |
codes << "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1A\x1B\x1C\x1D\x1E\x1F" | |
codes << "\x20\x21\x22\x23\x24\x25\x26\x27\x28\x29\x2A\x2B\x2C\x2D\x2E\x2F" | |
codes << "\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x3A\x3B\x3C\x3D\x3E\x3F" | |
codes << "\x40\x41\x42\x43\x44\x45\x46\x47\x48\x49\x4A\x4B\x4C\x4D\x4E\x4F" | |
codes << "\x50\x51\x52\x53\x54\x55\x56\x57\x58\x59\x5A\x5B\x5C\x5D\x5E\x5F" | |
codes << "\x60\x61\x62\x63\x64\x65\x66\x67\x68\x69\x6A\x6B\x6C\x6D\x6E\x6F" | |
codes << "\x70\x71\x72\x73\x74\x75\x76\x77\x78\x79\x7A\x7B\x7C\x7D\x7E\x7F" | |
codes << "\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8A\x8B\x8C\x8D\x8E\x8F" | |
codes << "\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9A\x9B\x9C\x9D\x9E\x9F" | |
codes << "\xA0\xA1\xA2\xA3\xA4\xA5\xA6\xA7\xA8\xA9\xAA\xAB\xAC\xAD\xAE\xAF" | |
codes << "\xB0\xB1\xB2\xB3\xB4\xB5\xB6\xB7\xB8\xB9\xBA\xBB\xBC\xBD\xBE\xBF" | |
codes << "\xC0\xC1\xC2\xC3\xC4\xC5\xC6\xC7\xC8\xC9\xCA\xCB\xCC\xCD\xCE\xCF" | |
codes << "\xD0\xD1\xD2\xD3\xD4\xD5\xD6\xD7\xD8\xD9\xDA\xDB\xDC\xDD\xDE\xDF" | |
codes << "\xE0\xE1\xE2\xE3\xE4\xE5\xE6\xE7\xE8\xE9\xEA\xEB\xEC\xED\xEE\xEF" | |
codes << "\xF0\xF1\xF2\xF3\xF4\xF5\xF6\xF7\xF8\xF9\xFA\xFB\xFC\xFD\xFE\xFF" | |
angle_index = map(angle, 0, 360, 0, 15).to_i | |
power_index = map(power, 0, 100, 0, 15).to_i | |
codes[angle_index][power_index] | |
end | |
((1..9).to_a + ('A'..'F').to_a).each{|x| puts "x#{x}0x#{x}1x#{x}2x#{x}3x#{x}4x#{x}5x#{x}6x#{x}7x#{x}8x#{x}9x#{x}Ax#{x}Bx#{x}Cx#{x}Dx#{x}Ex#{x}F"} | |
binding.pry | |
# while (line = @@sp.gets) do | |
# next unless line | |
# line.chomp! | |
# p line | |
# # Thread.new{`afplay bell.aiff`} if line.to_i > 500 | |
# Thread.new{`afplay harlem.mp3`} if line.to_i > 500 | |
# end |
This file contains 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
#simplest ruby program to read from arduino serial, | |
#using the SerialPort gem | |
#(http://rubygems.org/gems/serialport) | |
# from http://playground.arduino.cc/interfacing/ruby | |
require 'serialport' | |
require 'pry' | |
# https://github.com/igrigorik/em-websocket | |
# require 'em-websocket' | |
#params for serial port | |
port_str = "/dev/tty.usbmodem1411" | |
baud_rate = 9600 | |
data_bits = 8 | |
stop_bits = 1 | |
parity = SerialPort::NONE | |
@@sp = SerialPort.new(port_str, baud_rate, data_bits, stop_bits, parity) | |
def map(value, min, max, mappedMin, mappedMax) | |
min, max = [min, max].sort | |
mappedMin, mappedMax = [mappedMin, mappedMax].sort | |
# make sure the value is within the bounds | |
value = [value, min, max].sort[1] | |
value += (mappedMin - min) | |
value *= (mappedMax / (max * 1.0)) | |
value.round(2) | |
end | |
# @frequency = 3 | |
def angle(i) | |
@@sp.print i | |
# @@sp.print "\000" | |
end | |
def vibe(angle, power) | |
codes = [] | |
codes << "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F" | |
codes << "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1A\x1B\x1C\x1D\x1E\x1F" | |
codes << "\x20\x21\x22\x23\x24\x25\x26\x27\x28\x29\x2A\x2B\x2C\x2D\x2E\x2F" | |
codes << "\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x3A\x3B\x3C\x3D\x3E\x3F" | |
codes << "\x40\x41\x42\x43\x44\x45\x46\x47\x48\x49\x4A\x4B\x4C\x4D\x4E\x4F" | |
codes << "\x50\x51\x52\x53\x54\x55\x56\x57\x58\x59\x5A\x5B\x5C\x5D\x5E\x5F" | |
codes << "\x60\x61\x62\x63\x64\x65\x66\x67\x68\x69\x6A\x6B\x6C\x6D\x6E\x6F" | |
codes << "\x70\x71\x72\x73\x74\x75\x76\x77\x78\x79\x7A\x7B\x7C\x7D\x7E\x7F" | |
codes << "\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8A\x8B\x8C\x8D\x8E\x8F" | |
codes << "\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9A\x9B\x9C\x9D\x9E\x9F" | |
codes << "\xA0\xA1\xA2\xA3\xA4\xA5\xA6\xA7\xA8\xA9\xAA\xAB\xAC\xAD\xAE\xAF" | |
codes << "\xB0\xB1\xB2\xB3\xB4\xB5\xB6\xB7\xB8\xB9\xBA\xBB\xBC\xBD\xBE\xBF" | |
codes << "\xC0\xC1\xC2\xC3\xC4\xC5\xC6\xC7\xC8\xC9\xCA\xCB\xCC\xCD\xCE\xCF" | |
codes << "\xD0\xD1\xD2\xD3\xD4\xD5\xD6\xD7\xD8\xD9\xDA\xDB\xDC\xDD\xDE\xDF" | |
codes << "\xE0\xE1\xE2\xE3\xE4\xE5\xE6\xE7\xE8\xE9\xEA\xEB\xEC\xED\xEE\xEF" | |
codes << "\xF0\xF1\xF2\xF3\xF4\xF5\xF6\xF7\xF8\xF9\xFA\xFB\xFC\xFD\xFE\xFF" | |
angle_index = map(angle, 0, 360, 0, 15).to_i | |
power_index = map(power, 0, 100, 0, 15).to_i | |
codes[angle_index][power_index] | |
end | |
((1..9).to_a + ('A'..'F').to_a).each{|x| puts "x#{x}0x#{x}1x#{x}2x#{x}3x#{x}4x#{x}5x#{x}6x#{x}7x#{x}8x#{x}9x#{x}Ax#{x}Bx#{x}Cx#{x}Dx#{x}Ex#{x}F"} | |
binding.pry | |
# while (line = @@sp.gets) do | |
# next unless line | |
# line.chomp! | |
# p line | |
# # Thread.new{`afplay bell.aiff`} if line.to_i > 500 | |
# Thread.new{`afplay harlem.mp3`} if line.to_i > 500 | |
# end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment