-
Notifications
You must be signed in to change notification settings - Fork 3
/
onewheel_lights.ino
159 lines (137 loc) · 3.28 KB
/
onewheel_lights.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
#include <Adafruit_NeoPixel.h>
#include <VescUart.h>
// LED related
#define PIN A5
#define PIXEL 18
Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL, PIN, NEO_GRB + NEO_KHZ800);
int ledState = LOW; // ledState used to set the LED
unsigned long previousLedMillis = 0; // will store last time LED was updated
const long interval = 1000; // interval at which to blink (milliseconds)
// VESC related
VescUart UART;
//Setup---------------------------------------------------------------------------------------------------------------------
void setup()
{
strip.begin();
strip.show(); // Initialize all pixels to 'off'
strip.setBrightness(50);
showBlue();
Serial.begin(115200);
while (!Serial) {;}
UART.setSerialPort(&Serial);
showBlack();
}
void reportError()
{
unsigned long currentLedMillis = millis();
if (currentLedMillis - previousLedMillis >= interval) {
// save the last time you blinked the LED
previousLedMillis = currentLedMillis;
uint32_t color;
// if the LED is off turn it on and vice-versa:
if (ledState == LOW) {
ledState = HIGH;
showRed();
} else {
ledState = LOW;
showBlack();
}
}
}
void showBlack()
{
uint32_t color;
color = strip.Color(0, 0, 0);
for(int i=0; i<strip.numPixels(); i++) {
strip.setPixelColor(i, color);
}
strip.show();
}
void showWhite()
{
uint32_t color;
color = strip.Color(255, 255, 255);
for(int i=0; i<strip.numPixels(); i++) {
strip.setPixelColor(i, color);
}
strip.show();
}
void showRed()
{
uint32_t color;
color = strip.Color(255, 0, 0);
for(int i=0; i<strip.numPixels(); i++) {
strip.setPixelColor(i, color);
}
strip.show();
}
void showGreen()
{
uint32_t color;
color = strip.Color(0, 255, 0);
for(int i=0; i<strip.numPixels(); i++) {
strip.setPixelColor(i, color);
}
strip.show();
}
void showBlue()
{
uint32_t color;
color = strip.Color(0, 0, 255);
for(int i=0; i<strip.numPixels(); i++) {
strip.setPixelColor(i, color);
}
strip.show();
}
void showForwardDrivingLights()
{
uint32_t color;
color = strip.Color(255, 255, 255);
for(int i=0; i<(strip.numPixels() / 2); i++) {
strip.setPixelColor(i, color);
}
color = strip.Color(255, 0, 0);
for(int i=(strip.numPixels() / 2); i<strip.numPixels(); i++) {
strip.setPixelColor(i, color);
}
strip.show();
}
void showBackwardDrivingLights()
{
uint32_t color;
color = strip.Color(255, 0, 0);
for(int i=0; i<(strip.numPixels() / 2); i++) {
strip.setPixelColor(i, color);
}
color = strip.Color(255, 255, 255);
for(int i=(strip.numPixels() / 2); i<strip.numPixels(); i++) {
strip.setPixelColor(i, color);
}
strip.show();
}
void loop()
{
if ( UART.getVescValues() ) {
if(UART.data.rpm < -10) {
showForwardDrivingLights();
}
if(UART.data.rpm > 10) {
showBackwardDrivingLights();
}
/* Available data.
data.tempMotor
data.avgMotorCurrent
data.avgInputCurrent
data.rpm
data.inpVoltage
data.watt_hours
data.watt_hours_charged
data.fault
*/
}
else
{
// Blink red lights.
reportError();
}
}