#include <Wire.h>
#include <SoftwareSerial.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_ADXL345_U.h>
// ADXL345 모듈 설정
Adafruit_ADXL345_Unified adxl = Adafruit_ADXL345_Unified(12345);
// HC-06 모듈 설정 (TX, RX)
SoftwareSerial bluetooth(10, 11);
unsigned long previousMillis = 0; // 이전 시간 저장
const long interval = 1000; // 데이터 전송 간격 (밀리초 단위)
void setup() {
// 시리얼 모니터와 블루투스 초기화
Serial.begin(9600);
bluetooth.begin(9600);
// ADXL345 초기화 확인
if (!adxl.begin()) {
Serial.println("No ADXL345 detected ... Check your wiring!");
while (1);
}
adxl.setRange(ADXL345_RANGE_2_G);
}
void loop() {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
sensors_event_t event;
adxl.getEvent(&event);
// 각도 계산
float x_angle = atan(event.acceleration.x / sqrt(pow(event.acceleration.y, 2) + pow(event.acceleration.z, 2))) * 180 / PI;
float y_angle = atan(event.acceleration.y / sqrt(pow(event.acceleration.x, 2) + pow(event.acceleration.z, 2))) * 180 / PI;
// 자세 결정
String posture = determinePosture(x_angle, y_angle);
// 자세 정보 시리얼 모니터와 블루투스에 출력
Serial.print("Posture: ");
Serial.println(posture);
bluetooth.println(posture); // 여기서 데이터가 전송됨을 확인
}
}
String determinePosture(float x_angle, float y_angle) {
if (abs(x_angle) <= 10 && abs(y_angle) <= 10) {
return "GOOD";
} else if (abs(x_angle) <= 20 && abs(y_angle) <= 20) {
return "SLIGHT";
} else {
return "BAD";
}
}
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.opensource">
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
<uses-permission android:name="android.permission.BLUETOOTH_SCAN" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>