-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSMD_spinner.ino
387 lines (331 loc) · 10.3 KB
/
SMD_spinner.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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
/*
Not all Attiny cores support the tone() function. Try this one
https://github.com/SpenceKonde/ATTinyCore
// ATMEL ATTINY84 / ARDUINO
//
// +-\/-+
// VCC 1| |14 GND
// (D 0) PB0 2| |13 AREF (D 10)
// (D 1) PB1 3| |12 PA1 (D 9)
// PB3 4| |11 PA2 (D 8)
// PWM INT0 (D 2) PB2 5| |10 PA3 (D 7)
// PWM (D 3) PA7 6| |9 PA4 (D 6)
// PWM (D 4) PA6 7| |8 PA5 (D 5) PWM
*/
#include <EEPROMex.h>
#include <avr/pgmspace.h>
#include "font.h"
#include "textAndShapes.h"
const int charHeight = 8;
const int charWidth = 5;
int rows= 8; // Total LED's in a row
int LEDS[] = {10, 9, 8, 7, 6, 5, 3, 4};
bool STATES[] = {LOW, LOW, LOW, LOW, LOW, LOW, LOW, LOW};
char charArray[6]; // holds characters to display
unsigned long lastTimeUs; // time (us) magnet sensed
unsigned long revTimeUs; // time (us) of last full rotation
unsigned long dwellTimeUs; // time (us) between LED changes (based on rotation speed)
volatile unsigned long revolutions = 0; // track number of revolutions since last start
long totalRevolutions; // track total number of revolutions (stored in EEPROM)
bool spinning = true; // track reset of time & counts
unsigned long startTimeUs; // time (us) current spinning started
int mode; // current operating mode, stored in EEPROM
int modes = 8; // number of modes available
// 0 -> text "Hello World!"
// 1 -> RPM
// 2 -> time in seconds
// 3 -> spin count
// 4 -> spin count (total)
// 5 -> "lilly pad" pattern
// 6 -> shape 1 (heart)
// 7 -> shape 2 (smile)
byte ledPin = 0;
byte magPin = 0; // Hall effect sensor,
byte magPullUp = 0; // Pin A0 / D0
byte touchPin = 1; // Push button A1 / D1
byte touchPullUp = 1;
volatile boolean rotationFlag = false; // modified by ISR
void setup(){
// setup inputs
pinMode(touchPin, INPUT);
digitalWrite(touchPullUp, HIGH);
pinMode(magPin, INPUT_PULLUP);
digitalWrite(magPullUp, HIGH);
// setup other LEDs
for(int LED=0; LED<(sizeof(LEDS)/sizeof(int)); LED++){
pinMode(LEDS[LED], OUTPUT);
digitalWrite(LEDS[LED], STATES[LED]);
}
// Interupt setting
GIMSK = bit (PCIE0); // Pin change interrupt enable
PCMSK0 = bit (PCINT0); // Enable interupt on PCINT0 (D10)
sei(); // enables interrupts
// get saved mode from eeprom
mode = EEPROM.read(0);
if (mode >= modes) {
mode = 0; // may be very large first time
}
// get saved revolution total from eeprom
totalRevolutions = EEPROMReadlong(1);
if (totalRevolutions < 0 || totalRevolutions > 999999UL){ // will be -1 first time.
totalRevolutions = 0;
EEPROMWritelong(1, 0);
}
// show we are alive and what mode is active
blipLEDs();
lastTimeUs = micros();
}
void loop(){
int sigFigs = 6; // number of significant figures to deplay
unsigned long curTimeUs;
int seconds;
unsigned int rpm;
checkButton();
if (((micros() - lastTimeUs) > 1000000UL)){ // less than 1 rev / sec
if (spinning){
spinning = false;
totalRevolutions = totalRevolutions + revolutions;
EEPROMWritelong(1, totalRevolutions);
revolutions = 0;
clearArray();
blipLEDs();
}
else {
//digitalWrite(LEDS[mode], LOW);
}
}
if (mode == 5){ // lilly pad pattern, show regardles of magnet.
for(int LED=0; LED<(sizeof(LEDS)/sizeof(int)); LED++){
digitalWrite(LEDS[LED], HIGH);
delay(1);
digitalWrite(LEDS[LED], LOW);
}
for(int LED=(sizeof(LEDS)/sizeof(int))-1; LED >= 0; LED--){
digitalWrite(LEDS[LED], HIGH);
delay(1);
digitalWrite(LEDS[LED], LOW);
}
}
else if (rotationFlag){ // we are spinning!
rotationFlag = false;
if (!spinning){
spinning = true;
startTimeUs = micros();
}
curTimeUs = micros();
revTimeUs = curTimeUs - lastTimeUs;
dwellTimeUs = revTimeUs * 3UL / 360UL; // 3 degrees
seconds = (curTimeUs - startTimeUs) / 1000000UL;
rpm = 60000 * 1000 / revTimeUs;
lastTimeUs = curTimeUs;
clearArray();
if (mode == 0){
strcpy (charArray, text);
//sprintf(charArray, "%lu", dwellTimeUs);
}
else if (mode == 1){
sprintf(charArray, "%d", rpm);
sigFigs = 2;
}
else if (mode == 2){
sprintf(charArray, "%d", seconds);
}
else if (mode == 3){
sprintf(charArray, "%lu", revolutions);
}
else if (mode == 4){
sprintf(charArray, "%lu", totalRevolutions + revolutions);
}
else if (mode == 6){ // shape 1 (heart)
for(int k=0; k< sizeof(shape_1);k++){
if (rotationFlag){
break;
}
char b = pgm_read_byte_near(&(shape_1[k]));
for (int j=0; j<charHeight; j++) {
digitalWrite(LEDS[j], bitRead(b, 7-j));
}
dwellTimeUs = revTimeUs * 4UL / 360UL; // 5 degrees
delayMicroseconds(dwellTimeUs);
}
}
else if (mode == 7){ // shape 2 (smile)
for(int k=0; k< sizeof(shape_2);k++){
if (rotationFlag){
break;
}
char b = pgm_read_byte_near(&(shape_2[k]));
for (int j=0; j<charHeight; j++) {
digitalWrite(LEDS[j], bitRead(b, 7-j));
}
dwellTimeUs = revTimeUs * 4UL / 360UL; // 5 degrees
delayMicroseconds(dwellTimeUs);
}
}
// Text in top half
if (mode < 5) {
int digits = 0;
for(int k=0; k< sizeof(charArray);k++){
char c = charArray[k];
if (rotationFlag){
break;
}
if(c){
if (digits < sigFigs){
printLetter(c);
//digits += 1;
}
else{
printLetter('0');
}
digits += 1;
}
}
// Handle display in lower section
clearArray();
if(1 && (revTimeUs < 200000)){
char * ptr = (char *) pgm_read_word (&string_table[mode]);
//char buffer [6]; // must be large enough!
strcpy_P (charArray, ptr);
// wait for it . . .
while((micros() < (lastTimeUs + revTimeUs / 2)) && !rotationFlag){};
// show it
for (int k=sizeof(charArray)-1; k>=0; k--){
if (rotationFlag){
break;
}
printLetterLower(charArray[k]);;
}
}
}
}
}
ISR(PCINT0_vect){ // Magnet sensed
if (!digitalRead(magPullUp)){
rotationFlag = true; // Increment volatile variables
revolutions += 1;
}
}
void dwellDelay(){ // avoid glitch on first rotation having erronious value
if (dwellTimeUs > 2000){
dwellTimeUs = 2000;
}
if (dwellTimeUs < 100){
dwellTimeUs = 100;
}
delayMicroseconds(dwellTimeUs);
}
void printLetter(char ch){
// https://github.com/reger-men/Arduion-POV-clock/blob/master/clock.ino
// make sure the character is within the alphabet bounds (defined by the font.h file)
// if it's not, make it a blank character
if (ch < 32 || ch > 126){
ch = 32;
}
// subtract the space character (converts the ASCII number to the font index number)
ch -= 32;
// step through each byte of the character array
for (int i=0; i<charWidth; i++) {
char b = pgm_read_byte_near(&(font[ch][i]));
for (int j=0; j<charHeight; j++) {
digitalWrite(LEDS[j], bitRead(b, 7-j));
}
dwellDelay();
}
//clear the LEDs
for (int i = 0; i < rows; i++)
digitalWrite(LEDS[i] , LOW);
dwellDelay();
}
void printLetterLower(char ch){
// make sure the character is within the alphabet bounds (defined by the font.h file)
// if it's not, make it a blank character
if (ch < 32 || ch > 126){
ch = 32;
}
// subtract the space character (converts the ASCII number to the font index number)
ch -= 32;
// step through each byte of the character array
for (int i=charWidth-1; i>-1; i--) {
char b = pgm_read_byte_near(&(font[ch][i]));
for (int j=0; j<charHeight; j++) {
digitalWrite(LEDS[j+1], bitRead(b,j));
}
dwellDelay();
}
//clear the LEDs
for (int i = 0; i < rows; i++)
digitalWrite(LEDS[i] , LOW);
// space between letters
dwellDelay();
}
bool touched(){
// returns true if touched, false if not. Light LED until touch released
bool touchVal = digitalRead(touchPullUp);
if (!touchVal){
while(!digitalRead(touchPullUp)){ // wait till touch release
delay(10);
digitalWrite(LEDS[mode], LOW);
}
//digitalWrite(LEDS[0], LOW);
return (true);
}
else{
return (false);
}
}
void checkButton(){
// check button for mode change and display current mode
if (touched()){
mode += 1;
if (mode >= modes){
mode = 0;
}
EEPROM.write(0, mode);
blipLEDs();
}
}
void blipLEDs(){
// something to show we are alive
for(int LED=0; LED<(sizeof(LEDS)/sizeof(int)); LED++){
digitalWrite(LEDS[LED], HIGH);
delay(10);
digitalWrite(LEDS[LED], LOW);
}
for(int LED=sizeof(LEDS)/sizeof(int); LED>mode; LED--){
digitalWrite(LEDS[LED], HIGH);
delay(10);
digitalWrite(LEDS[LED], LOW);
}
digitalWrite(LEDS[mode], HIGH);
}
void EEPROMWritelong(int address, long value) {
//This function will write a 4 byte (32bit) long to the eeprom at
//the specified address to address + 3.
//https://playground.arduino.cc/Code/EEPROMReadWriteLong
//Decomposition from a long to 4 bytes by using bitshift.
//One = Most significant -> Four = Least significant byte
byte four = (value & 0xFF);
byte three = ((value >> 8) & 0xFF);
byte two = ((value >> 16) & 0xFF);
byte one = ((value >> 24) & 0xFF);
//Write the 4 bytes into the eeprom memory.
EEPROM.write(address, four);
EEPROM.write(address + 1, three);
EEPROM.write(address + 2, two);
EEPROM.write(address + 3, one);
}
long EEPROMReadlong(long address){
//Read the 4 bytes from the eeprom memory.
long four = EEPROM.read(address);
long three = EEPROM.read(address + 1);
long two = EEPROM.read(address + 2);
long one = EEPROM.read(address + 3);
//Return the recomposed long by using bitshift.
return ((four << 0) & 0xFF) + ((three << 8) & 0xFFFF) + ((two << 16) & 0xFFFFFF) + ((one << 24) & 0xFFFFFFFF);
}
void clearArray(){
for(int i=0; i<sizeof(charArray); i++){ // clear array
charArray[i] = 0;
}
}