산책로봇(faet. 비컨)
Makerist
예제&실습
0
723
2024.03.12 22:37
이번예제는 비콘을 사용한 산책로봇 입니다.
#include <SoftwareSerial.h> SoftwareSerial bluetoothSerial(2, 3); // HM-10 Bluetooth 모듈을 위한 소프트웨어 시리얼 설정 int robotSpeed = 100; // 로봇의 이동 속도 설정 int beaconLocation = 0; // 비콘의 위치 정보를 저장할 변수 void setup() { Serial.begin(9600); // 시리얼 통신 시작 bluetoothSerial.begin(9600); // Bluetooth 시리얼 통신 시작 } void loop() { if (bluetoothSerial.available()) { // Bluetooth 모듈로부터 데이터를 수신했는지 확인 char receivedChar = (char)bluetoothSerial.read(); // 수신된 문자를 읽음 if (receivedChar == 'n') { // 줄 바꿈 문자가 수신되었는지 확인 // 수신된 데이터를 비콘 위치로 저장 beaconLocation = atoi(receivedData.c_str()); Serial.print("비콘 위치: "); Serial.println(beaconLocation); // 비콘 위치로 이동하는 함수 호출 moveTo(beaconLocation); receivedData = ""; // 수신된 데이터를 저장한 변수 초기화 } else { receivedData += receivedChar; // 수신된 문자를 데이터 변수에 추가 } } } // 비콘 위치로 이동하는 함수 void moveTo(int targetLocation) { Serial.print("로봇이 "); Serial.print(targetLocation); Serial.println("로 이동합니다."); // 로봇을 이동시키는 코드를 추가하세요. }