PWM(外部LED)
PWM.new()
PWM.pin(16)
PWM.start(2)
PWM.cycle(0x95A,4)
while true
PWM.rate(80,2)
sleep 1
PWM.rate(40,2)
sleep 1
PWM.rate(20,2)
sleep 1
end
圧電ブザー
http://wiki.seeedstudio.com/Grove-Buzzer/
PWM.new()
PWM.pin(16)
PWM.start(2)
while true
PWM.cycle(0x95A,4)
sleep 1
PWM.cycle(0x84D,4)
sleep 1
PWM.cycle(0x76A,4)
sleep 1
PWM.cycle(0x6EF,4)
sleep 1
PWM.cycle(0x639,4)
sleep 1
PWM.cycle(0x58C,4)
sleep 1
PWM.cycle(0x4F3,4)
sleep 1
end
簡易mml再生
TBL_FREQ = [0x95A, 0x84D, 0x76A, 0x6EF, 0x639, 0x58C, 0x4F3,
0x4AB, 0x428, 0x3B4, 0x37F, 0x31D, 0x2BF, 0x278]
TBL_LENGTH = {1=>2000, 2=>1000, 4=>500, 8=>250}
PWM.new()
PWM.pin(16)
PWM.start(2)
def tone( freq )
if freq == 0
PWM.cycle(0,4)
return
end
PWM.cycle(freq ,4)
end
def play( mml )
mnote = nil
mlength = 4
octabe = 0
mml.each_char {|ch|
# 音階指示か?
if "CDEFGABR".index(ch)
play_sub( mnote, octabe, mlength )
mnote = ch
next
end
# 音長か?
if TBL_LENGTH[ch.to_i]
mlength = ch.to_i
next
end
# オクターブ上下か?
if ch == "<" || ch == ">"
play_sub( mnote, octabe, mlength )
if ch == "<"
octabe += 1
else
octabe -= 1
end
mnote = nil
next
end
}
play_sub( mnote, octabe, mlength )
end
def play_sub( mnote, octabe, mlength )
if !mnote
return
end
if mnote == "R"
sleep_ms( TBL_LENGTH[mlength] || 1 )
return
end
idx = "CDEFGAB".index(mnote)
if idx
idx = octabe * 7 + idx
if TBL_FREQ[idx]
tone( TBL_FREQ[idx] )
#display( idx )
sleep_ms( TBL_LENGTH[mlength] || 1 )
tone( 0 )
end
return
end
end
mml3 = "<C8DE4>C8DE4<C8DE4>C8DE4<C8DE4>C8DE4<E8FG4EDC A8AAFDDEFGGGEC4R8 EF4>A<C8>BAB<C2"
play mml3
SG90(サーボモータ)
http://akizukidenshi.com/catalog/g/gM-08761/
PWM.new()
PWM.pin(18)
PWM.start(4)
PWM.cycle(0xC35,6)
while true
PWM.rate(3,4)
sleep 1
PWM.rate(6,4)
sleep 1
PWM.rate(12,4)
sleep 1
end