1. HC SR04 테스트
갈리레오 보드에서 아두이노를 이용하여 HC SR04 초음파 거리센서 테스트를 해보았다.
HC SR04 Spec
• Working Voltage : DC 5 V
• Working Current : 15mA
• Working Frequency : 40Hz
• Max Range : 4m
• Min Range : 2cm
• MeasuringAngle : 15 degree
• Trigger Input Signal : 10uS TTL pulse
• Echo Output Signal : Input TTL lever signal and the range in proportion
• Dimension : 45*20*15mm
• Working Current : 15mA
• Working Frequency : 40Hz
• Max Range : 4m
• Min Range : 2cm
• MeasuringAngle : 15 degree
• Trigger Input Signal : 10uS TTL pulse
• Echo Output Signal : Input TTL lever signal and the range in proportion
• Dimension : 45*20*15mm
기존의 거리 센서를 동작하는 아두이노 코드를 이용하였다.
사용된 코드의 정보는 다음과 같다.
/*
HC-SR04 Ping distance sensor:
VCC to arduino 5v
GND to arduino GND
Echo to Arduino pin 7
Trig to Arduino pin 8
This sketch originates from Virtualmix: http://goo.gl/kJ8Gl
Has been modified by Winkle ink here: http://winkleink.blogspot.com.au/2012/05/arduino-hc-sr04-ultrasonic-distance.html
And modified further by ScottC here: http://arduinobasics.blogspot.com.au/2012/11/arduinobasics-hc-sr04-ultrasonic-sensor.html
on 10 Nov 2012.
*/
HC-SR04 Ping distance sensor:
VCC to arduino 5v
GND to arduino GND
Echo to Arduino pin 7
Trig to Arduino pin 8
This sketch originates from Virtualmix: http://goo.gl/kJ8Gl
Has been modified by Winkle ink here: http://winkleink.blogspot.com.au/2012/05/arduino-hc-sr04-ultrasonic-distance.html
And modified further by ScottC here: http://arduinobasics.blogspot.com.au/2012/11/arduinobasics-hc-sr04-ultrasonic-sensor.html
on 10 Nov 2012.
*/
아래 첨부된 코드는 원 코드에서 거리가 변할 때만 UART로 출력하도록 수정되었다.
-
#define echoPin 7 // Echo Pin
-
#define trigPin 8 // Trigger Pin
-
#define LEDPin 13 // Onboard LED
-
-
int maximumRange = 200; // Maximum range needed
-
int minimumRange = 0; // Minimum range needed
-
long duration, distance, prev_dist; // Duration used to calculate distance
-
-
void setup() {
-
Serial.begin (9600);
-
prev_dist = -1;
-
pinMode(trigPin, OUTPUT);
-
pinMode(echoPin, INPUT);
-
pinMode(LEDPin, OUTPUT); // Use LED indicator (if required)
-
}
-
-
void loop() {
-
/* The following trigPin/echoPin cycle is used to determine the
-
distance of the nearest object by bouncing soundwaves off of it. */
-
digitalWrite(trigPin, LOW);
-
delayMicroseconds(2);
-
-
digitalWrite(trigPin, HIGH);
-
delayMicroseconds(10);
-
-
digitalWrite(trigPin, LOW);
-
duration = pulseIn(echoPin, HIGH);
-
-
//Calculate the distance (in cm) based on the speed of sound.
-
distance = duration/58.2;
-
-
if (distance >= maximumRange || distance <= minimumRange){
-
/* Send a negative number to computer and Turn LED ON
-
to indicate "out of range" */
-
distance = prev_dist;
-
digitalWrite(LEDPin, HIGH);
-
}
-
else
-
{
-
digitalWrite(LEDPin, LOW);
-
}
-
-
if(distance != prev_dist)
-
{
-
/* Send the distance to the computer using Serial protocol, and
-
turn LED OFF to indicate successful reading. */
-
Serial.println(distance);
-
prev_dist = distance;
-
}
-
-
//Delay 50ms before next reading.
-
delay(50);
-
}
-
2. SG90 서보모터 Test
갈릴레오용 아두이노 코드에 서보모터 라이브러리와 예제가 포함되어 있다.
.\arduino-1.5.3-Intel.1.0.4\libraries\Servo\examples\Sweep\ 이 경로에 포함된 예제파일을 실해하면 그대로 동작한다.
사용된 서보모터 SG90 Spec은 다음과 같다.
• Weight: 9 g
• Dimension: 22.2 x 11.8 x 31 mm approx.
• Stall torque: 1.8 kgf·cm
• Operating speed: 0.1 s/60 degree
• Operating voltage: 4.8 V (~5V)
• Dead band width: 10 µs
• Temperature range: 0 ºC – 55 ºC
• Dimension: 22.2 x 11.8 x 31 mm approx.
• Stall torque: 1.8 kgf·cm
• Operating speed: 0.1 s/60 degree
• Operating voltage: 4.8 V (~5V)
• Dead band width: 10 µs
• Temperature range: 0 ºC – 55 ºC
<동작코드>
0 ~ 180도의 값을 인자로 받아 서보를 제어하는 코드 이다.
숫자를 입력후 s 를 입력하여 저장된다.
a 를 누를 경우 0 ~ 180도 180 ~ 0도로 값을 증가 시키며 움직인다.
-
// Sweep
-
// by BARRAGAN <http://barraganstudio.com>
-
// This example code is in the public domain.
-
-
-
#include <Servo.h>
-
-
Servo myservo; // create servo object to control a servo
-
// a maximum of eight servo objects can be created
-
-
int pos = 0; // variable to store the servo position
-
-
void setup()
-
{
-
myservo.attach(9); // attaches the servo on pin 9 to the servo object
-
myservo.write(0);
-
-
Serial.begin(115200);
-
Serial.println("Ready");
-
}
-
-
-
void loop()
-
{
-
static int v = 0;
-
-
if ( Serial.available()) {
-
char ch = Serial.read();
-
-
switch(ch) {
-
case '0'...'9':
-
v = v * 10 + ch - '0';
-
Serial.println(v);
-
break;
-
case 's':
-
myservo.write(v);
-
v = 0;
-
Serial.println(v);
-
break;
-
case 'a':
-
for(pos = 0; pos < 180; pos += 1) // goes from 0 degrees to 180 degrees
-
{ // in steps of 1 degree
-
myservo.write(pos); // tell servo to go to position in variable 'pos'
-
delay(15); // waits 15ms for the servo to reach the position
-
}
-
for(pos = 180; pos>=1; pos-=1) // goes from 180 degrees to 0 degrees
-
{
-
myservo.write(pos); // tell servo to go to position in variable 'pos'
-
delay(15); // waits 15ms for the servo to reach the position
-
}
-
break;
-
}
-
}
-
}