-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBike_Resistance_Smoothing.ino
210 lines (179 loc) · 5.4 KB
/
Bike_Resistance_Smoothing.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
int fsrAnalogPin = A0; // FSR is connected to analog 2
int fsrReading; // the analog reading from the FSR resistor divider
int fsrScaled;
int fsrScaledOld;
int fsrScaledBnc;
int counter = 0;
Adafruit_SSD1306 display = Adafruit_SSD1306();
#if defined(ESP8266)
#define BUTTON_A 0
#define BUTTON_B 16
#define BUTTON_C 2
#define LED 0
#elif defined(ESP32)
#define BUTTON_A 15
#define BUTTON_B 32
#define BUTTON_C 14
#define LED 13
#elif defined(ARDUINO_STM32F2_FEATHER)
#define BUTTON_A PA15
#define BUTTON_B PC7
#define BUTTON_C PC5
#define LED PB5
#elif defined(TEENSYDUINO)
#define BUTTON_A 4
#define BUTTON_B 3
#define BUTTON_C 8
#define LED 13
#elif defined(ARDUINO_FEATHER52)
#define BUTTON_A 31
#define BUTTON_B 30
#define BUTTON_C 27
#define LED 17
#else // 32u4, M0, and 328p
#define BUTTON_A 9
#define BUTTON_B 6
#define BUTTON_C 5
#define LED 13
#endif
#if (SSD1306_LCDHEIGHT != 32)
#error("Height incorrect, please fix Adafruit_SSD1306.h!");
#endif
// Define the number of samples to keep track of for smoothing. The higher the number, the
// more the readings will be smoothed, but the slower the output will respond to
// the input. Using a constant rather than a normal variable lets us use this
// value to determine the size of the readings array.
const int numReadings = 100;
int readings[numReadings]; // the readings from the analog input
int readIndex = 0; // the index of the current reading
int total = 0; // the running total
int average = 0;
void setup() {
Serial.begin(9600);
// initialize all the smoothing readings to 0:
for (int thisReading = 0; thisReading < numReadings; thisReading++) {
readings[thisReading] = 0;
}
Serial.println("OLED FeatherWing test");
// by default, we'll generate the high voltage from the 3.3v line internally! (neat!)
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // initialize with the I2C addr 0x3C (for the 128x32)
// init done
Serial.println("OLED begun");
// Show image buffer on the display hardware.
// Since the buffer is intialized with an Adafruit splashscreen
// internally, this will display the splashscreen.
display.display();
delay(1000);
// Clear the buffer.
display.clearDisplay();
display.display();
Serial.println("IO test");
pinMode(BUTTON_A, INPUT_PULLUP);
pinMode(BUTTON_B, INPUT_PULLUP);
pinMode(BUTTON_C, INPUT_PULLUP);
// text display tests
display.setTextSize(4);
display.setTextColor(WHITE);
display.setCursor(0,0);
display.clearDisplay();
fsrScaledOld=fsrScaled;
/*display.print("Connecting to SSID\n'adafruit':");
display.print("connected!");
display.println("IP: 10.0.1.23");
display.println("Sending val #0");
display.setCursor(0,0);
display.display(); // actually display all of the above
*/
}
void loop() {
/*if (! digitalRead(BUTTON_A)) display.print("A");
if (! digitalRead(BUTTON_B)) display.print("B");
if (! digitalRead(BUTTON_C)) display.print("C");*/
// subtract the last reading:
total = total - readings[readIndex];
// read from the sensor:
readings[readIndex] = analogRead(fsrAnalogPin);
// add the reading to the total:
total = total + readings[readIndex];
// advance to the next position in the array:
readIndex = readIndex + 1;
// if we're at the end of the array...
if (readIndex >= numReadings) {
// ...wrap around to the beginning:
readIndex = 0;
}
// calculate the average:
average = total / numReadings;
// send it to the computer as ASCII digits
Serial.println(average);
delay(1); // delay in between reads for stability
//delay(1000);
//display.clearDisplay();
//fsrReading = analogRead(fsrAnalogPin);
fsrReading = average;
Serial.print("Resistance Level = ");
fsrScaled = (fsrReading*0.07);
Serial.println(fsrScaled);
//display.println("Resist Lvl");
display.setCursor(50, 0);
display.setTextColor(BLACK);
display.print(fsrScaledOld);
display.print(fsrScaled);
display.setCursor(50, 0);
display.setTextColor(WHITE);
display.print(fsrScaled);
//fsrScaledOld=fsrScaled;
//if(fsrScaled==fsrScaledOld)
/* if(counter < 5)
{
display.setCursor(50, 16);
display.setTextColor(BLACK);
display.print(fsrScaled);
display.print(fsrScaledOld);
display.setCursor(50, 16);
display.setTextColor(WHITE);
display.println(fsrScaledOld);
}
/*else if(counter < 5)
{
if(fsrScaled > fsrScaledBnc + 3 && fsrScaled < fsrScaledBnc - 3)
{
display.setCursor(50, 16);
display.setTextColor(BLACK);
display.print(fsrScaled);
//display.print(fsrScaledOld);
display.setCursor(50, 16);
display.setTextColor(WHITE);
display.println(fsrScaledBnc);
//counter = 0;
}
}
else{
display.setCursor(50, 16);
display.setTextColor(BLACK);
//display.print(fsrScaled);
display.print(fsrScaledOld);
display.setCursor(50, 16);
display.setTextColor(WHITE);
display.print(fsrScaled);
fsrScaledOld=fsrScaled;
}*/
display.setCursor(0,0);
delay(100);
yield();
display.display();
// Clear the buffer.
//display.clearDisplay();
//display.display();
fsrScaledOld=fsrScaled;
counter = counter + 1;
if (counter > 5)
{
counter = 0;
//fsrScaledBnc = fsrScaled;
}
}