-
Notifications
You must be signed in to change notification settings - Fork 0
/
final_foot_board.ino
173 lines (138 loc) · 3.92 KB
/
final_foot_board.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#include "HX711.h"
#include <string.h>
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h> // Required for 16 MHz Adafruit Trinket
#endif
// HX711 circuit wiring
const int leftFoot_LOADCELL_DOUT = 2;
const int leftFoot_LOADCELL_SCK = 3;
const int rightFoot_LOADCELL_DOUT = 4;
const int rightFoot_LOADCELL_SCK = 5;
HX711 leftFootHX, rightFootHX;
//led 관련 상수
const int LEFT_LED_PIN = 6;
const int RIGHT_LED_PIN = 7;
const int LED_COUNT = 60;
Adafruit_NeoPixel leftStrip(LED_COUNT, LEFT_LED_PIN, NEO_GRB + NEO_KHZ800);
Adafruit_NeoPixel rightStrip(LED_COUNT, RIGHT_LED_PIN, NEO_GRB + NEO_KHZ800);
const int arraySize = 4;
bool leftPrev[arraySize] = { 1, 1, 1, 1 };
bool rightPrev[arraySize] = { 1, 1, 1, 1 };
unsigned long lastUpdatedTimeMillis = 0;
long leftLimit = 440000;
long rightLimit = 810000;
void shiftArray(bool arr[], int size) {
for (int i = 0; i < size - 1; i++) {
arr[i] = arr[i + 1];
}
}
void printArray(bool arr[], int size) {
for (int i = 0; i < size; i++) {
if (arr[i]) {
Serial.print("1");
} else {
Serial.print("0");
}
Serial.print(" ");
}
Serial.println("");
}
bool isLeftStep(long scale) {
//Serial.println(scale);
if(leftPrev[3]==0) return scale > leftLimit-1000;
return scale > leftLimit;
}
bool isRightStep(long scale) {
//Serial.println(scale);
if(rightPrev[3]==0) return scale < rightLimit + 1000;
return scale < rightLimit;
}
int firstPixelHueBy256 = 0;
void rainbow(Adafruit_NeoPixel* strip) {
strip->rainbow(firstPixelHueBy256*256*20);
strip->show();
firstPixelHueBy256++;
}
void deem(Adafruit_NeoPixel* strip) {
strip->clear();
strip->show();
}
bool isStart() {
if (leftPrev[3] != leftPrev[2]
&& rightPrev[3] != rightPrev[2]
&& leftPrev[3] != rightPrev[3]) {
return true;
}
return false;
}
bool isStop() {
return millis() - lastUpdatedTimeMillis > 2000;
}
bool isJump() {
if (leftPrev[3] == 0 && rightPrev[3] == 0) {
return true;
}
return false;
}
void setup() {
Serial.begin(9600);
leftFootHX.begin(leftFoot_LOADCELL_DOUT, leftFoot_LOADCELL_SCK);
rightFootHX.begin(rightFoot_LOADCELL_DOUT, rightFoot_LOADCELL_SCK);
leftStrip.begin(); // INITIALIZE NeoPixel strip object (REQUIRED)
leftStrip.show(); // Turn OFF all pixels ASAP
leftStrip.setBrightness(50); // Set BRIGHTNESS to about 1/5 (max = 255)
rightStrip.begin(); // INITIALIZE NeoPixel strip object (REQUIRED)
rightStrip.show(); // Turn OFF all pixels ASAP
rightStrip.setBrightness(50);
}
String prevStatus = "stop";
String currentStatus = "stop";
void loop() {
if (leftFootHX.is_ready() || rightFootHX.is_ready()) {
bool curLeftIsStep = isLeftStep(leftFootHX.read());
bool curRightIsStep = isRightStep(rightFootHX.read());
if (leftPrev[3] != curLeftIsStep || rightPrev[3] != curRightIsStep) {
shiftArray(leftPrev, arraySize);
shiftArray(rightPrev, arraySize);
leftPrev[3] = isLeftStep(leftFootHX.read());
rightPrev[3] = isRightStep(rightFootHX.read());
lastUpdatedTimeMillis = millis();
}
// Serial.println("");
if(leftPrev[3]==1) {
rainbow(&leftStrip);
} else {
deem(&leftStrip);
}
if(rightPrev[3]==1) {
rainbow(&rightStrip);
} else {
deem(&rightStrip);
}
bool start = isStart();
bool stop = isStop();
bool jump = isJump();
if (stop) {
currentStatus = "stop";
} else if (start) {
currentStatus = "start";
} else if (jump) {
currentStatus = "jump";
}
// Serial.print("left : ");
// printArray(leftPrev,4);
// Serial.print("right : ");
// printArray(rightPrev,4);
if(prevStatus == "start" && currentStatus == "start") {
Serial.println("start");
}
else if (prevStatus != currentStatus) {
Serial.println(currentStatus);
prevStatus = currentStatus;
}
} else {
Serial.println("HX711 not found.");
}
delay(100);
}