-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhippo.ino
350 lines (305 loc) · 8.51 KB
/
hippo.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
// CODE TASKS
//************
// change specific neopixel on strip
// change neopixel brightness w/ potentiometer
// take button input
// calmly rotate servo ~30 degrees
// servos and neopixels dont work well together or at all fuck
// use attiny 85 for servo?(no)
// need new servo
#include <Adafruit_NeoPixel.h>
#include <Adafruit_TiCoServo.h>
// define pin uses
#define PIXEL_PIN 6
#define PIXEL_COUNT 15
#define BUTTON_PIN 23 //use input_pullup (check with uno first)
#define encoder0PinA 20
#define encoder0PinB 21
#define SERVO_PIN 3
Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, NEO_GRB + NEO_KHZ800);
Adafruit_TiCoServo hippoServo; // create servo object to control a servo
void colorFade(uint8_t wait);
void startShow(int i);
//uint32_t Wheel(byte WheelPos);
void checkTodaysWeather();
void checkSocialAccts();
void checkWebsite();
void checkTomorrowsWeather();
void updateButtonState();
int showType = 0;
int previousShowType = -1;
int reading; // the current reading from the input pin
int previous = HIGH; // the previous reading from the input pin
int encoder0Pos = 0;
int encoder0PinALast = LOW;
int n = LOW;
// the follow variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long time = 0; // the last time the output pin was toggled
long debounce = 100; // the debounce time, increase if the output flickers
boolean facebookNeedsUpdate = false;
boolean gmailNeedsUpdate = false;
int currentBrightness = 255;
int lastPotValue = -1;
//***********
//setup code
//***********
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
pinMode(BUTTON_PIN, INPUT);
pinMode(encoder0PinA, INPUT);
pinMode(encoder0PinB, INPUT);
hippoServo.attach(SERVO_PIN);
strip.begin();
strip.show();
pinMode(13, OUTPUT);
digitalWrite(13, HIGH);
}
//**********
//start main loop
//**********
void loop(){
updateButtonState();
updatePotentiometer();
// Perform the designated action (today weather, tomorrow weather, light show).
startShow(showType);
if(Serial.available() > 0){
char serialInString[20]; // Allocate some space for the string
int index = 0;
char inChar;
while (Serial.available() > 0) {
if(index < 19) {
inChar = Serial.read(); // Read a character
serialInString[index] = inChar; // Store it
index++; // Increment where to write next
}
}
serialInString[index] = '\0'; // Null terminate the string
if(strcmp(serialInString, "facebook") == 0){
facebookNeedsUpdate = true;
}
else if(strcmp(serialInString, "gmail") == 0){
gmailNeedsUpdate = true;
}
}
}
void updateButtonState(){
// Button reading code
reading = analogRead(BUTTON_PIN);
if(reading < 8){
reading = LOW;
}
else{
reading = HIGH;
}
if (reading == LOW && previous == HIGH && millis() - time > debounce) {
// Only 3 states for the button. Increment it.
showType = (showType + 1) % 3;
time = millis();
}
previous = reading;
}
void updatePotentiometer(){
n = digitalRead(encoder0PinA);
if ((encoder0PinALast == LOW) && (n == HIGH)){
if (digitalRead(encoder0PinB) == LOW)
encoder0Pos--;
//if (encoder0Pos >= 10)
//encoder0Pos = 10;
else
encoder0Pos++;
//if (encoder0Pos <= 0)
//encoder0Pos = 0;
}
Serial.println(encoder0Pos);
encoder0PinALast = n;
delay(20);
/*if(currentValue == 0){
return;
}
Serial.println(currentValue);
delay(20);
if(lastPotValue == -1){
lastPotValue = currentValue;
currentBrightness = 255;
}
int delta = lastPotValue - currentValue;
if(abs(delta) > 10 && abs(delta) < 40){
if(delta > 0){
currentBrightness = (currentBrightness + 25) % 256;
}
else{
currentBrightness = (currentBrightness - 25) % 256;
}
Serial.printf("Setting brightness to %d. Delta = %d\n", currentBrightness, delta);
}
else if(abs(delta) > 10){
Serial.printf("Delta=%d\n", delta);
delay(50);
}
lastPotValue = currentValue;*/
currentBrightness = encoder0Pos;
strip.setBrightness(currentBrightness);
strip.show();
}
void startShow(int i) {
int today;
int tomorrow;
switch(i){
case 0: {
checkTodaysWeather();
checkSocialAccts();
checkWebsite();
today = HIGH;
tomorrow = LOW;
break;
}
case 1: {
checkTomorrowsWeather();
checkSocialAccts();
checkWebsite();
today = LOW;
tomorrow = HIGH;
break;
}
case 2: {
colorFade(300);
today = LOW;
tomorrow = LOW;
break;
}
}
}
void colorFade(uint8_t wait) {
if(previousShowType != showType){
previousShowType = showType;
Serial.println("Color fading.");
}
// secret rainbow mode
uint16_t i, j;
for(j=0; j<256; j++) {
for(i=0; i<strip.numPixels(); i++) {
strip.setPixelColor(i, Wheel((i+j) & 255));
}
strip.show();
delay(wait);
updateButtonState();
updatePotentiometer();
if(showType != 2){
delay(500);
break;
}
}
}
//strip.setPixelColor(i+q, Wheel( (i+j) % 255)); //turn every third pixel on
//strip.show();
uint32_t Wheel(byte WheelPos) {
if(WheelPos < 85) {
return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
} else if(WheelPos < 170) {
WheelPos -= 85;
return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
} else {
WheelPos -= 170;
return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
}
}
void checkTodaysWeather(){
if(previousShowType != showType){
previousShowType = showType;
Serial.println("Checking today's weather.");
}
}
void checkTomorrowsWeather(){
if(previousShowType != showType){
previousShowType = showType;
Serial.println("Checking tomorrow's weather.");
}
}
void checkSocialAccts(){
if(facebookNeedsUpdate){
fadePixel(0, 59,89,152);
facebookNeedsUpdate = false;
}
else if(gmailNeedsUpdate){
fadePixel(0, 213,15,37);
facebookNeedsUpdate = false;
}
}
void fadePixel(int pixel, int r, int g, int b){
strip.setPixelColor(pixel, strip.Color(r,g,b));
int originalBrightness = currentBrightness;
for (int i = 0; i < originalBrightness; i++)
{
strip.setBrightness(i);
strip.show();
delay(10);
}
for (int i = originalBrightness; i > 0; i--)
{
strip.setBrightness(i);
strip.show();
delay(10);
}
}
void checkWebsite(){
}
/*
#define encoder0PinA 2
#define encoder0PinB 4
volatile unsigned int encoder0Pos = 0;
float encodercalc;
int ledpin = 3;
void setup() {
pinMode(encoder0PinA, INPUT);
digitalWrite(encoder0PinA, HIGH); // turn on pullup resistor
pinMode(encoder0PinB, INPUT);
digitalWrite(encoder0PinB, HIGH); // turn on pullup resistor
attachInterrupt(0, doEncoder, CHANGE); // encoder pin on interrupt 0 - pin 2
Serial.begin (9600);
Serial.println("start"); // a personal quirk
Serial.print("?c0");
Serial.print("?fNo. of revs:");
Serial.print("?k");
Serial.print("?BFF");
}
void loop(){
// do some stuff here - the joy of interrupts is that they take care of themselves
}
void doEncoder(){
if (digitalRead(encoder0PinA) == HIGH) { // found a low-to-high on channel A
if (digitalRead(encoder0PinB) == LOW) { // check channel B to see which way
// encoder is turning
encoder0Pos = encoder0Pos - 1; // CCW
}
else {
encoder0Pos = encoder0Pos + 1; // CW
}
}
else // found a high-to-low on channel A
{
if (digitalRead(encoder0PinB) == LOW) { // check channel B to see which way
// encoder is turning
encoder0Pos = encoder0Pos + 1; // CW
}
else {
encoder0Pos = encoder0Pos - 1; // CCW
}
}
Serial.print("?l");
encodercalc = (float)encoder0Pos / 56.00;
encodercalc = encodercalc * 100;
//encodercalc = encoder0Pos;
if (encoder0Pos < 257) {
analogWrite(ledpin, encoder0Pos);
}
Serial.println (encoder0Pos, DEC); // debug - remember to comment out
// before final program run
// you don't want serial slowing down your program if not needed
}
/* to read the other two transitions - just use another attachInterrupt()
in the setup and duplicate the doEncoder function into say,
doEncoderA and doEncoderB.
You also need to move the other encoder wire over to pin 3 (interrupt 1).
*/