Last active
August 13, 2016 08:35
-
-
Save sh4869/10296864 to your computer and use it in GitHub Desktop.
Rubyでserialportを弄る簡単なスクリプト
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
char ch = 'R'; //送受信の時に使う文字列。 | |
int i = 1; | |
void setup() | |
{ | |
Serial.begin(9600); | |
pinMode(13,OUTPUT); | |
} | |
void loop() | |
{ | |
while(Serial.available()){ | |
ch = Serial.read(); | |
if(ch == 'y'){ | |
digitalWrite(13,HIGH); | |
} | |
if(ch == 'n'){ | |
digitalWrite(13,LOW); | |
} | |
} | |
} |
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
require 'rubygems' | |
require 'serialport' | |
@serial_port = "/dev/ttyACM0" #シリアルのポートを指定。OSによって違うので確認のこと。 | |
@serial_bps = 9600 | |
sp = SerialPort.new(@serial_port,@serial_bps) | |
count = 0 | |
loop do | |
sp.write('y') #serialへの書き込み | |
sleep(2) | |
sp.write('n') | |
sleep(2) | |
if count == 20 | |
break | |
end | |
count += 1 | |
puts "#{count}" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment