This repository has been archived by the owner on Jul 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sketch_apr30a.ino
186 lines (127 loc) · 3.85 KB
/
sketch_apr30a.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
#define cbi( sfr, bit ) ( _SFR_BYTE( sfr ) &= ~_BV( bit ) )
#define sbi( sfr, bit ) ( _SFR_BYTE( sfr ) |= _BV( bit ) )
volatile byte Timewaster;
void setup()
{
/* Speed up timer 2 to make pins 3 and 11 output a blistering fast PWM. */
TCCR2B = TCCR2B & 0b11111000 | 1;
TCCR2A &= ~B11;
TCCR2A |= B011;
pinMode( 3, OUTPUT );
pinMode( 11, OUTPUT );
/* Do the same for timer 0. WARNING: Much of the Arduino library relies on timer 0 and you will find some functions output incorrect values. Look up the full effects and make sure you don't need it. */
TCCR0B = TCCR0B & 0b11111000 | 1;
TCCR0A &= ~B11;
TCCR0A |= B011;
pinMode( 5, OUTPUT );
pinMode( 6, OUTPUT );
analogWrite( 3, 0 );
analogWrite( 11, 0 );
analogWrite( 5, 0 );
analogWrite( 6, 0 );
/* Set the reference level to 1.1v */
sbi( ADMUX, REFS0 );
sbi( ADMUX, REFS1 );
/* Increase our input speed - prescaler to /8 */
cbi( ADCSRA, ADPS2 );
sbi( ADCSRA, ADPS1 );
sbi( ADCSRA, ADPS0 );
/* Select A0. */
cbi( ADMUX, MUX0 );
cbi( ADMUX, MUX1 );
cbi( ADMUX, MUX2 );
cbi( ADMUX, MUX3 );
/* Turn on the ADC. */
sbi( ADCSRA, ADEN );
/* Give it four cycles to sink in. */
Timewaster++;
Timewaster--;
Timewaster++;
Timewaster--;
/* Tell the ADC to get to work! */
sbi( ADCSRA, ADSC );
/* Our LED. */
pinMode( 13, OUTPUT );
}
unsigned short Buffer[ 256 ];
byte Ringstart = 0;
byte LFO = 0;
boolean LFODirection = 0;
unsigned short UpdateAgo = 0;
boolean UpdateWhich = 0;
byte LFOSpeed = 0;
byte LFOSlow = 0;
byte LFODepth = 0;
void loop()
{
/* Wait for the conversion to complete. */
while( !( ADCSRA & _BV( ADIF ) ) ){}
/* Grab the result. */
/* We need to grab ADCL first because accessing ADCH will discard ADCL. */
byte Low = ADCL;
unsigned short Left = ( ADCH << 8 ) | Low;
/* Clear the finished bit. */
cbi( ADCSRA, ADIF );
if( UpdateAgo ) UpdateAgo--; else
{
sbi( ADMUX, MUX1 );
if( UpdateWhich ) sbi( ADMUX, MUX0 );
/* Give it four cycles to sink in. */
Timewaster++;
Timewaster--;
Timewaster++;
Timewaster--;
/* Tell the ADC to get to work! */
sbi( ADCSRA, ADSC );
/* Wait for the conversion to complete. */
while( !( ADCSRA & _BV( ADIF ) ) ){}
/* Grab the result. */
/* We need to grab ADCL first because accessing ADCH will discard ADCL. */
byte Low = ADCL;
if( UpdateWhich )
{
LFODepth = ( ( ADCH << 8 ) | Low ) >> 2;
cbi( ADMUX, MUX0 );
UpdateWhich = 0;
}
else
{
LFOSpeed = ( ( ADCH << 8 ) | Low ) >> 2;
UpdateWhich = 1;
}
/* Clear the finished bit. */
cbi( ADCSRA, ADIF );
cbi( ADMUX, MUX1 );
UpdateAgo = 4096;
}
/* Give it four cycles to sink in. */
Timewaster++;
Timewaster--;
Timewaster++;
Timewaster--;
/* Tell the ADC to get to work! */
sbi( ADCSRA, ADSC );
Buffer[ Ringstart ] = Left;
Ringstart++;
if( LFOSlow ) LFOSlow--; else
{
if( LFODirection )
{
LFO++;
if( LFO == 255 ) LFODirection = 0;
}
else
{
LFO--;
if( LFO <= LFODepth ) LFODirection = 1;
}
LFOSlow = LFOSpeed;
}
byte Progress = Ringstart;
Progress += LFO;
Left += Buffer[ Progress ];
/* Scale it up to full 16-bit. */
Left <<= 5;
analogWrite( 3, Left >> 8 );
analogWrite( 11, Left & 255 );
}