728x90
개발 내용
- 거리센서를 이용한 팔굽혀펴기 기록
- LED strip를 이용한 기록 표시
- 부저를 이용한 비프음 출력
개발진행
1. ESP32
2. LED Strip
- LED strip 2개 제어
소스코드
#include <FastLED.h>
#define NUM_LEDS 20
#define LED_PIN 2
#define NUM_LEDS2 32
#define LED_PIN2 4
CRGB leds[NUM_LEDS];
CRGB leds2[NUM_LEDS2];
void setup() {
Serial.begin(115200);
FastLED.addLeds<WS2812B, LED_PIN, GRB>(leds, NUM_LEDS);
FastLED.addLeds<WS2812B, LED_PIN2, GRB>(leds2, NUM_LEDS2);
FastLED.setBrightness(50);
tf_mini_setup();
}
int j = 0;
void loop() {
int i = 0;
for (; i < j; i++) {
leds[i] = CRGB(255, 0, 0);
leds2[i] = CRGB(0, 0, 255);
}
for (; i < NUM_LEDS; i++) {
leds[i] = CRGB(0, 0, 0);
leds2[i] = CRGB(0, 0, 0);
}
FastLED.show();
delay(100);
Serial.println(j);
j += 1;
if(NUM_LEDS < j) j = 0;
}
테스트
3. 거리센서
- Serial2 사용
- TF 미니-S 라이다 거리 측정센서 -12미터 (TFMini-S - Micro LiDAR Module)
참조소스코드 | |
How to use TFMini-S LiDAR Distance Sensor with Arduino | https://how2electronics.com/how-to-use-tfmini-s-lidar-distance-sensor-with-arduino/ |
https://devicein.tistory.com/89 | |
ESP32 Hardware Serial2 Example | https://circuits4you.com/2018/12/31/esp32-hardware-serial2-example/ |
- 테스트
- 테스트 소스코드
#include "TFMini.h"
TFMini tfmini;
// SoftwareSerial SerialTFMini(16, 17); //The only value that matters here is the first one, 2, Rx
void getTFminiData(int* distance, int* strength)
{
static char i = 0;
char j = 0;
int checksum = 0;
static int rx[9];
if (Serial2.available())
{
rx[i] = Serial2.read();
if (rx[0] != 0x59)
{
i = 0;
}
else if (i == 1 && rx[1] != 0x59)
{
i = 0;
}
else if (i == 8)
{
for (j = 0; j < 8; j++)
{
checksum += rx[j];
}
if (rx[8] == (checksum % 256))
{
*distance = rx[2] + rx[3] * 256;
*strength = rx[4] + rx[5] * 256;
}
i = 0;
}
else
{
i++;
}
}
}
void setup()
{
Serial.begin(115200); //Initialize hardware serial port (serial debug port)
while (!Serial); // wait for serial port to connect. Needed for native USB port only
Serial.println ("Initializing...");
Serial2.begin(TFMINI_BAUDRATE); //Initialize the data rate for the SoftwareSerial port
tfmini.begin(&Serial2); //Initialize the TF Mini sensor
}
void loop()
{
int distance = 0;
int strength = 0;
getTFminiData(&distance, &strength);
while (!distance)
{
getTFminiData(&distance, &strength);
if (distance)
{
Serial.println(distance);
// Serial.print("cm\t");
// Serial.print("strength: ");
// Serial.println(strength);
break;
}
}
delay(10);
}
거리센서 테스트
아두이노 시리얼 모니터를 이용한 테스트 |
테스트동영상
거리센서에 따라 LED STRIP 표시하기
테스트영상
- 거리센서에 따라 LED Strip를 표시함.
#include <FastLED.h>
#define NUM_LEDS 20
#define LED_PIN 2
#define NUM_LEDS2 32
#define LED_PIN2 4
CRGB leds[NUM_LEDS];
CRGB leds2[NUM_LEDS2];
extern void tf_mini_setup();
extern int tf_mini_loop();
void setup() {
Serial.begin(115200);
FastLED.addLeds<WS2812B, LED_PIN, GRB>(leds, NUM_LEDS);
FastLED.addLeds<WS2812B, LED_PIN2, GRB>(leds2, NUM_LEDS2);
FastLED.setBrightness(50);
tf_mini_setup();
}
void loop() {
int distance = tf_mini_loop();
// Serial.println(j);
Serial.println(distance);
int ledc = distance / 2;
if(NUM_LEDS < ledc) ledc = NUM_LEDS;
int i = 0;
for (; i < ledc; i++) {
leds[i] = CRGB(255, 0, 0);
leds2[i] = CRGB(0, 0, 255);
}
for (; i < NUM_LEDS; i++) {
leds[i] = CRGB(0, 0, 0);
leds2[i] = CRGB(0, 0, 0);
}
FastLED.show();
delay(10);
}
팔굽혀펴기 테스트 동영상
센서 동작테스트
실제 팔굽혀펴기 테스트