Skip to content

Instantly share code, notes, and snippets.

@4noha
Last active March 15, 2018 06:53
Show Gist options
  • Save 4noha/c26463835cbcde566295f09212596ab8 to your computer and use it in GitHub Desktop.
Save 4noha/c26463835cbcde566295f09212596ab8 to your computer and use it in GitHub Desktop.
# ライブラリのインストール
# gem install serialport
require 'io/console'
require 'serialport'
sp = SerialPort.new('/dev/ttyS0', 9600, 8, 1, 0) # device, rate, data, stop, parity
puts "Angle( C-c: exit )\n1. 0°\n2. 90°\n3. 180°"
# ArduinoからのAngle情報受信ループ
while angle_str = sp.readline.chomp.strip
if angle_str
# ユーザのコマンド選択ループ
while c = STDIN.getch
exit if c == ?\C-c
    # キーのValidation
if c == "1" || c == "2" || c == "3"
# ファイル書き込み
File.open("./data.txt", "a") do |f|
f.puts("angle,#{angle_str},feel,#{c}");
end
print c
puts "\nAngle( C-c: exit )\n1. 0°\n2. 90°\n3. 180°"
sp.puts(c) #ユーザの入力をArduinoに送信
# コマンド選択ループ脱出
break
else
puts "Prease choose 1-3 number"
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment