'Research Topics/Arduino'에 해당되는 글 3건

  1. 2009.02.23 Arduino - XBee (XBee Shield)
  2. 2009.02.16 Arduino - Processing 연동
  3. 2009.02.14 Arduino Basic - 초음파센서 1
Research Topics/Arduino2009. 2. 23. 12:54
Arduino에서 XBee 를 사용하는 방법입니다. 책 Making Things Talk 의 203페이지에서는 XBee 칩의 초기화
및 주소 설정 과정이 있습니다만, 브로드케스팅방식을 사용하는데는 굳이 위의 주소설정 과정을 거치지 않아도 잘 동작 합니다.

책에서는 XBee 의 좁은 핀을 넓혀주는 Breakout board를 사용하거나 Arduino 에 장착되기 쉽게 만든 XBee Shield를 사용합니다. 여기서는 sadi의 XBee Shield 를 사용하였으나, 굳이 Shield 가 없어도 구성 방법은 동일합니다. XBee의 핀에 소켓등을 꽂아 핀번호에 맞게 Power / Tx, Rx 등을 연결만 해주면 됩니다. (선들이 좀 주렁주렁 지저분 해지겠지만요)

sadi XBee shield 를 사용할 경우 다음과 같이 Analog 와 Digital Pin 을 사용할 수 있습니다.
주의할점은, USB 를 통하여 컴퓨터와 통신 (Arduino 프로그램 작성 및 다운로드 등) 하는 경우는 우측하단의 점퍼 두개를 왼쪽두개로 잡아주어야 합니다. (XBee 와 통신시에는 오른쪽 두개)

XBee 간의 통신은 브로드캐스팅 방식으로, 별 특별한 코드 없이 일반적인 Serial.print() 와 Serial.read() 로 정보를 쏘고 수신할 수 있습니다. 다음과 같이 마스터/슬레이브 또는 서버/클라이언트 구분없이 동일 코드에서 송신/수신을 테스트 해 볼 수 있습니다.

void setup() {
  Serial.begin(19200);
  pinMode(txLed, OUTPUT);
  pinMode(rxLed, OUTPUT);
}

void loop() {

  if(Serial.available() > 0) { // Listening
    digitalWrite(rxLed, HIGH);
    handleSerial();
 
    ... 중간생략
 
  char sensorValue = readSensor();
 
  if(sensorValue > 0) {
   
    digitalWrite(txLed, HIGH);
    Serial.print(sensorValue, DEC);
    Serial.print("\r");
    digitalWrite(txLed, LOW);
  }
}

void handleSerial() {

  inByte = Serial.read();
 
  if((inByte >= '0') && (inByte <= '9')) {
   inString[stringPos] = inByte;
   stringPos++;
  }
 
  if(inByte == '\r') {
   int brightness = atoi(inString);
   analogWrite(analogLed, brightness);
  
   for(int c=0 ; c<stringPos ; c++) {
    inString[c] = 0;  
   }
  
   stringPos = 0;
 
  }


통신 가능한 거리는 실내에서는 30미터가 안되는 것 같고 확 트인 야외에서는 100미터 정도 가능하다고 합니다.


Posted by 알 수 없는 사용자
Research Topics/Arduino2009. 2. 16. 01:31

Arduino 보드에서 Arduino 프로그램으로 USB Serial 방식으로 전송하기때문에, Processing 에서도 Serial API를 활용하여
Arduino 보드에서 보내는 데이터를 읽어낼 수 있다.

Arduino 코드에서는 Serial.print를 이용하여 Serial 데이터를 송신
int flexiSensor = 5;
int button1 = 2;
int button2 = 3;


int flexiValue = 0;
int button1Value = 0;
int button2Value = 0;

void setup()
{
  Serial.begin(9600);
 
  pinMode(button1, INPUT); 
  pinMode(button2, INPUT);
}

void loop ()
{
  flexiValue = analogRead(flexiSensor);
  button1Value = digitalRead(button1);
  button2Value = digitalRead(button2);
 
  Serial.print(flexiValue, DEC);
  Serial.print(",");
  Serial.print(button1Value, DEC);
  Serial.print(","); 
  Serial.println(button2Value, DEC);
}

Processing 에서는 serial 라이브러리를 추가해주고,  Serial.list() 를 이용해 목록확인, 읽어들일 포트를 설정한 후

serialEvent 함수를 구현하여 그안에서 데이터가 왔을경우에 대한 처리를 한다
import processing.serial.*;

Serial inputPort;
int linefeed = 10;
int ballPosition = 10;

void setup()
{
  size(640,480);
  println(Serial.list()); // 가용한 serial port 의 자원의 목록을 보여준다
 
  inputPort = new Serial(this, Serial.list()[0], 9600); // 이경우는 목록의 [0] 번이 Arduino 인경우임
  inputPort.bufferUntil(linefeed);
  
}


void draw()
{
   background(0);
   fill(255,230,0);
   ellipse(ballPosition, 50, 50, 50);
   print("ballPosition=" + ballPosition);

}

void serialEvent(Serial inputPort) // serial port 로 데이터가 들어올 경우 호출되는 함수
{
  String inputString = inputPort.readStringUntil(linefeed);
 
  if(inputString != null)
  {
    inputString = trim(inputString);
    int sensorValues[] = int(split(inputString, ','));
    ballPosition = sensorValues[0];
   
  }
}

이와 같은 방법으로 Arduino 를 통한 다양한 센서, 버튼 등의 physical 한 interface 의 입력을 받아
Processing 을 통하여 graphical 한 visual 요소를 표현 할 수 있다.


Posted by 알 수 없는 사용자
Research Topics/Arduino2009. 2. 14. 11:13

초음파센서 SRF05 와이어링과 샘플코드 - Arduino 프로그램의 Serial Monitor 에 cm, inch 로 거리 출력

Mode선과 0V Gnd 모두 Gnd로 연결하면 됨.





int pingPin = 9;

void setup()                    // run once, when the sketch starts
{
   Serial.begin(9600);
}

void loop()                     // run over and over again
{
    long duration, inches, cm;

  // The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
  // We give a short LOW pulse beforehand to ensure a clean HIGH pulse.
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);

  // The same pin is used to read the signal from the PING))): a HIGH
  // pulse whose duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off of an object.
  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);

  // convert the time into a distance
  inches = microsecondsToInches(duration);
  cm = microsecondsToCentimeters(duration);

  Serial.print(inches);
  Serial.print("in, ");
  Serial.print(cm);
  Serial.print("cm");
  Serial.println();

  delay(100);
}

long microsecondsToInches(long microseconds)
{
  // According to Parallax's datasheet for the PING))), there are
  // 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
  // second).  This gives the distance travelled by the ping, outbound
  // and return, so we divide by 2 to get the distance of the obstacle.
  // See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
  return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds)
{
  // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  // The ping travels out and back, so to find the distance of the
  // object we take half of the distance travelled.
  return microseconds / 29 / 2;
}







Posted by 알 수 없는 사용자