-
Notifications
You must be signed in to change notification settings - Fork 4
/
tapTempoLFO.ino
755 lines (572 loc) · 16.1 KB
/
tapTempoLFO.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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
/*
TAP-tempo LFO
( designed for arduino nano ATmega328P/16MHz/5v and I2C LCD16*2 )
- Pedal & button Tap-IN
- Analog Sync IN
- Sync OUT (analog & Midi)
- LFO out with 7 waveForms
- Divided analog Clock OUT
- Tap tempo can be constrain in settable up & dw limits, avoiding bad taps
-- Config at start
Press TAP-button and plug power on , keep TAP-button press , adjust preset with rotactors and knobs.
- "Clock-OUT lenght" can be set with DIVIDER rotactor from 2/48 beat to 20/48 beat.
- "Clock-OUT polarity" can be set with LIMIT knob.
- "running Beats-Until-Chain-Reset" can be set with BPM knob from 2 to 8
- "Total-Tap to compute" can be set with WAVEFORM rotactor from 2 to 9
Release TAP button to save.
____ __ ______ _ ____ ___ ___ ___
/ __/_ __/ /___ _____/_ __/______ ___ (_)___ |_ // _ \/ _ \/ _ \
/ _// // / __/ // / __// / / __/ _ \/ _ \/ / __/ _/_ </ // / // / // /
/_/ \_,_/\__/\_,_/_/ /_/ /_/ \___/_//_/_/\__/ /____/\___/\___/\___/
with love.
*/
/*** includes ***/
#include <EEPROM.h>
#include <EEWrap.h>
#include <TimerOne.h>
#include <elapsedMillis.h>
// LCD lib
#include <LiquidCrystal_I2C.h>
//include custom characteres
#include "customChar.h"
// include the modified ArduinoTapTempo library
#include "ArduinoTapTempo_mod.h"
/* include lfo waveform looking-up table */
#include "lut48x256.h"
/*** CONFIG ***/
#define TAP_DEBOUNCE_MS 5UL //tap button debounce time in millis
/*** defines ***/
#define WAVE_SEL A3 //waveform selector
#define BPM_POT A1 //manual bpm pot
#define LIMIT_POT A2 //limit bpm pot
#define DIVIDER_SEL A0 //clock divider selector
#define TAP_PIN 3 // the one with int1 !!
#define SYNC_OUT_PIN 8 //
#define SYNC_OUT PORTB , 0
#define CLK_OUT_PIN 9 // divided clock out
#define CLK_OUT PORTB , 1
#define PWM_OUT_PIN 11 //must be timer2 OC2A pin
#define PWM_OUT PORTB , 3
#define LED_ONBOARD_PIN 13 // led
#define LED_ONBOARD PORTB , 5
#define CLOCK 0xF8 // midi clock message
//lfo stuff ******************************************
#define TIMER2_TIME 16UL //in microsecond
#define PWM11 OCR2A //shortcut
/** some MACROS **/
#define SET_CLK_OUT (PORTB |=(1<<1))
#define CLR_CLK_OUT (PORTB &= (~(1<<1)))
#define SET_SYNC_OUT (PORTB |=(1<<0))
#define CLR_SYNC_OUT (PORTB &= (~(1<<0)))
#define SET_LED (PORTB |=(1<<5))
#define CLR_LED (PORTB &= (~(1<<5)))
#define SET(x,y) (x |=(1<<y)) //-Bit set/clear macros
#define CLR(x,y) (x &= (~(1<<y))) // |
#define CHK(x,y) (x & (1<<y)) // |
#define TOG(x,y) (x^=(1<<y)) //-+
/*** Vars ***/
unsigned int divider = 2;
const uint8_t clk_dividers[10]={ 96, 64, 48, 32, 24, 16, 12, 8, 6, 1 }; // clock divider setting
volatile unsigned int tick_counter = 0;
volatile unsigned int sync_counter = 0;
volatile unsigned int max_tick = 48;
volatile bool flag_inhibit_tap = false;
volatile bool flag_tempo_change = false;
volatile unsigned long _q; // timer1 time in micros
volatile unsigned char wave_sel = 0;
volatile float low_limit, high_limit;
elapsedMillis TS_last_run = 0; //unsigned long TS_last_run = 0;
elapsedMicros TS_last_int1 = 0; //unsigned long TS_last_int1 = 0;
unsigned int bpm_pot;
unsigned int old_limit_pot;
/* config vars */
uint8_t clockOutLenght = 4;
bool clockOutPolarity = false;
uint8_t beatsUntilChainRst = 4;
uint8_t totalTapVal = 4;
/*lfo ( timer2 interrupt stuff ) */
volatile unsigned long totIteToNextTableStep=5000;
volatile unsigned long timer2acc = 0; //
volatile unsigned int lfoTableStep = 0; //
//EEPROM *******************************************
uint8_e savedClockOutLenght EEMEM;
bool_e savedClockOutPolarity EEMEM;
uint8_e savedBeatsUntilChainRst EEMEM;
uint8_e savedTotalTapVal EEMEM;
// make an ArduinoTapTempo object
ArduinoTapTempo tapTempo;
//make an lcd
LiquidCrystal_I2C lcd(0x27, 16, 2); // set the LCD address to 0x27 for a 16 chars and 2 line display
/*** interrupts routines **************************************************************************************/
/* timer2 overflow interrupt service routine --> lfo pwm */
ISR(TIMER2_OVF_vect)
{
timer2acc ++; //
if( timer2acc >= totIteToNextTableStep )
{
timer2acc = 0; //
//go next lfo table step
lfoTableStep++;
if(lfoTableStep >= WAVEFORM_MAX_STEP)lfoTableStep= 0;
if(wave_sel != ( WAVEFORM_TYPES - 1 )) //not rnd
{
PWM11=*(waveform[wave_sel] + lfoTableStep);
}else{
if(lfoTableStep == 0)PWM11=random(255); //random case
}
}
}
/* timer1 int ***/
void beatClockInt()
{
//
tick_counter++;
sync_counter++;
if ( flag_tempo_change )
{
flag_tempo_change = false;
Timer1.setPeriod( _q );
setLFO_Period( _q * max_tick );
}
// divided clock out
if ( tick_counter >= max_tick )
{
tick_counter = 0;
if (clockOutPolarity)
{
SET_CLK_OUT;
}
else
{
CLR_CLK_OUT;
}
resetLFO_Period( _q * max_tick );
}
else if ( tick_counter >= clockOutLenght || tick_counter >= (max_tick - 1))
{
if (clockOutPolarity)
{
CLR_CLK_OUT;
}
else
{
SET_CLK_OUT;
}
}
// analog sync out
if ( sync_counter >= 48 )
{
sync_counter = 0;
SET_SYNC_OUT;
SET_LED;
// update ArduinoTapTempo if in manual (knob) bpm
if (flag_inhibit_tap)
{
tapTempo.setBeatLength( ( _q / 1000UL ) * 48UL ); // _q * 1000
tapTempo.resetTapChain();
flag_inhibit_tap = false;
}
}
else if ( sync_counter >= 8 )
{
CLR_SYNC_OUT;
CLR_LED;
tapTempo.update(false);
}
/*
// midi clock output
if ( ! CHK(sync_counter, 0) ) // fire on even number
{
Serial.write((char)CLOCK);
}
*/
}
/* INT1 interrupt (TAP)***/
void button_int()
{
bool flagNewTap;
unsigned long new_q;
unsigned long diff_TS;
diff_TS = TS_last_int1;//(millis() - TS_last_int1);
if ( ! flag_inhibit_tap )
{
if ( diff_TS > TAP_DEBOUNCE_MS * 1000) //sort of poor debounce
{
flagNewTap = ! tapTempo.isChainActive();
// update ArduinoTapTempo
if (flagNewTap)
{
tapTempo.update(true);
TS_last_int1 = 0; //millis();
tick_counter = 255; //will overflow next beatclock() ... was 0;
/*
if (clockOutPolarity)
{
SET_CLK_OUT;
}
else
{
CLR_CLK_OUT;
}
*/
resetLFO_Period( _q * max_tick );
sync_counter = 255; //will overflow next beatclock() ... was 0;
//force beatClockInt
beatClockInt();
/*
SET_SYNC_OUT;
SET_LED;
Serial.write((char)CLOCK);
Timer1.start();
// */
} else {
if (diff_TS < (60000000UL / uint16_t(low_limit)) && diff_TS > (60000000UL / uint16_t(high_limit)))
{
tapTempo.update(true);
TS_last_int1 = 0;//millis();
//chk if tempo has changed
new_q = (tapTempo.getBeatLength() * 1000UL) / 48UL;
if ( _q != new_q && tapTempo.getTapsInChain() > 2 ) //totalTapVal
{
_q = new_q;
flag_tempo_change = true;
}
if( tapTempo.getTapsInChain() == totalTapVal )
{
//resync divided CLOCK & LFO
tick_counter = 255; //will overflow next beatclock() ... was 0;
/*
if (clockOutPolarity)
{
SET_CLK_OUT;
}
else
{
CLR_CLK_OUT;
}
*/
resetLFO_Period( _q * max_tick );
sync_counter = 255; //will overflow next beatclock() ... was 0;
//force beatClockInt
beatClockInt();
/*
SET_SYNC_OUT;
SET_LED;
Serial.write((char)CLOCK);
Timer1.start();
*/
}
} else {
tapTempo.update(false);
tapTempo.resetTapChain();
}
}
}
}
}
/** routines ****************************************************/
unsigned long get_int1_BPM()
{
return 60000000UL / ( _q * 48UL );
}
/********* RUN ONCE *********************************************/
void setup()
{
// Set MIDI baud rate: *************************************
Serial.begin(31250); // set midi baudrate
Serial.write(0xFF); // "is alive" midi msg
// setup tap button
pinMode(TAP_PIN, INPUT_PULLUP);
digitalWrite(TAP_PIN, HIGH);
delay(100);
//setup analog inputs
pinMode(BPM_POT, INPUT);
pinMode(LIMIT_POT, INPUT);
pinMode(DIVIDER_SEL, INPUT);
//setup outputs
pinMode (SYNC_OUT_PIN, OUTPUT);
pinMode (CLK_OUT_PIN, OUTPUT);
pinMode (PWM_OUT_PIN, OUTPUT);
pinMode (LED_ONBOARD_PIN, OUTPUT);
// init pseudo random
randomSeed(analogRead(BPM_POT));
//init LCD
initLCD();
//get config at start
if (! digitalRead(TAP_PIN))
{
ConfigMenu();
}
//load config from eeprom
clockOutLenght = savedClockOutLenght; //load from eeprom
clockOutPolarity = savedClockOutPolarity;
totalTapVal = savedTotalTapVal;
beatsUntilChainRst = savedBeatsUntilChainRst;
/* tapTempo config ********** */
tapTempo.disableSkippedTapDetection();
// tapTempo.enableSkippedTapDetection();
//tapTempo.setSkippedTapThresholdLow(1.8);
// tapTempo.setSkippedTapThresholdHigh(2.2);
tapTempo.setBeatsUntilChainReset(beatsUntilChainRst);
tapTempo.setTotalTapValues(totalTapVal);
// init interrupt on TAP button
attachInterrupt(INT1, button_int, FALLING);
//init timer1
Timer1.initialize(float(( 60000.0 / 120.0 ) * 1000.00 / 48.00));
Timer1.attachInterrupt(beatClockInt);
//init timer2 (lfo pwm generator)
initTimer2_PWM();
enable_Timer2_intr();
//last inits
TS_last_run = 0; //millis();
flag_inhibit_tap = false;
tapTempo.update(false);
}
/********* LOOP forever ******************************************/
void loop()
{
tapTempo.update(false);
if ( true || TS_last_run > 20UL )
{
TS_last_run = 0; //millis();
float bpm;
//first chk if manual bpm has changed
unsigned int new_bpm_pot = (analogRead(BPM_POT) >> 2) + 30;
if ( new_bpm_pot != bpm_pot )
{
flag_inhibit_tap = true;
tapTempo.resetTapChain();
bpm_pot = new_bpm_pot;
bpm = new_bpm_pot;
//tapTempo.setMinBPM(30.0);
//tapTempo.setMaxBPM(255.0);
_q = 1250000UL / bpm;
flag_tempo_change = true;
}
/* chk limit pot */
unsigned int limit_pot = analogRead(LIMIT_POT) >> 3;
if ( true || limit_pot != old_limit_pot || flag_tempo_change)
{
old_limit_pot = limit_pot;
bpm = get_int1_BPM();//tapTempo.getBPM();
low_limit = float(constrain((bpm - limit_pot), 30.0, bpm));
high_limit = float(constrain((bpm + limit_pot), bpm, 284.0));
}
/* chk if divider rotary has changed */
unsigned int new_divider = map( analogRead(DIVIDER_SEL), 0, 1024, 0, 10 );
if ( divider != new_divider)
{
divider = new_divider;
max_tick = clk_dividers[divider];
//flag_divider_change = true;
}
/* chk lfo wave_sel_pot */
wave_sel = map( analogRead(WAVE_SEL), 0, 1024, 0, 8 );
/* update LCD */
RefreshLcd( int(get_int1_BPM()), low_limit, high_limit, tapTempo.isChainActive(), max_tick, wave_sel);
}
}
/** LFO stuff *****************/
void initTimer2_PWM()
{
pinMode(PWM_OUT_PIN, OUTPUT);
TCCR2A = 0;
TCCR2A = _BV(COM2A1) | _BV(WGM21) | _BV(WGM20); // fastPWM
TCCR2B = 0;
TCCR2B = _BV(CS20); //no prescaler
PWM11= 127; //
}
void enable_Timer2_intr()
{
TIMSK2 = _BV(TOIE2);
}
void disable_Timer2_intr()
{
TIMSK2 = 0;
}
void setPWM11( uint8_t duty )
{
PWM11=duty;
}
void setLFO_Period( unsigned long timeMicros )
{
totIteToNextTableStep = ( timeMicros / ( WAVEFORM_MAX_STEP * TIMER2_TIME ) );// nb of iteration to go next step
}
void resetLFO_Period( unsigned long timeMicros ) //put in beatclock
{
//set LFO speed
setLFO_Period( timeMicros );
//reset lfo to start
//disable_Timer2_intr(); // not needed since resetLFO_Period() is call in interupt
timer2acc=totIteToNextTableStep ;// will reset next timer2ovf
lfoTableStep = WAVEFORM_MAX_STEP; //will reset next time timer2ovf
//enable_Timer2_intr();
}
/** LCD stuff *****************/
void initLCD()
{
// init Transmission
Wire.begin();
Wire.beginTransmission(0x27);
//create custom char
delay(100);
lcd.createChar(0, blanche);
delay(10);
lcd.createChar(1, noire);
delay(10);
lcd.createChar(2, croche);
delay(10);
lcd.createChar(3, dblcroche);
delay(10);
lcd.createChar(4, triolet);
delay(10);
lcd.begin(16, 2, LCD_5x8DOTS); // initialize the lcd
delay(100);
//hello lcd screen
lcd.setBacklight(255);
lcd.home();
lcd.clear();
lcd.print(F("Tap-tempo LFO"));
lcd.setCursor(0, 1);
lcd.print(F("FuturTronic 3000"));
delay(1500);
//re-create custom char sometimes CGRam hangs
lcd.createChar(0, blanche);
delay(10);
lcd.createChar(1, noire);
delay(10);
lcd.createChar(2, croche);
delay(10);
lcd.createChar(3, dblcroche);
delay(10);
lcd.createChar(4, triolet);
delay(10);
return;
}
void RefreshLcd(unsigned int _bpm, unsigned int _low_limit, unsigned int _high_limit, bool tap_again, int _div, int _waveform)
{
/* first line : "low < bpm < hgh " **************/
lcd.setCursor(0, 0);
lcd.print(_low_limit);
lcd.print(F(" "));
lcd.setCursor(3, 0);
if ( _bpm == _low_limit )
{
lcd.print(F(" = "));
}
else
{
lcd.print(F(" < "));
}
lcd.setCursor(6, 0);
lcd.print(_bpm);
lcd.print(F(" "));
lcd.setCursor(9, 0);
if ( _bpm == _high_limit )
{
lcd.print(F(" = "));
}
else
{
lcd.print(F(" < "));
}
lcd.print(_high_limit);
lcd.print(F(" "));
/*second line : "[DIV][TAP][WAVF]" *****************/
lcd.setCursor(0, 1);
//Divider
if ( _div == 1 )
{
lcd.print(F("[HLD]"));
}
else
{
lcd.print(F("[ "));
lcd.write((uint8_t)noteChar[divider][0]);
lcd.write((uint8_t)noteChar[divider][1]);
lcd.print(F("]"));
}
//TAP chain active or not
if (tap_again)
{
lcd.print(F("[TAP]"));
}
else
{
lcd.print(F("[ ]"));
}
//Waveform
lcd.setCursor(10, 1);
lcd.print(F("["));
lcd.print(waveformName[_waveform]);
lcd.print(F("]"));
return;
}
void ConfigMenu()
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(F(" Keep TAP press,"));
lcd.setCursor(0, 1);
lcd.print(F("release to save."));
delay(1500);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(F("CLK Lgt: Pol: "));
lcd.setCursor(0, 1);
lcd.print(F("TAP Tot: Rst: "));
while ( ! digitalRead(TAP_PIN))
{
clockOutLenght = map( analogRead(DIVIDER_SEL), 0, 1024, 2, 21 );
lcd.setCursor(8, 0);
lcd.print(clockOutLenght);
if (clockOutLenght < 10)lcd.print(F(" "));
clockOutPolarity = analogRead(LIMIT_POT)>>9; //map( analogRead(LIMIT_POT), 0, 1024, 0, 1 );
lcd.setCursor(15, 0);
if (clockOutPolarity)
{
lcd.print(F("+"));
} else {
lcd.print(F("-"));
}
totalTapVal = map( analogRead(WAVE_SEL), 0, 1024, 2, 10 );
lcd.setCursor(8, 1);
lcd.print(totalTapVal);
beatsUntilChainRst = map( analogRead(BPM_POT), 0, 1024, 2, 10 );
lcd.setCursor(14, 1);
lcd.print(beatsUntilChainRst);
delay(100); //slow down
}
//button was released; save values in EEPROM
savedClockOutLenght = clockOutLenght; //save in eeprom
savedClockOutPolarity = clockOutPolarity; //save in eeprom
savedTotalTapVal = totalTapVal;
savedBeatsUntilChainRst = beatsUntilChainRst;
//display config
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(F("config saved"));
lcd.setCursor(0, 1);
lcd.print(F("L: P: T: R: "));
lcd.setCursor(2, 1);
lcd.print(savedClockOutLenght);
lcd.setCursor(7, 1);
if (savedClockOutPolarity)
{
lcd.print(F("+"));
} else {
lcd.print(F("-"));
}
lcd.setCursor(11, 1);
lcd.print(savedTotalTapVal);
lcd.setCursor(15, 1);
lcd.print(savedBeatsUntilChainRst);
delay(2000);
}
/* debug */
int freeRam () {
extern int __heap_start, *__brkval;
int v;
return (int) &v - (__brkval == 0 ? (int) &__heap_start : (int) __brkval);
}
// END OF FILE