티스토리 뷰
블루투스통신
코드내용
#include <SoftwareSerial.h> // 블루투스 통신을 위한 SoftwareSerial 라이브러리를 불러온다.
SoftwareSerial BTSerial(2, 3); //
SoftwareSerial(RX, TX) byte buffer[1024]; // 데이터를 수신 받을 버퍼
int bufferPosition; // 버퍼에 데이타를 저장할 때 기록할 위치
boolean temp = 0; void setup(){ BTSerial.begin(9600); Serial.begin(9600); pinMode(13, OUTPUT); bufferPosition = 0; // 버퍼 위치 초기화 }
void loop(){ if (BTSerial.available()){ // 블루투스로 데이터 수신 byte data = BTSerial.read(); // 수신 받은 데이터 저장 Serial.write(data); // 수신된 데이터 시리얼 모니터로 출력 buffer[bufferPosition++] = data; // 수신 받은 데이터를 버퍼에 저장 if(data == '1'){ // 블루투스를 통해 '1' 이 들어오면 if(temp == 0){ // LED가 꺼있을 경우 LED를 켭니다. digitalWrite(13, HIGH); temp = 1; }else{ // LED가 켜져있을 경우 LED를 끕니다. digitalWrite(13, LOW); temp = 0; } } if(data == '\n'){ // 문자열 종료 표시 buffer[bufferPosition] = '\0'; bufferPosition = 0; } } }
아두이노 코드
https://www.youtube.com/watchtime_continue=25&v=4IOGn_-UgF0
#include <SoftwareSerial.h> //import library software serial, SoftwareSerial serial(0, 1); //TX, RX at ATtiny 44, 20MHz external
void setup() { serial.begin(9600); pinMode(7, OUTPUT); //led on the board digitalWrite(7, HIGH); //turn on the led }
void loop() { long val; val = analogRead(3); //save the sensor value to val val = map(val, 450, 680, 0, 1023); val = constrain(val, 0, 1023); serial.println(val); //send the data to serial delay(10); }
https://www.youtube.com/watch?time_continue=25&v=4IOGn_-UgF0
프로세싱코드
https://www.youtube.com/watchtime_continue=25&v=4IOGn_-UgF0
import processing.serial.*; int i; Serial myPort; float data; public static int ON = 1; public static int OFF = 0;
void setup() { size (1200, 800); // set resolution delay(3000); // make delay for communication well String portName = Serial.list()[2]; //select serial port in the Serial list println(Serial.list()); myPort = new Serial(this, portName, 9600); }
void draw() { background(255); //background color - write ellipse(600,400,data,data); //make circle, the center is (600,400), fill(0,0,0); //the color inside of the circle is black. }
void serialEvent(Serial p) { String message = myPort.readStringUntil(10); // if (message != null) //when there is a data, { print("Received: "); //print the data println(message); data = float(message); //save the data } }
'팹랩메이커양성프로젝트' 카테고리의 다른 글
| 제13회차--아두이노 (0) | 2017.11.22 |
|---|---|
| 제20회차--프로젝트발표와 마무리 (0) | 2017.11.22 |
| 18회차--아두이노시리얼통신 (0) | 2017.11.22 |
| 제17회차--아두이노 (0) | 2017.11.22 |
| 제16회차--아두이노 (0) | 2017.11.22 |