温湿度センサ(MCP9808)
温度センサ:https://www.switch-science.com/catalog/3986/
while true
I2C.write(0x18, 0x08, 0x03)
ans = I2C.read(0x18,0x05,2)
a = ans[1] | ((ans[0] & 0x1f)<<8)
a = (a.to_f)* 0.0625
puts("ans:"+a.to_s+"\r\n")
sleep(5)
end
温度気圧センサ(BME280, BMP280)
温度センサ:https://www.switch-science.com/catalog/3373/
ADRS_BME280 = 0x77
$i2c = I2C.new()
##
# 気圧計クラス(BME280, BMP280)
#
class Barometer
attr_reader :atm
attr_reader :temperature
attr_reader :dig_T1
attr_reader :dig_T2
attr_reader :dig_T3
attr_reader :dig_P1
attr_reader :dig_P2
attr_reader :dig_P3
attr_reader :dig_P4
attr_reader :dig_P5
attr_reader :dig_P6
attr_reader :dig_P7
attr_reader :dig_P8
attr_reader :dig_P9
##
# 初期化
#
def init()
$i2c.write( ADRS_BME280, 0xf5, 0xA0 )
$i2c.write( ADRS_BME280, 0xf2, 0x00 )
$i2c.write( ADRS_BME280, 0xf4, 0x25 )
datas = $i2c.read( ADRS_BME280, 0x88, 24)
if datas
@dig_T1 = (datas[1] << 8) | datas[0]
@dig_T2 = (datas[3] << 8) | datas[2]
@dig_T3 = (datas[5] << 8) | datas[4]
@dig_P1 = (datas[7] << 8) | datas[6]
@dig_P2 = (datas[9] << 8) | datas[8]
@dig_P2 = self.negative_num(@dig_P2)
@dig_P3 = (datas[11] << 8) | datas[10]
@dig_P4 = (datas[13] << 8) | datas[12]
@dig_P5 = (datas[15] << 8) | datas[14]
@dig_P5 = self.negative_num(@dig_P5)
@dig_P6 = (datas[17] << 8) | datas[16]
@dig_P6 = self.negative_num(@dig_P6)
@dig_P7 = (datas[19] << 8) | datas[18]
@dig_P8 = (datas[21] << 8) | datas[20]
@dig_P8 = self.negative_num(@dig_P8)
@dig_P9 = (datas[23] << 8) | datas[22]
end
end
def negative_num(param)
if(param >= 0x8000)
param = ((-param ^ 0xFFFF) + 1)
end
return param
end
##
# 測定開始トリガー
#
def trigger()
$i2c.write( ADRS_BME280, 0xf4, 0x25 )
end
##
# データ読み込み
#
def read()
# read pressure and temperature.
datas = $i2c.read( ADRS_BME280, 0xF7, 8 )
$i2c.write( ADRS_BME280, 0xf4, 0x27 )
a_para = ((datas[0] << 12) | (datas[1] << 4) | (datas[2] >> 4))
t_para = ((datas[3] << 12) | (datas[4] << 4) | (datas[5] >> 4))
p1 = (a_para / 16384.0 - @dig_T1 / 1024.0) * @dig_T2
p2 = (a_para / 131072.0 - @dig_T1 / 8192.0)
p2 = p2 * (a_para / 131072.0 - @dig_T1 / 8192.0) * @dig_T3
t_fine = p1 + p2
@temperature = t_fine / 5120.0
pressure = 0.0
p1 = (t_fine / 2.0) - 64000.0
p2 = (((p1 / 4.0) * (p1 / 4.0)) / 2048) * @dig_P6
p2 = p2 + ((p1 * @dig_P5) * 2.0)
p2 = (p2 / 4.0) + (@dig_P4 * 65536.0)
p1 = (((@dig_P3 * (((p1 / 4.0) * (p1 / 4.0)) / 8192)) / 8)
p1 = p1 + ((@dig_P2 * p1) / 2.0)) / 262144
p1 = ((32768 + p1) * @dig_P1) / 32768
pressure = ((1048576 - a_para) - (p2 / 4096)) * 3125
if pressure < 0x80000000
pressure = (pressure * 2.0) / p1
else
pressure = (pressure / p1) * 2
end
p1 = (@dig_P9 * (((pressure / 8.0) * (pressure / 8.0)) / 8192.0)) / 4096
p2 = ((pressure / 4.0) * @dig_P8) / 8192.0
@atm = (pressure + ((p1 + p2 + @dig_P7) / 16.0))/100
return @atm
end
end
$barometer = Barometer.new
$barometer.init()
while true
$barometer.trigger()
$barometer.read()
puts "ATM: #{$barometer.atm} hPa TEMP: #{$barometer.temperature} deg"
sleep(1)
end
モータードライバ(DRV8830)
https://www.switch-science.com/catalog/2510/
class Moter
def init()
I2C.write(0x62,0x01,0x80)
I2C.write(0x60,0x01,0x80)
end
def r(speed,r)
I2C.write(0x62,0x00,speed+r)
end
def l(speed,l)
I2C.write(0x60,0x00,speed+l)
end
def control(speed,r,l)
I2C.write(0x62,0x00,speed+r)
I2C.write(0x60,0x00,speed+l)
end
end
インスタンスがどうこう
class Moter
def initialize(n)
@num = n
I2C.write(0x60+@num,0x01,0x80)
end
def control(speed,status)
I2C.write(0x60+@num,0x00,speed+status)
end
end
サーボモータードライバ(PCA9685)
通常品:https://www.amazon.co.jp/dp/B01D1D0CX2GROVE:https://item.rakuten.co.jp/robotshopjapan/rb-see-679/
$moters = I2C.new()
def module_init()
$moters.write(0x40, 0xFA, 0x00)
$moters.write(0x40, 0xFB, 0x00)
$moters.write(0x40, 0xFC, 0x00)
$moters.write(0x40, 0xFD, 0x00)
$moters.write(0x40, 0x01, 0x04)
$moters.write(0x40, 0x00, 0x01)
sleep_ms(50)
end
def freq_set()
$moters.write(0x40, 0x00, 0x11)
$moters.write(0x40, 0xFE, 0x65)
$moters.write(0x40, 0x00, 0x01)
sleep_ms(50)
$moters.write(0x40, 0x00, 0x81)
end
def m_all_move(angle)
angle = (angle/180.0*500 + 150).to_i
$moters.write(0x40, 0xFA, 0x00)
$moters.write(0x40, 0xFB, 0x00)
$moters.write(0x40, 0xFC, angle & 0xff)
$moters.write(0x40, 0xFD, angle >>8)
end
def m_move(ch,angle)
angle = (angle/180.0*500 + 150).to_i
$moters.write(0x40, ch*4+6, 0x00)
$moters.write(0x40, ch*4+7, 0x00)
$moters.write(0x40, ch*4+8, angle & 0xff)
$moters.write(0x40, ch*4+9, angle >>8)
end
module_init()
freq_set()
m_all_move(90)
while true
m_move(6,45)
m_move(8,135)
m_move(10,90)
m_move(11,90)
sleep(2)
m_move(6,135)
m_move(8,45)
m_move(10,135)
m_move(11,45)
sleep(2)
end
OLED
https://www.switch-science.com/catalog/829/ASCIIコード
スペース(32)~Z(90)まで対応
class OLED
$oled = 0
def init()
$oled = I2C.new
$oled.write(0x3c,0x00,0xAE,0xA8,0x3F)
$oled.write(0x3c,0x00,0xD3,0x00)
$oled.write(0x3c,0x00,0x40,0xA0,0xC0,0xDA,0x12)
$oled.write(0x3c,0x00,0x81,0xFF)
$oled.write(0x3c,0x00,0xA4,0xA6,0xD5,0x80)
$oled.write(0x3c,0x00,0x2E,0x20,0x10)
$oled.write(0x3c,0x00,0x21,0,127)
$oled.write(0x3c,0x00,0x22,0,7)
$oled.write(0x3c,0x00,0x8D,0x14,0xAF)
i = 0
while i<8
$oled.write(0x3C,0x80,0xB0 + i)
j = 0
while j<16
$oled.write(0x3C,0x40,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00)
j = j+1
end
i = i+1
end
end
def send(i,name)
a=[
[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00],
[0x00, 0x00, 0x5F, 0x00, 0x00, 0x00, 0x00, 0x00],
[0x00, 0x00, 0x07, 0x00, 0x07, 0x00, 0x00, 0x00],
[0x00, 0x14, 0x7F, 0x14, 0x7F, 0x14, 0x00, 0x00],
[0x00, 0x24, 0x2A, 0x7F, 0x2A, 0x12, 0x00, 0x00],
[0x00, 0x23, 0x13, 0x08, 0x64, 0x62, 0x00, 0x00],
[0x00, 0x36, 0x49, 0x55, 0x22, 0x50, 0x00, 0x00],
[0x00, 0x00, 0x05, 0x03, 0x00, 0x00, 0x00, 0x00],
[0x00, 0x1C, 0x22, 0x41, 0x00, 0x00, 0x00, 0x00],
[0x00, 0x41, 0x22, 0x1C, 0x00, 0x00, 0x00, 0x00],
[0x00, 0x08, 0x2A, 0x1C, 0x2A, 0x08, 0x00, 0x00],
[0x00, 0x08, 0x08, 0x3E, 0x08, 0x08, 0x00, 0x00],
[0x00, 0xA0, 0x60, 0x00, 0x00, 0x00, 0x00, 0x00],
[0x00, 0x08, 0x08, 0x08, 0x08, 0x08, 0x00, 0x00],
[0x00, 0x60, 0x60, 0x00, 0x00, 0x00, 0x00, 0x00],
[0x00, 0x20, 0x10, 0x08, 0x04, 0x02, 0x00, 0x00],
[0x00, 0x3E, 0x51, 0x49, 0x45, 0x3E, 0x00, 0x00],
[0x00, 0x00, 0x42, 0x7F, 0x40, 0x00, 0x00, 0x00],
[0x00, 0x62, 0x51, 0x49, 0x49, 0x46, 0x00, 0x00],
[0x00, 0x22, 0x41, 0x49, 0x49, 0x36, 0x00, 0x00],
[0x00, 0x18, 0x14, 0x12, 0x7F, 0x10, 0x00, 0x00],
[0x00, 0x27, 0x45, 0x45, 0x45, 0x39, 0x00, 0x00],
[0x00, 0x3C, 0x4A, 0x49, 0x49, 0x30, 0x00, 0x00],
[0x00, 0x01, 0x71, 0x09, 0x05, 0x03, 0x00, 0x00],
[0x00, 0x36, 0x49, 0x49, 0x49, 0x36, 0x00, 0x00],
[0x00, 0x06, 0x49, 0x49, 0x29, 0x1E, 0x00, 0x00],
[0x00, 0x00, 0x36, 0x36, 0x00, 0x00, 0x00, 0x00],
[0x00, 0x00, 0xAC, 0x6C, 0x00, 0x00, 0x00, 0x00],
[0x00, 0x08, 0x14, 0x22, 0x41, 0x00, 0x00, 0x00],
[0x00, 0x14, 0x14, 0x14, 0x14, 0x14, 0x00, 0x00],
[0x00, 0x41, 0x22, 0x14, 0x08, 0x00, 0x00, 0x00],
[0x00, 0x02, 0x01, 0x51, 0x09, 0x06, 0x00, 0x00],
[0x00, 0x32, 0x49, 0x79, 0x41, 0x3E, 0x00, 0x00],
[0x00, 0x7E, 0x09, 0x09, 0x09, 0x7E, 0x00, 0x00],
[0x00, 0x7F, 0x49, 0x49, 0x49, 0x36, 0x00, 0x00],
[0x00, 0x3E, 0x41, 0x41, 0x41, 0x22, 0x00, 0x00],
[0x00, 0x7F, 0x41, 0x41, 0x22, 0x1C, 0x00, 0x00],
[0x00, 0x7F, 0x49, 0x49, 0x49, 0x41, 0x00, 0x00],
[0x00, 0x7F, 0x09, 0x09, 0x09, 0x01, 0x00, 0x00],
[0x00, 0x3E, 0x41, 0x41, 0x51, 0x72, 0x00, 0x00],
[0x00, 0x7F, 0x08, 0x08, 0x08, 0x7F, 0x00, 0x00],
[0x00, 0x41, 0x7F, 0x41, 0x00, 0x00, 0x00, 0x00],
[0x00, 0x20, 0x40, 0x41, 0x3F, 0x01, 0x00, 0x00],
[0x00, 0x7F, 0x08, 0x14, 0x22, 0x41, 0x00, 0x00],
[0x00, 0x7F, 0x40, 0x40, 0x40, 0x40, 0x00, 0x00],
[0x00, 0x7F, 0x02, 0x0C, 0x02, 0x7F, 0x00, 0x00],
[0x00, 0x7F, 0x04, 0x08, 0x10, 0x7F, 0x00, 0x00],
[0x00, 0x3E, 0x41, 0x41, 0x41, 0x3E, 0x00, 0x00],
[0x00, 0x7F, 0x09, 0x09, 0x09, 0x06, 0x00, 0x00],
[0x00, 0x3E, 0x41, 0x51, 0x21, 0x5E, 0x00, 0x00],
[0x00, 0x7F, 0x09, 0x19, 0x29, 0x46, 0x00, 0x00],
[0x00, 0x26, 0x49, 0x49, 0x49, 0x32, 0x00, 0x00],
[0x00, 0x01, 0x01, 0x7F, 0x01, 0x01, 0x00, 0x00],
[0x00, 0x3F, 0x40, 0x40, 0x40, 0x3F, 0x00, 0x00],
[0x00, 0x1F, 0x20, 0x40, 0x20, 0x1F, 0x00, 0x00],
[0x00, 0x3F, 0x40, 0x38, 0x40, 0x3F, 0x00, 0x00],
[0x00, 0x63, 0x14, 0x08, 0x14, 0x63, 0x00, 0x00],
[0x00, 0x03, 0x04, 0x78, 0x04, 0x03, 0x00, 0x00],
[0x00, 0x61, 0x51, 0x49, 0x45, 0x43, 0x00, 0x00],
]
$oled.write(0x3C,0x00,0xB0+i,0x21,0x00,0x7F)
j = 0
while(j<16)
b = (name[j].ord) - 32
$oled.write(0x3C,0x40,a[b][0],a[b][1],a[b][2],a[b][3],a[b][4],a[b][5],a[b][6],a[b][7])
j = j+1
end
end
end
oled = OLED.new
# 初期化
oled.init()
# oled.send(行,表示文字列16文字)
oled.send(0,"LETS!GO!MRUBY/C!")
oled.send(1,"================")
oled.send(2,"ALL HAPPY ")
2値画像表示
class OLED
$oled = 0
def init()
$oled = I2C.new
$oled.write(0x3c,0x00,0xAE,0xA8,0x3F)
$oled.write(0x3c,0x00,0xD3,0x00)
$oled.write(0x3c,0x00,0x40,0xA0,0xC0,0xDA,0x12)
$oled.write(0x3c,0x00,0x81,0xFF)
$oled.write(0x3c,0x00,0xA4,0xA6,0xD5,0x80)
$oled.write(0x3c,0x00,0x2E,0x20,0x10)
$oled.write(0x3c,0x00,0x21,0,127)
$oled.write(0x3c,0x00,0x22,0,7)
$oled.write(0x3c,0x00,0x8D,0x14,0xAF)
i = 0
while i<9
$oled.write(0x3C,0x80,0xB0 + i)
j = 0
while j<16
$oled.write(0x3C,0x40,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00)
j = j+1
end
i = i+1
end
end
def send(a)
$oled.write(0x3C,0x00,0xB0+i,0x21,0x00,0x7F)
c = 0
while(c<7)
j = 0
while(j<16)
b = j
$oled.write(0x3C,0x40,a[c][b][0],a[c][b][1],a[c][b][2],a[c][b][3],a[c][b][4],a[c][b][5],a[c][b][6],a[c][b][7])
j = j+1
end
c = c+1
end
end
end
picture=[
[[0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00],[0x00,0x80,0xE0,0xF0,0xFC,0x00,0x00,0xFC],[0xFC,0xFC,0xFC,0xFC,0xFC,0xFC,0xFC,0xFC],[0xFC,0x7C,0x3C,0x1C,0x9C,0xCC,0xE4,0xF0],[0xF8,0xFC,0xF8,0xF0,0xE4,0xCC,0x9C,0x3C],[0x7C,0xFC,0xFC,0xFC,0xFC,0xFC,0xFC,0xFC],[0xFC,0xFC,0xFC,0xFC,0x00,0x00,0xF8,0xE0],[0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00]],
[[0x00,0x00,0x00,0x80,0xC0,0xE0,0xF8,0xFC],[0xFF,0xFF,0xFF,0xFF,0xFF,0x00,0x00,0xFF],[0xFF,0x7F,0x3F,0x1F,0x8F,0xC7,0xE3,0xF1],[0xF8,0xFC,0xFE,0xFF,0xFF,0xFF,0xFF,0xFF],[0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF],[0xFE,0xFC,0xF9,0xF3,0xE7,0xCF,0x9F,0x3F],[0x7F,0xFF,0xFF,0xFF,0x00,0x00,0xFF,0xFF],[0xFF,0xFF,0xFE,0xF8,0xF0,0xC0,0x80,0x00]],
[[0x00,0x00,0x02,0x23,0xE3,0xE3,0xE3,0xE3],[0xE3,0xE3,0xE3,0xE3,0xE3,0x80,0x00,0x21],[0xE0,0xE0,0xE2,0xE3,0xE3,0xE3,0xE3,0xE3],[0xE3,0xE3,0xE3,0xE3,0xE3,0xE3,0xE3,0xE3],[0xE3,0xE3,0xE3,0xE3,0xE3,0xE3,0xE3,0xE3],[0xE3,0xE3,0xE3,0xE3,0xE3,0xE3,0xE3,0xE3],[0xE2,0xE0,0x61,0x01,0x80,0xE0,0xE3,0xE3],[0xE3,0xE3,0xE3,0xE3,0xE3,0xE3,0x63,0x23]],
[[0x00,0x00,0x00,0x00,0x00,0x01,0x03,0x07],[0x0F,0x3F,0x7F,0xFF,0xFF,0xFF,0xFE,0xF8],[0xE0,0x83,0x0F,0x3F,0xFF,0xFF,0xFF,0xFF],[0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF],[0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF],[0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x7F,0x1F],[0x87,0xE1,0xF8,0xFE,0xFF,0xFF,0xFF,0x7F],[0x3F,0x1F,0x0F,0x07,0x03,0x00,0x00,0x00],[0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00],[0x00, 0x7C, 0x04, 0x18, 0x04, 0x78, 0x00, 0x00],[0x00, 0x00, 0x7C, 0x08, 0x04, 0x00, 0x00, 0x00],[0x00, 0x3C, 0x40, 0x40, 0x7C, 0x00, 0x00, 0x00],[0x00, 0x7F, 0x48, 0x44, 0x44, 0x38, 0x00, 0x00],[0x00, 0x1C, 0xA0, 0xA0, 0x7C, 0x00, 0x00, 0x00],[0x00, 0x20, 0x10, 0x08, 0x04, 0x02, 0x00, 0x00],[0x00, 0x38, 0x44, 0x44, 0x28, 0x00, 0x00, 0x00],],
[[0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00],[0x00,0x00,0x00,0x00,0x01,0x03,0x0F,0x1F],[0x3F,0x7F,0xFE,0xF8,0xE0,0x83,0x0F,0x3F],[0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF],[0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF],[0xFF,0xFF,0x7F,0x1F,0x07,0xC1,0xF0,0xFE],[0xFF,0x7F,0x1F,0x0F,0x07,0x03,0x01,0x00],[0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00]],
[[0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00],[0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00],[0x00,0x00,0x00,0x03,0x07,0x0F,0x1E,0x38],[0x60,0x83,0x0F,0x3F,0xFF,0xFF,0xFF,0xFF],[0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x7F,0x1F],[0x07,0xC1,0x70,0x3C,0x1F,0x07,0x03,0x01],[0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00],[0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00]],
[[0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00],
[0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00],
[0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00],
[0x00,0x01,0x02,0x00,0x00,0x07,0x1F,0xFF],
[0xFF,0xFF,0xFF,0x1F,0x07,0x01,0x00,0x00],
[0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00],
[0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00],
[0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00]]
]
oled = OLED.new
# 初期化
oled.init()
# oled.send(画像配列8×16×8)
oled.send(picture)
LCD
LCD:https://www.switch-science.com/catalog/1629/
$lcd = I2C.new
sleep(1)
def rgb(r,g,b)
$lcd.write(0x62,0x00,0x00)
$lcd.write(0x62,0x08,0xFF)
$lcd.write(0x62,0x01,0x20)
$lcd.write(0x62,0x04,r)
$lcd.write(0x62,0x03,g)
$lcd.write(0x62,0x02,b)
end
def send_txt(txt)
$lcd.write(0x3E,0x80,0x02)
sleep_ms(5)
$lcd.write(0x3E,0x80,0x0C)
sleep_ms(5)
cnt = txt.size
i = 0
while(i < cnt)
if(i == 15)
$lcd.write(0x3E,0x80,0xC0)
end
$lcd.write(0x3E,0x40,(txt[i]).ord)
i = i+1
end
end
rgb(255,255,255)
send_txt("YoshihiroOgura")
3軸加速度センサ
LIS3DHTRを搭載した三軸加速度センサhttps://akizukidenshi.com/catalog/g/gM-16771/
$i2c = I2C.new()
# ====================================================
# @brief 符号付2バイトの数値の絶対値を出力
#
# @param param 数値(2バイト)
#
# @return 絶対値(整数値)
# ====================================================
def absolute(param)
if(param >= 0x8000)
param = ((-param ^ 0xFFFF) + 1)
end
return param
end
class Lis3dhtr
attr_reader :addr
# ====================================================
# @brief LIS3DHTRの初期設定
#
# @param slave_addr 使用しているLIS3DHTRのアドレス
# ====================================================
def init(slave_addr)
@addr = slave_addr
# データレート:400Hz(0xF0-0x00)
# x,y,zのデータを取得(0x07-0x00)
$i2c.write(@addr,0x20,0x77)
# 継続的に値を更新(0x80-0x00)
# フルスケール±2g(0x06-0x00)
$i2c.write(@addr,0x23,0x00)
end
# ====================================================
# @brief LIS3DHTRからX,Y,Zの加速度データを取得
#
# @return x,y,zの加速度データ(配列[x,y,z])
# ====================================================
def read_data()
# output status
# data = $i2c.read(ADDR, 0x27, 1)
# x = data[0]
# puts("status:"+x.to_s)
# x data read
data = $i2c.read(@addr, 0x28, 1)
x = data[0]
data = $i2c.read(@addr, 0x29, 1)
x += data[0] * 256
x = (absolute(x)).to_f
x /= 16000
x = (x*100).to_i
# y data read
data = $i2c.read(@addr, 0x2A, 1)
y = data[0]
data = $i2c.read(@addr, 0x2B, 1)
y += data[0] * 256
y = (absolute(y)).to_f
y /= 16000
y = (y*100).to_i
# z data read
data = $i2c.read(@addr, 0x2C, 1)
z = data[0]
data = $i2c.read(@addr, 0x2D, 1)
z += data[0] * 256
z = (absolute(z)).to_f
z /= 16000
z = (z*100).to_i
return [x,y,z]
end
end
# メインプログラム
sensor = Lis3dhtr.new
sensor.init(0x19)
sleep(2)
while true
data_raw = sensor.read_data()
puts("x:#{data_raw[0]},y:#{data_raw[1]}z:#{data_raw[2]}")
sleep_ms(50)
end
Qwiic
QwiicとはSparkFunから販売されているI2Cにて制御するモジュール。
https://www.sparkfun.com/qwiic
赤色LEDボタン
ボタンを押したときにLEDを点灯する。
led = I2C.new
while true
l = led.read(0x6F,0x03,1)
s = (l[0]&0x04)>>2
led.write(0x6F,0x19,s * 0xFF)
end
ToF測距センサ
VL53L0Xを搭載したレーザーの距離センサーhttps://www.switch-science.com/catalog/5219/
while true
status = 0
I2C.write(0x29,0x00,0x01)
while((status & 0x01) == 1)
data = I2C.read(0x29,0x14,1)
status = data[0]
end
data = I2C.read(0x29,0x14,12)
puts(data[11] + (data[10]<<8))
sleep_ms(100)
end